jenkins-bot has submitted this change and it was merged.

Change subject: Fully test MobileContext#shouldDisplayMobileView
......................................................................


Fully test MobileContext#shouldDisplayMobileView

Changes:
* Remove unnecessary MobileContextTest#setUp.
* Locate suites of test cases for MobileContext methods near one another
  in tests/phpunit/context/.
* Extract MobileContextTest#testIsFauxMobileDevice and
  #testShouldDisplayMobileView to
  MobileContextShouldDisplayMobileViewIntegrationTest and merge them

Bug: T143891
Change-Id: I9512c455e56edecf94737249c4848d6578e8124c
---
M tests/phpunit/MobileContextTest.php
A tests/phpunit/context/MobileContextShouldDisplayMobileViewIntegrationTest.php
R tests/phpunit/context/MobileContextWikibaseDescriptionsTest.php
3 files changed, 131 insertions(+), 77 deletions(-)

Approvals:
  Jdlrobson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/phpunit/MobileContextTest.php 
b/tests/phpunit/MobileContextTest.php
index 15a87e2..7331e6b 100644
--- a/tests/phpunit/MobileContextTest.php
+++ b/tests/phpunit/MobileContextTest.php
@@ -19,10 +19,6 @@
                return $method;
        }
 
-       protected function setUp() {
-               parent::setUp();
-       }
-
        protected function tearDown() {
                parent::tearDown();
 
@@ -265,76 +261,6 @@
                        "http://en.wikipedia.org/wiki/mobile/Gustavus_Airport";,
                        wfAssembleUrl( $parsedUrl )
                );
