phpunit: Abstract user-lang override in MediaWikiTestCase
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 9 Mar 2016 16:47:58 +0000 (16:47 +0000)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 9 Mar 2016 16:55:50 +0000 (16:55 +0000)
Removed redundant set up in these classes (same as their paren
class MediaWikiLangTestCase does already).
* BlockTest
* ExportTest
* MWTimestampTest
* TitlePermissionTest

Change-Id: I28d18cb797bb249981727b02dffce4f0d8682b02

16 files changed:
tests/phpunit/MediaWikiLangTestCase.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/BlockTest.php
tests/phpunit/includes/ExportTest.php
tests/phpunit/includes/ExtraParserTest.php
tests/phpunit/includes/HtmlTest.php
tests/phpunit/includes/MWTimestampTest.php
tests/phpunit/includes/MessageTest.php
tests/phpunit/includes/TitlePermissionTest.php
tests/phpunit/includes/TitleTest.php
tests/phpunit/includes/cache/MessageCacheTest.php
tests/phpunit/includes/changes/OldChangesListTest.php
tests/phpunit/includes/changes/TestRecentChangesHelper.php
tests/phpunit/includes/exception/MWExceptionTest.php
tests/phpunit/includes/page/ArticleTablesTest.php
tests/phpunit/includes/title/MediaWikiTitleCodecTest.php

