https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113887

Revision: 113887
Author:   awjrichards
Date:     2012-03-15 01:43:17 +0000 (Thu, 15 Mar 2012)
Log Message:
-----------
* 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
    trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php

Modified: trunk/extensions/MobileFrontend/MobileFrontend.body.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-15 
01:42:34 UTC (rev 113886)
+++ trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-15 
01:43:17 UTC (rev 113887)
@@ -1472,11 +1472,25 @@
         * @param string The format to store in the cookie
         */
        protected function setUseFormatCookie( $useFormat ) {
-               global $wgRequest, $wgCookieExpiration, 
$wgMobileFrontendFormatCookieExpiry;
-               $cookieDuration = ( $wgMobileFrontendFormatCookieExpiry ) ? 
+               global $wgRequest;
+               $expiry = $this->getUseFormatCookieExpiry();
+               $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, 
$expiry );
+       }
+       
+       /**
+        * Get the expiration time for the mf_useformat cookie
+        *
+        * If $wgMobileFrontendFormatCookieExpiry as a non-0 value, 
+        * @param int The base time (in seconds since Epoch) from which to 
calculate
+        *              cookie expiration. If null, time() is used.
+        */
+       protected function getUseFormatCookieExpiry( $startTime=null ) {
+               global $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry;
+               $cookieDuration = ( abs( intval( 
$wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ? 
                                $wgMobileFrontendFormatCookieExpiry : 
$wgCookieExpiration;
-               $expire = time() + $cookieDuration;
-               $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, 
$expire );
+               if ( intval( $startTime ) === 0 ) $startTime = time();
+               $expiry = $startTime + $cookieDuration;
+               return $expiry;
        }
        
        public function getVersion() {

Modified: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
===================================================================
--- trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php        
2012-03-15 01:42:34 UTC (rev 113886)
+++ trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php        
2012-03-15 01:43:17 UTC (rev 113887)
@@ -301,4 +301,65 @@
                        array( 'edit' ),
                );
        }
+       
+       /**
+        * @dataProvider getUseFormatProvider
+        */
+       public function testGetUseFormat( $explicit, $requestParam, $expected ) 
{
+               global $wgRequest, $wgExtMobileFrontend;
+               $wgRequest->setVal( 'useformat', $requestParam );
+               $wgExtMobileFrontend->setUseFormat( $explicit );
+               $this->assertEquals( $expected, 
$wgExtMobileFrontend->getUseFormat() );
+       }
+       
+       public function getUseFormatProvider() {
+               return array(
+                       array( 'mobile', null, 'mobile' ),
+                       array( null, 'mobile', 'mobile' ),
+                       array( null, null, '' ),
+                       array( 'desktop', 'mobile', 'desktop' ),
+               );
+       }
+       
+       public function testGetUseFormatCookieExpiry() {
+               global $wgExtMobileFrontend, $wgCookieExpiration, 
$wgMobileFrontendFormatCookieExpiry;
+               $getUseFormatCookieExpiry = self::getMethod( 
'getUseFormatCookieExpiry' );
+               
+               $origMFCookieExpiry = $wgMobileFrontendFormatCookieExpiry;
+               $startTime = time();
+               $wgMobileFrontendFormatCookieExpiry = 60;
+               $mfCookieExpected = $startTime + 60;
+               $this->assertTrue( $mfCookieExpected == 
$getUseFormatCookieExpiry->invokeArgs( $wgExtMobileFrontend, array( $startTime 
) ), 'Using MobileFrontend expiry.' );
+               
+               $wgMobileFrontendFormatCookieExpiry = null;
+               $defaultMWCookieExpected = $startTime + $wgCookieExpiration;
+               $this->assertTrue( $defaultMWCookieExpected == 
$getUseFormatCookieExpiry->invokeArgs( $wgExtMobileFrontend, array( $startTime 
) ), 'Using default MediaWiki cookie expiry.' );
+
+               // reset global back to original value
+               $wgMobileFrontendFormatCookieExpiry = $origMFCookieExpiry;
+       }
+       
+       /**
+     * @outputBuffering enabled
+        */
+       /*public function testCookie() {
+               global $wgRequest;
+               $wgRequest->response()->setCookie( 'foo', 'bar' );
+               $this->assertEquals( $wgRequest->getCookie( 'foo' ), 'bar' );
+               setcookie( 'foobar', 'pants' );
+               $this->asertEquals( $_COOKIE[ 'foobar' ], 'pants' );
+       }
+       
+       /**
+        * NB this will not work as PHPUnit seems to not make it possible to set
+        * and retrieve cookies. Note above test, testCookie() - both assertions
+        * currently fail, making testing 
ExtMobileFrontend::checkUserFormatCookie()
+        * impossible.
+        *
+     * @outputBuffering enabled
+        */
+       /*public function testCheckUseFormatCookie() {
+       
+       }
+       */
 }


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to