-       }
-
-       /**
-        * @dataProvider isFauxMobileDeviceProvider
-        * @covers MobileContext::isFauxMobileDevice
-        */
-       public function testIsFauxMobileDevice( $isFauxDevice, $msg, $useformat 
= null ) {
-               $isFauxMobileDevice = self::getMethod( 'isFauxMobileDevice' );
-
-               $testMethod = ( $isFauxDevice ) ? 'assertTrue' : 'assertFalse';
-
-               $context = $this->makeContext();
-               $context->setUseFormat( $useformat );
-               $this->$testMethod(
-                       $isFauxMobileDevice->invokeArgs( $context, [] ),
-                       $msg
-               );
-       }
-
-       public function isFauxMobileDeviceProvider() {
-               return [
-                       [ false, 'Nothing set' ],
-                       [ true, 'useformat=mobile', 'mobile' ],
-                       [ true, 'useformat=mobile-wap', 'mobile-wap' ],
-                       [ false, 'useformat=yourmom', 'yourmom' ],
-               ];
-       }
-
-       /**
-        * @dataProvider shouldDisplayMobileViewProvider
-        * @covers MobileContext::shouldDisplayMobileView
-        */
-       public function testShouldDisplayMobileView( $shouldDisplay, $xWap = 
null,
-               $requestVal = [], $msg = null
-       ) {
-               $testMethod = ( $shouldDisplay ) ? 'assertTrue' : 'assertFalse';
-
-               $this->setMwGlobals( [
-                       'wgMFMobileHeader' => 'X-WAP',
-                       'wgMobileUrlTemplate' => '%h0.m.%h1.%h2',
-               ] );
-               $context = $this->makeContext();
-               $request = $context->getRequest();
-               if ( count( $requestVal ) ) {
-                       foreach ( $requestVal as $key => $val ) {
-                               if ( $key == 'useformat' ) {
-                                       $context->setUseFormat( $val );
-                               } else {
-                                       $request->setVal( $key, $val );
-                               }
-                       }
-               }
-
-               if ( !is_null( $xWap ) ) {
-                       $request->setHeader( 'X-WAP', $xWap );
-               }
-
-               $this->$testMethod( $context->shouldDisplayMobileView(), $msg );
-       }
-
-       public function shouldDisplayMobileViewProvider() {
-               return [
-                       [ false, null, [] ],
-                       [ true, 'yes', [] ],
-                       [ true, 'no', [] ],
-                       [ false, 'yes', [ 'useformat' => 'desktop' ] ],
-                       [ true, null, [ 'useformat' => 'mobile-wap' ] ],
-                       [ false, null, [ 'useformat' => 'desktop' ] ],
-                       [ true, null, [ 'useformat' => 'mobile' ] ],
-               ];
        }
 
        /**
diff --git 
a/tests/phpunit/context/MobileContextShouldDisplayMobileViewIntegrationTest.php 
b/tests/phpunit/context/MobileContextShouldDisplayMobileViewIntegrationTest.php
new file mode 100644
index 0000000..e844fe5
--- /dev/null
+++ 
b/tests/phpunit/context/MobileContextShouldDisplayMobileViewIntegrationTest.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Tests\MobileFrontend\Context;
+
+use MediaWikiTestCase;
+use MobileContext;
+
+/**
+ * This suite of tests cases tests the behaviour of
+ * `MobileContext#shouldDisplayMobileView` and 
`#shoudDisplayMobileViewInternal`
+ * with no stubbed dependencies.
+ *
+ * @group integration
+ */
+class MobileContextShouldDisplayMobileViewIntegrationTest extends 
MediaWikiTestCase {
+
+       /**
+        * @var MobileContext
+        */
+       private $context;
+
+       protected function setUp() {
+               parent::setUp();
+
+               MobileContext::resetInstanceForTesting();
+               $this->context = MobileContext::singleton();
+       }
+
+       /**
+        * @covers MobileContext::shouldDisplayMobileView
+        */
+       public function test_it_can_be_overridden() {
+               $this->context->setForceMobileView( true );
+
+               $this->assertTrue( $this->context->shouldDisplayMobileView() );
+       }
+
+       /**
+        * @dataProvider shouldDisplayMobileViewProvider
+        * @covers MobileContext::shouldDisplayMobileView
+        */
+       public function testShouldDisplayMobileView(
+               $expected,
+               $customHeader,
+               $format,
+               $formatCookie = null,
+               $stopMobileRedirectCookie = null,
+               $isMobileUA = false
+       ) {
+               $this->setMwGlobals( [
+                       'wgMFAutodetectMobileView' => true,
+                       'wgMFMobileHeader' => 'X-Subdomain',
+                       'wgMobileUrlTemplate' => '%h0.m.%h1.%h2',
+               ] );
+
+               $request = $this->context->getRequest();
+
+               if ( $customHeader !== null ) {
+                       $request->setHeader( 'X-Subdomain', $customHeader );
+               }
+
+               if ( $format !== null ) {
+                       $this->context->setUseFormat( $format );
+               }
+
+               if ( $formatCookie !== null ) {
+
+                       // N.B. that the format and the "stop mobile redirect" 
cookies
+                       // ("mf_useformat" and "stopMobileRedirect" 
respectively) aren't prefix
+                       // with MediaWiki's cookie prefix ($wgCookiePrefix).
+                       $request->setCookie( 'mf_useformat', $formatCookie, '' 
);
+               }
+
+               if ( $stopMobileRedirectCookie !== null ) {
+                       $request->setCookie( 'stopMobileRedirect', 
$stopMobileRedirectCookie, '' );
+               }
+
+               if ( $isMobileUA ) {
+                       $request->setHeader(
+                               'User-Agent',
+
+                               // An iPhone running iOS 8.0.
+                               // @codingStandardsIgnoreStart
+                               'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like 
Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e 
Safari/8536.25'
+                               // @codingStandardsIgnoreEnd
+                       );
+               }
+
+               $this->assertEquals( $expected, 
$this->context->shouldDisplayMobileView() );
+       }
+
+       public function shouldDisplayMobileViewProvider() {
+               return [
+
+                       // By default, the mobile view shouldn't be displayed.
+                       [ false, null, null ],
+
+                       // When the custom header (by default, "X-Subdomain") 
is set, then the
+                       // mobile view should be displayed.
+                       [ true, 'M', null ],
+
+                       // The format (the useformat=<$format> query parameter 
in the URL)
+                       // overrides the custom header.
+                       [ false, 'M', 'desktop' ],
+
+                       // If the format is either "mobile-wap" or "mobile", 
then the mobile view
+                       // should be displayed.
+                       [ true, null, 'mobile-wap' ],
+                       [ true, null, 'mobile' ],
+                       [ false, null, 'foo' ],
+
+                       // If the format cookie ("mf_useformat") is "true", 
then the mobile view
+                       // should be displayed.
+                       [ true, null, null, 'true' ],
+                       [ false, null, null, 'bar' ],
+
+                       // The custom header overrides the "stop mobile 
redirect" cookie
+                       // ("stopMobileRedirect").
+                       [ true, 'M', null, null, 'true' ],
+
+                       // When the request is sent from a mobile UA, then the 
mobile view should
+                       // be displayed.
+                       [ true, null, null, null, null, true ],
+
+                       // stopMobileRedirect overrides device detection.
+                       [ false, null, null, null, 'true', true ],
+               ];
+       }
+}
diff --git a/tests/phpunit/MobileContextWikibaseDescriptionsTest.php 
b/tests/phpunit/context/MobileContextWikibaseDescriptionsTest.php
similarity index 98%
rename from tests/phpunit/MobileContextWikibaseDescriptionsTest.php
rename to tests/phpunit/context/MobileContextWikibaseDescriptionsTest.php
index bd00754..b5f85d2 100644
--- a/tests/phpunit/MobileContextWikibaseDescriptionsTest.php
+++ b/tests/phpunit/context/MobileContextWikibaseDescriptionsTest.php
@@ -1,8 +1,7 @@
 <?php
 
-/**
- * @group MobileFrontend
- */
+use Tests\MobileFrontend\Context;
+
 class MobileContextWikibaseDescriptionsTest extends MediaWikiTestCase {
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/315499
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I9512c455e56edecf94737249c4848d6578e8124c
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Phuedx <g...@samsmith.io>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to