index 214bcc1..fd308b2 100644 (file)
@@ -6,7 +6,6 @@
 abstract class MediaWikiLangTestCase extends MediaWikiTestCase {
        protected function setUp() {
                global $wgLanguageCode, $wgContLang;
-               parent::setUp();
 
                if ( $wgLanguageCode != $wgContLang->getCode() ) {
                        throw new MWException( "Error in MediaWikiLangTestCase::setUp(): " .
@@ -14,18 +13,11 @@ abstract class MediaWikiLangTestCase extends MediaWikiTestCase {
                                "\$wgContLang->getCode() (" . $wgContLang->getCode() . ")" );
                }
 
-               // HACK: Call getLanguage() so the real $wgContLang is cached as the user language
-               // rather than our fake one. This is to avoid breaking other, unrelated tests.
-               RequestContext::getMain()->getLanguage();
-
-               $langCode = 'en'; # For mainpage to be 'Main Page'
-               $langObj = Language::factory( $langCode );
+               parent::setUp();
 
-               $this->setMwGlobals( [
-                       'wgLanguageCode' => $langCode,
-                       'wgLang' => $langObj,
-                       'wgContLang' => $langObj,
-               ] );
+               $this->setUserLang( 'en' );
+               // For mainpage to be 'Main Page'
+               $this->setContentLang( 'en' );
 
                MessageCache::singleton()->disable();
        }
index d5192ac..3bd5ed8 100644 (file)
@@ -415,6 +415,33 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->setMwGlobals( $name, $merged );
        }
 
+       /**
+        * @since 1.27
+        * @param string|Language $lang
+        */
+       public function setUserLang( $lang ) {
+               RequestContext::getMain()->setLanguage( $lang );
+               $this->setMwGlobals( 'wgLang', RequestContext::getMain()->getLanguage() );
+       }
+
+       /**
+        * @since 1.27
+        * @param string|Language $lang
+        */
+       public function setContentLang( $lang ) {
+               if ( $lang instanceof Language ) {
+                       $langCode = $lang->getCode();
+                       $langObj = $lang;
+               } else {
+                       $langCode = $lang;
+                       $langObj = Language::factory( $langCode );
+               }
+               $this->setMwGlobals( [
+                       'wgLanguageCode' => $langCode,
+                       'wgContLang' => $langObj,
+               ] );
+       }
+
        /**
         * Sets the logger for a specified channel, for the duration of the test.
         * @since 1.27
index 28192e9..e14960a 100644 (file)
@@ -13,14 +13,6 @@ class BlockTest extends MediaWikiLangTestCase {
        /* variable used to save up the blockID we insert in this test suite */
        private $blockId;
 
-       protected function setUp() {
-               parent::setUp();
-               $this->setMwGlobals( [
-                       'wgLanguageCode' => 'en',
-                       'wgContLang' => Language::factory( 'en' )
-               ] );
-       }
-
        function addDBData() {
 
                $user = User::newFromName( 'UTBlockee' );
index d451e21..a2bb97a 100644 (file)
@@ -12,8 +12,6 @@ class ExportTest extends MediaWikiLangTestCase {
        protected function setUp() {
                parent::setUp();
                $this->setMwGlobals( [
-                       'wgContLang' => Language::factory( 'en' ),
-                       'wgLanguageCode' => 'en',
                        'wgCapitalLinks' => true,
                ] );
        }
index 78e71ea..5dd4f7a 100644 (file)
@@ -18,12 +18,12 @@ class ExtraParserTest extends MediaWikiTestCase {
                $contLang = Language::factory( 'en' );
                $this->setMwGlobals( [
                        'wgShowDBErrorBacktrace' => true,
-                       'wgLanguageCode' => 'en',
-                       'wgContLang' => $contLang,
-                       'wgLang' => Language::factory( 'en' ),
                        'wgCleanSignatures' => true,
                ] );
+               $this->setUserLang( 'en' );
+               $this->setContentLang( $contLang );
 
+               // FIXME: This test should pass without setting global content language
                $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
                $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
                $this->parser = new Parser;
index a18e3c8..a90295a 100644 (file)
@@ -6,8 +6,11 @@ class HtmlTest extends MediaWikiTestCase {
        protected function setUp() {
                parent::setUp();
 
-               $langCode = 'en';
-               $langObj = Language::factory( $langCode );
+               $this->setMwGlobals( [
+                       'wgWellFormedXml' => false,
+               ] );
+
+               $langObj = Language::factory( 'en' );
 
                // Hardcode namespaces during test runs,
                // so that html output based on existing namespaces
@@ -32,13 +35,8 @@ class HtmlTest extends MediaWikiTestCase {
                        100 => 'Custom',
                        101 => 'Custom_talk',
                ] );
-
-               $this->setMwGlobals( [
-                       'wgLanguageCode' => $langCode,
-                       'wgContLang' => $langObj,
-                       'wgLang' => $langObj,
-                       'wgWellFormedXml' => false,
-               ] );
+               $this->setUserLang( $langObj );
+               $this->setContentLang( $langObj );
        }
 
        /**
index 908376b..bca3982 100644 (file)
@@ -10,8 +10,6 @@ class MWTimestampTest extends MediaWikiLangTestCase {
 
                // Avoid 'GetHumanTimestamp' hook and others
                $this->setMwGlobals( 'wgHooks', [] );
-
-               RequestContext::getMain()->setLanguage( Language::factory( 'en' ) );
        }
 
        /**
index bdd80e4..cf34b18 100644 (file)
@@ -6,9 +6,9 @@ class MessageTest extends MediaWikiLangTestCase {
                parent::setUp();
 
                $this->setMwGlobals( [
-                       'wgLang' => Language::factory( 'en' ),
                        'wgForceUIMsgAsContentMsg' => [],
                ] );
+               $this->setUserLang( 'en' );
        }
 
        /**
@@ -517,7 +517,7 @@ class MessageTest extends MediaWikiLangTestCase {
         * @covers Message::inContentLanguage
         */
        public function testInContentLanguage() {
-               $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
+               $this->setUserLang( 'fr' );
 
                // NOTE: make sure internal caching of the message text is reset appropriately
                $msg = wfMessage( 'mainpage' );
@@ -531,9 +531,9 @@ class MessageTest extends MediaWikiLangTestCase {
         */
        public function testInContentLanguageOverride() {
                $this->setMwGlobals( [
-                       'wgLang' => Language::factory( 'fr' ),
                        'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
                ] );
+               $this->setUserLang( 'fr' );
 
                // NOTE: make sure internal caching of the message text is reset appropriately.
                // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
index a4d0059..584a368 100644 (file)
@@ -26,14 +26,10 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
        protected function setUp() {
                parent::setUp();
 
-               $langObj = Language::factory( 'en' );
                $localZone = 'UTC';
                $localOffset = date( 'Z' ) / 60;
 
                $this->setMwGlobals( [
-                       'wgContLang' => $langObj,
-                       'wgLanguageCode' => 'en',
-                       'wgLang' => $langObj,
                        'wgLocaltimezone' => $localZone,
                        'wgLocalTZoffset' => $localOffset,
                        'wgNamespaceProtection' => [
index f2ad1c6..b3465e1 100644 (file)
@@ -9,14 +9,12 @@ class TitleTest extends MediaWikiTestCase {
                parent::setUp();
 
                $this->setMwGlobals( [
-                       'wgLanguageCode' => 'en',
-                       'wgContLang' => Language::factory( 'en' ),
-                       // User language
-                       'wgLang' => Language::factory( 'en' ),
                        'wgAllowUserJs' => false,
                        'wgDefaultLanguageVariant' => false,
                        'wgMetaNamespace' => 'Project',
                ] );
+               $this->setUserLang( 'en' );
+               $this->setContentLang( 'en' );
        }
 
        /**
@@ -421,12 +419,11 @@ class TitleTest extends MediaWikiTestCase {
        ) {
                // Setup environnement for this test
                $this->setMwGlobals( [
-                       'wgLanguageCode' => $contLang,
-                       'wgContLang' => Language::factory( $contLang ),
-                       'wgLang' => Language::factory( $lang ),
                        'wgDefaultLanguageVariant' => $variant,
                        'wgAllowUserJs' => true,
                ] );
+               $this->setUserLang( $lang );
+               $this->setContentLang( $contLang );
 
                $title = Title::newFromText( $titleText );
                $this->assertInstanceOf( 'Title', $title,
index ddd74e3..38cc863 100644 (file)
@@ -19,14 +19,8 @@ class MessageCacheTest extends MediaWikiLangTestCase {
        protected function configureLanguages() {
                // for the test, we need the content language to be anything but English,
                // let's choose e.g. German (de)
-               $langCode = 'de';
-               $langObj = Language::factory( $langCode );
-
-               $this->setMwGlobals( [
-                       'wgLanguageCode' => $langCode,
-                       'wgLang' => $langObj,
-                       'wgContLang' => $langObj,
-               ] );
+               $this->setUserLang( 'de' );
+               $this->setContentLang( 'de' );
        }
 
        function addDBData() {
index 79cc666..5746a61 100644 (file)
@@ -29,8 +29,8 @@ class OldChangesListTest extends MediaWikiLangTestCase {
 
                $this->setMwGlobals( [
                        'wgArticlePath' => '/wiki/$1',
-                       'wgLang' => Language::factory( 'qqx' )
                ] );
+               $this->setUserLang( 'qqx' );
        }
 
        /**
@@ -197,7 +197,7 @@ class OldChangesListTest extends MediaWikiLangTestCase {
        private function getContext() {
                $user = $this->getTestUser();
                $context = $this->testRecentChangesHelper->getTestContext( $user );
-               $context->setLanguage( Language::factory( 'qqx' ) );
+               $context->setLanguage( 'qqx' );
 
                return $context;
        }
index 128e067..faa1dcc 100644 (file)
@@ -154,7 +154,7 @@ class TestRecentChangesHelper {
 
        public function getTestContext( User $user ) {
                $context = new RequestContext();
-               $context->setLanguage( Language::factory( 'en' ) );
+               $context->setLanguage( 'en' );
 
                $context->setUser( $user );
 
index 94c039c..0e87ffa 100644 (file)
@@ -19,9 +19,9 @@ class MWExceptionTest extends MediaWikiTestCase {
         * @dataProvider provideTextUseOutputPage
         * @covers MWException::useOutputPage
         */
-       public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) {
+       public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
                $this->setMwGlobals( [
-                       'wgLang' => $wgLang,
+                       'wgLang' => $langObj,
                        'wgFullyInitialised' => $wgFullyInitialised,
                        'wgOut' => $wgOut,
                ] );
@@ -32,7 +32,7 @@ class MWExceptionTest extends MediaWikiTestCase {
 
        public function provideTextUseOutputPage() {
                return [
-                       // expected, wgLang, wgFullyInitialised, wgOut
+                       // expected, langObj, wgFullyInitialised, wgOut
                        [ false, null, null, null ],
                        [ false, $this->getMockLanguage(), null, null ],
                        [ false, $this->getMockLanguage(), true, null ],
@@ -52,9 +52,9 @@ class MWExceptionTest extends MediaWikiTestCase {
         * @dataProvider provideUseMessageCache
         * @covers MWException::useMessageCache
         */
-       public function testUseMessageCache( $expected, $wgLang ) {
+       public function testUseMessageCache( $expected, $langObj ) {
                $this->setMwGlobals( [
-                       'wgLang' => $wgLang,
+                       'wgLang' => $langObj,
                ] );
                $e = new MWException();
                $this->assertEquals( $expected, $e->useMessageCache() );
index d885926..3a3b514 100644 (file)
@@ -17,9 +17,8 @@ class ArticleTablesTest extends MediaWikiLangTestCase {
                $page = WikiPage::factory( $title );
                $user = new User();
                $user->mRights = [ 'createpage', 'edit', 'purge' ];
-               $this->setMwGlobals( 'wgLanguageCode', 'es' );
-               $this->setMwGlobals( 'wgContLang', Language::factory( 'es' ) );
-               $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
+               $this->setContentLang( 'es' );
+               $this->setUserLang( 'fr' );
 
                $page->doEditContent(
                        new WikitextContent( '{{:{{int:history}}}}' ),
@@ -30,7 +29,7 @@ class ArticleTablesTest extends MediaWikiLangTestCase {
                );
                $templates1 = $title->getTemplateLinksFrom();
 
-               $this->setMwGlobals( 'wgLang', Language::factory( 'de' ) );
+               $this->setUserLang( 'de' );
                $page = WikiPage::factory( $title ); // In order to force the re-rendering of the same wikitext
 
                // We need an edit, a purge is not enough to regenerate the tables
index 1ac5e59..0bb0afe 100644 (file)
@@ -32,10 +32,6 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase {
                parent::setUp();
 
                $this->setMwGlobals( [
-                       'wgLanguageCode' => 'en',
-                       'wgContLang' => Language::factory( 'en' ),
-                       // User language
-                       'wgLang' => Language::factory( 'en' ),
                        'wgAllowUserJs' => false,
                        'wgDefaultLanguageVariant' => false,
                        'wgMetaNamespace' => 'Project',
@@ -57,6 +53,8 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase {
                                ]
                        ]
                ] );
+               $this->setUserLang( 'en' );
+               $this->setContentLang( 'en' );
        }
 
        /**