Index: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php |
— | — | @@ -301,4 +301,65 @@ |
302 | 302 | array( 'edit' ), |
303 | 303 | ); |
304 | 304 | } |
| 305 | + |
| 306 | + /** |
| 307 | + * @dataProvider getUseFormatProvider |
| 308 | + */ |
| 309 | + public function testGetUseFormat( $explicit, $requestParam, $expected ) { |
| 310 | + global $wgRequest, $wgExtMobileFrontend; |
| 311 | + $wgRequest->setVal( 'useformat', $requestParam ); |
| 312 | + $wgExtMobileFrontend->setUseFormat( $explicit ); |
| 313 | + $this->assertEquals( $expected, $wgExtMobileFrontend->getUseFormat() ); |
| 314 | + } |
| 315 | + |
| 316 | + public function getUseFormatProvider() { |
| 317 | + return array( |
| 318 | + array( 'mobile', null, 'mobile' ), |
| 319 | + array( null, 'mobile', 'mobile' ), |
| 320 | + array( null, null, '' ), |
| 321 | + array( 'desktop', 'mobile', 'desktop' ), |
| 322 | + ); |
| 323 | + } |
| 324 | + |
| 325 | + public function testGetUseFormatCookieExpiry() { |
| 326 | + global $wgExtMobileFrontend, $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry; |
| 327 | + $getUseFormatCookieExpiry = self::getMethod( 'getUseFormatCookieExpiry' ); |
| 328 | + |
| 329 | + $origMFCookieExpiry = $wgMobileFrontendFormatCookieExpiry; |
| 330 | + $startTime = time(); |
| 331 | + $wgMobileFrontendFormatCookieExpiry = 60; |
| 332 | + $mfCookieExpected = $startTime + 60; |
| 333 | + $this->assertTrue( $mfCookieExpected == $getUseFormatCookieExpiry->invokeArgs( $wgExtMobileFrontend, array( $startTime ) ), 'Using MobileFrontend expiry.' ); |
| 334 | + |
| 335 | + $wgMobileFrontendFormatCookieExpiry = null; |
| 336 | + $defaultMWCookieExpected = $startTime + $wgCookieExpiration; |
| 337 | + $this->assertTrue( $defaultMWCookieExpected == $getUseFormatCookieExpiry->invokeArgs( $wgExtMobileFrontend, array( $startTime ) ), 'Using default MediaWiki cookie expiry.' ); |
| 338 | + |
| 339 | + // reset global back to original value |
| 340 | + $wgMobileFrontendFormatCookieExpiry = $origMFCookieExpiry; |
| 341 | + } |
| 342 | + |
| 343 | + /** |
| 344 | + * @outputBuffering enabled |
| 345 | + */ |
| 346 | + /*public function testCookie() { |
| 347 | + global $wgRequest; |
| 348 | + $wgRequest->response()->setCookie( 'foo', 'bar' ); |
| 349 | + $this->assertEquals( $wgRequest->getCookie( 'foo' ), 'bar' ); |
| 350 | + setcookie( 'foobar', 'pants' ); |
| 351 | + $this->asertEquals( $_COOKIE[ 'foobar' ], 'pants' ); |
| 352 | + } |
| 353 | + |
| 354 | + /** |
| 355 | + * NB this will not work as PHPUnit seems to not make it possible to set |
| 356 | + * and retrieve cookies. Note above test, testCookie() - both assertions |
| 357 | + * currently fail, making testing ExtMobileFrontend::checkUserFormatCookie() |
| 358 | + * impossible. |
| 359 | + * |
| 360 | + * @outputBuffering enabled |
| 361 | + */ |
| 362 | + /*public function testCheckUseFormatCookie() { |
| 363 | + |
| 364 | + } |
| 365 | + */ |
305 | 366 | } |
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php |
— | — | @@ -1472,11 +1472,25 @@ |
1473 | 1473 | * @param string The format to store in the cookie |
1474 | 1474 | */ |
1475 | 1475 | protected function setUseFormatCookie( $useFormat ) { |
1476 | | - global $wgRequest, $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry; |
1477 | | - $cookieDuration = ( $wgMobileFrontendFormatCookieExpiry ) ? |
| 1476 | + global $wgRequest; |
| 1477 | + $expiry = $this->getUseFormatCookieExpiry(); |
| 1478 | + $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, $expiry ); |
| 1479 | + } |
| 1480 | + |
| 1481 | + /** |
| 1482 | + * Get the expiration time for the mf_useformat cookie |
| 1483 | + * |
| 1484 | + * If $wgMobileFrontendFormatCookieExpiry as a non-0 value, |
| 1485 | + * @param int The base time (in seconds since Epoch) from which to calculate |
| 1486 | + * cookie expiration. If null, time() is used. |
| 1487 | + */ |
| 1488 | + protected function getUseFormatCookieExpiry( $startTime=null ) { |
| 1489 | + global $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry; |
| 1490 | + $cookieDuration = ( abs( intval( $wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ? |
1478 | 1491 | $wgMobileFrontendFormatCookieExpiry : $wgCookieExpiration; |
1479 | | - $expire = time() + $cookieDuration; |
1480 | | - $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, $expire ); |
| 1492 | + if ( intval( $startTime ) === 0 ) $startTime = time(); |
| 1493 | + $expiry = $startTime + $cookieDuration; |
| 1494 | + return $expiry; |
1481 | 1495 | } |
1482 | 1496 | |
1483 | 1497 | public function getVersion() { |