r113887 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113886‎ | r113887 | r113888 >
Date:01:43, 15 March 2012
Author:awjrichards
Status:ok (Comments)
Tags:
Comment:
* Abstracted cookie expiration time handling to its own method
* Added unit tests for cookie expiration time handling method as well as getUseFormat()
* Wanted to add further unit tests around cookie handling, but I cannot get PHPUnit playing nicely with cookies. At first it was causing header issues until prielly suggested disabling output buffereing for the test method, which quieted the errors. However, setcookie() seems to have actual effect in the tests (see commented out tests in this commit). Open to suggestions :)
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
@@ -301,4 +301,65 @@
302302 array( 'edit' ),
303303 );
304304 }
 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+ */
305366 }
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -1472,11 +1472,25 @@
14731473 * @param string The format to store in the cookie
14741474 */
14751475 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 ) ?
14781491 $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;
14811495 }
14821496
14831497 public function getVersion() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r114202MFT r113807, r113831, r113832, r113865, r113866, r113870, r113871, r113872, r...awjrichards22:23, 19 March 2012

Comments

#Comment by Awjrichards (talk | contribs)   01:45, 15 March 2012

Wow, typing fail in the commit message.

Wanted to add further unit tests around cookie handling, but I cannot get PHPUnit playing nicely with cookies. At first it was causing header issues until prielly suggested disabling output buffereing for the test method, which quieted the errors. However, setcookie() seems to have actual effect in the tests (see commented out tests in this commit). Open to suggestions :)

Should read:

Wanted to add further unit tests around cookie handling, but I cannot get PHPUnit playing nicely with cookies. At first it was causing header issues until preilly suggested disabling output buffering for the test method, which quieted the errors. However, setcookie() seems to have NO actual effect in the tests (see commented out tests in this commit). Open to suggestions :)

Status & tagging log