From ff3485ec99de12c6328e2b73c8c3e2026ecf772a Mon Sep 17 00:00:00 2001 From: umherirrender Date: Thu, 21 Mar 2013 20:35:44 +0100 Subject: [PATCH] Tests: Use more setMwGlobals Change some tests to use setMwGlobals to have restoring of globals after the test. This also removes some save/restore code, which is not needed, due to the automatically restoring on tearDown with setMwGlobals. Change-Id: I8d2ac9f6cc14f0bd4ee8eb851c09f2e71babc6e0 --- tests/phpunit/includes/ExtraParserTest.php | 3 +-- .../GlobalFunctions/wfExpandUrlTest.php | 13 +++------ tests/phpunit/includes/HtmlTest.php | 14 +++------- ...evisionStorageTest_ContentHandlerUseDB.php | 15 +---------- tests/phpunit/includes/RevisionTest.php | 4 +-- .../includes/SeleniumConfigurationTest.php | 8 +++--- tests/phpunit/includes/WebRequestTest.php | 8 +++--- .../WikiPageTest_ContentHandlerUseDB.php | 16 +---------- .../includes/content/TextContentTest.php | 6 +---- .../includes/jobqueue/JobQueueTest.php | 10 +++---- .../media/BitmapMetadataHandlerTest.php | 4 +-- .../includes/media/ExifRotationTest.php | 27 +++++-------------- .../includes/upload/UploadFromUrlTest.php | 9 ++++--- .../includes/upload/UploadStashTest.php | 3 +-- tests/phpunit/maintenance/DumpTestCase.php | 4 +-- 15 files changed, 40 insertions(+), 104 deletions(-) diff --git a/tests/phpunit/includes/ExtraParserTest.php b/tests/phpunit/includes/ExtraParserTest.php index e0e5535ac6..c8400568fb 100644 --- a/tests/phpunit/includes/ExtraParserTest.php +++ b/tests/phpunit/includes/ExtraParserTest.php @@ -87,8 +87,7 @@ class ExtraParserTest extends MediaWikiTestCase { * cleanSig() should do nothing if disabled */ function testCleanSigDisabled() { - global $wgCleanSignatures; - $wgCleanSignatures = false; + $this->setMwGlobals( 'wgCleanSignatures', false ); $title = Title::newFromText( __FUNCTION__ ); $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); diff --git a/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php b/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php index c1225e3e06..8c67ced402 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php @@ -6,11 +6,10 @@ class WfExpandUrlTest extends MediaWikiTestCase { /** @dataProvider provideExpandableUrls */ public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) { // Fake $wgServer and $wgCanonicalServer - global $wgServer, $wgCanonicalServer; - $oldServer = $wgServer; - $oldCanServer = $wgCanonicalServer; - $wgServer = $server; - $wgCanonicalServer = $canServer; + $this->setMwGlobals( array( + 'wgServer' => $server, + 'wgCanonicalServer' => $canServer, + ) ); // Fake $_SERVER['HTTPS'] if needed if ( $httpsMode ) { @@ -20,10 +19,6 @@ class WfExpandUrlTest extends MediaWikiTestCase { } $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message ); - - // Restore $wgServer and $wgCanonicalServer - $wgServer = $oldServer; - $wgCanonicalServer = $oldCanServer; } /** diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index 590664e89b..554a6d008b 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -43,8 +43,6 @@ class HtmlTest extends MediaWikiTestCase { } public function testElementBasics() { - global $wgWellFormedXml; - $this->assertEquals( '', Html::element( 'img', null, '' ), @@ -63,7 +61,7 @@ class HtmlTest extends MediaWikiTestCase { 'Close tag for empty element (array, string)' ); - $wgWellFormedXml = true; + $this->setMwGlobals( 'wgWellFormedXml', true ); $this->assertEquals( '', @@ -90,8 +88,6 @@ class HtmlTest extends MediaWikiTestCase { } public function testExpandAttributesForBooleans() { - global $wgHtml5, $wgWellFormedXml; - $this->assertEquals( '', Html::expandAttributes( array( 'selected' => false ) ), @@ -114,7 +110,7 @@ class HtmlTest extends MediaWikiTestCase { 'Boolean attributes have no value when value is true (passed as numerical array)' ); - $wgWellFormedXml = true; + $this->setMwGlobals( 'wgWellFormedXml', true ); $this->assertEquals( ' selected=""', @@ -122,7 +118,7 @@ class HtmlTest extends MediaWikiTestCase { 'Boolean attributes have empty string value when value is true (wgWellFormedXml)' ); - $wgHtml5 = false; + $this->setMwGlobals( 'wgHtml5', false ); $this->assertEquals( ' selected="selected"', @@ -136,8 +132,6 @@ class HtmlTest extends MediaWikiTestCase { * Please note it output a string prefixed with a space! */ public function testExpandAttributesVariousExpansions() { - global $wgWellFormedXml; - ### NOT EMPTY #### $this->assertEquals( ' empty_string=""', @@ -160,7 +154,7 @@ class HtmlTest extends MediaWikiTestCase { 'Number 0 value needs no quotes' ); - $wgWellFormedXml = true; + $this->setMwGlobals( 'wgWellFormedXml', true ); $this->assertEquals( ' empty_string=""', diff --git a/tests/phpunit/includes/RevisionStorageTest_ContentHandlerUseDB.php b/tests/phpunit/includes/RevisionStorageTest_ContentHandlerUseDB.php index 3948e34595..39673c0318 100644 --- a/tests/phpunit/includes/RevisionStorageTest_ContentHandlerUseDB.php +++ b/tests/phpunit/includes/RevisionStorageTest_ContentHandlerUseDB.php @@ -6,14 +6,9 @@ * ^--- important, causes temporary tables to be used instead of the real database */ class RevisionTest_ContentHandlerUseDB extends RevisionStorageTest { - var $saveContentHandlerNoDB = null; function setUp() { - global $wgContentHandlerUseDB; - - $this->saveContentHandlerNoDB = $wgContentHandlerUseDB; - - $wgContentHandlerUseDB = false; + $this->setMwGlobals( 'wgContentHandlerUseDB', false ); $dbw = wfGetDB( DB_MASTER ); @@ -32,14 +27,6 @@ class RevisionTest_ContentHandlerUseDB extends RevisionStorageTest { parent::setUp(); } - function tearDown() { - global $wgContentHandlerUseDB; - - parent::tearDown(); - - $wgContentHandlerUseDB = $this->saveContentHandlerNoDB; - } - /** * @covers Revision::selectFields */ diff --git a/tests/phpunit/includes/RevisionTest.php b/tests/phpunit/includes/RevisionTest.php index db0245b959..9380928d6e 100644 --- a/tests/phpunit/includes/RevisionTest.php +++ b/tests/phpunit/includes/RevisionTest.php @@ -134,9 +134,7 @@ class RevisionTest extends MediaWikiTestCase { function testCompressRevisionTextUtf8Gzip() { $this->checkPHPExtension( 'zlib' ); - - global $wgCompressRevisions; - $wgCompressRevisions = true; + $this->setMwGlobals( 'wgCompressRevisions', true ); $row = new stdClass; $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; diff --git a/tests/phpunit/includes/SeleniumConfigurationTest.php b/tests/phpunit/includes/SeleniumConfigurationTest.php index 3422c90c42..4b49f639e8 100644 --- a/tests/phpunit/includes/SeleniumConfigurationTest.php +++ b/tests/phpunit/includes/SeleniumConfigurationTest.php @@ -138,8 +138,8 @@ testBrowser = "firefox" $seleniumSettings = array(); $seleniumBrowsers = array(); $seleniumTestSuites = array(); - global $wgSeleniumConfigFile; - $wgSeleniumConfigFile = ''; + $this->setMwGlobals( 'wgSeleniumConfigFile', '' ); + SeleniumConfig::getSeleniumSettings( $seleniumSettings, $seleniumBrowsers, $seleniumTestSuites ); @@ -152,9 +152,9 @@ testBrowser = "firefox" $seleniumSettings = array(); $seleniumBrowsers = array(); $seleniumTestSuites = array(); - global $wgSeleniumConfigFile; $this->writeToTempFile( $this->testConfig0 ); - $wgSeleniumConfigFile = $this->tempFileName; + $this->setMwGlobals( 'wgSeleniumConfigFile', $this->tempFileName ); + SeleniumConfig::getSeleniumSettings( $seleniumSettings, $seleniumBrowsers, $seleniumTestSuites ); diff --git a/tests/phpunit/includes/WebRequestTest.php b/tests/phpunit/includes/WebRequestTest.php index 46f802553e..d382f6f57c 100644 --- a/tests/phpunit/includes/WebRequestTest.php +++ b/tests/phpunit/includes/WebRequestTest.php @@ -102,10 +102,12 @@ class WebRequestTest extends MediaWikiTestCase { * @dataProvider provideGetIP */ function testGetIP( $expected, $input, $squid, $private, $description ) { - global $wgSquidServersNoPurge, $wgUsePrivateIPs; $_SERVER = $input; - $wgSquidServersNoPurge = $squid; - $wgUsePrivateIPs = $private; + $this->setMwGlobals( array( + 'wgSquidServersNoPurge' => $squid, + 'wgUsePrivateIPs' => $private, + ) ); + $request = new WebRequest(); $result = $request->getIP(); $this->assertEquals( $expected, $result, $description ); diff --git a/tests/phpunit/includes/WikiPageTest_ContentHandlerUseDB.php b/tests/phpunit/includes/WikiPageTest_ContentHandlerUseDB.php index 1d937e9ba4..dca9910a35 100644 --- a/tests/phpunit/includes/WikiPageTest_ContentHandlerUseDB.php +++ b/tests/phpunit/includes/WikiPageTest_ContentHandlerUseDB.php @@ -6,16 +6,10 @@ * ^--- important, causes temporary tables to be used instead of the real database */ class WikiPageTest_ContentHandlerUseDB extends WikiPageTest { - var $saveContentHandlerNoDB = null; function setUp() { - global $wgContentHandlerUseDB; - parent::setUp(); - - $this->saveContentHandlerNoDB = $wgContentHandlerUseDB; - - $wgContentHandlerUseDB = false; + $this->setMwGlobals( 'wgContentHandlerUseDB', false ); $dbw = wfGetDB( DB_MASTER ); @@ -32,14 +26,6 @@ class WikiPageTest_ContentHandlerUseDB extends WikiPageTest { } } - function tearDown() { - global $wgContentHandlerUseDB; - - $wgContentHandlerUseDB = $this->saveContentHandlerNoDB; - - parent::tearDown(); - } - public function testGetContentModel() { $page = $this->createPage( "WikiPageTest_testGetContentModel", "some text", CONTENT_MODEL_JAVASCRIPT ); diff --git a/tests/phpunit/includes/content/TextContentTest.php b/tests/phpunit/includes/content/TextContentTest.php index 382f71a866..4fc2d51d03 100644 --- a/tests/phpunit/includes/content/TextContentTest.php +++ b/tests/phpunit/includes/content/TextContentTest.php @@ -211,15 +211,11 @@ class TextContentTest extends MediaWikiLangTestCase { * @group Database */ public function testIsCountable( $text, $hasLinks, $mode, $expected ) { - global $wgArticleCountMethod; - - $old = $wgArticleCountMethod; - $wgArticleCountMethod = $mode; + $this->setMwGlobals( 'wgArticleCountMethod', $mode ); $content = $this->newContent( $text ); $v = $content->isCountable( $hasLinks, $this->context->getTitle() ); - $wgArticleCountMethod = $old; $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true ) . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" ); diff --git a/tests/phpunit/includes/jobqueue/JobQueueTest.php b/tests/phpunit/includes/jobqueue/JobQueueTest.php index f3f9c11e12..31d7a469e7 100644 --- a/tests/phpunit/includes/jobqueue/JobQueueTest.php +++ b/tests/phpunit/includes/jobqueue/JobQueueTest.php @@ -8,7 +8,6 @@ class JobQueueTest extends MediaWikiTestCase { protected $key; protected $queueRand, $queueRandTTL, $queueFifo, $queueFifoTTL; - protected $old = array(); function __construct( $name = null, array $data = array(), $dataName = '' ) { parent::__construct( $name, $data, $dataName ); @@ -17,10 +16,11 @@ class JobQueueTest extends MediaWikiTestCase { } protected function setUp() { - global $wgMemc, $wgJobTypeConf; + global $wgJobTypeConf; parent::setUp(); - $this->old['wgMemc'] = $wgMemc; - $wgMemc = new HashBagOStuff(); + + $this->setMwGlobals( 'wgMemc', new HashBagOStuff() ); + if ( $this->getCliArg( 'use-jobqueue=' ) ) { $name = $this->getCliArg( 'use-jobqueue=' ); if ( !isset( $wgJobTypeConf[$name] ) ) { @@ -51,7 +51,6 @@ class JobQueueTest extends MediaWikiTestCase { } protected function tearDown() { - global $wgMemc; parent::tearDown(); foreach ( array( 'queueRand', 'queueRandTTL', 'queueTimestamp', 'queueTimestampTTL', @@ -67,7 +66,6 @@ class JobQueueTest extends MediaWikiTestCase { } $this->$q = null; } - $wgMemc = $this->old['wgMemc']; } /** diff --git a/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php b/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php index b221b8325b..117a072cd7 100644 --- a/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php +++ b/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php @@ -18,8 +18,6 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { * translation (to en) where XMP should win. */ public function testMultilingualCascade() { - global $wgShowEXIF; - if ( !wfDl( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } @@ -27,7 +25,7 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { $this->markTestSkipped( "This test needs the xml extension." ); } - $wgShowEXIF = true; + $this->setMwGlobals( 'wgShowEXIF', true ); $meta = BitmapMetadataHandler::Jpeg( $this->filePath . '/Xmp-exif-multilingual_test.jpg' ); diff --git a/tests/phpunit/includes/media/ExifRotationTest.php b/tests/phpunit/includes/media/ExifRotationTest.php index db29d17c0c..d9d7a688c0 100644 --- a/tests/phpunit/includes/media/ExifRotationTest.php +++ b/tests/phpunit/includes/media/ExifRotationTest.php @@ -25,21 +25,11 @@ class ExifRotationTest extends MediaWikiTestCase { if ( !wfDl( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - global $wgShowEXIF; - $this->show = $wgShowEXIF; - $wgShowEXIF = true; - global $wgEnableAutoRotation; - $this->oldAuto = $wgEnableAutoRotation; - $wgEnableAutoRotation = true; - } - - protected function tearDown() { - global $wgShowEXIF, $wgEnableAutoRotation; - $wgShowEXIF = $this->show; - $wgEnableAutoRotation = $this->oldAuto; - - parent::tearDown(); + $this->setMwGlobals( array( + 'wgShowEXIF' => true, + 'wgEnableAutoRotation' => true, + ) ); } /** @@ -139,14 +129,11 @@ class ExifRotationTest extends MediaWikiTestCase { * @dataProvider provideFilesNoAutoRotate */ function testMetadataNoAutoRotate( $name, $type, $info ) { - global $wgEnableAutoRotation; - $wgEnableAutoRotation = false; + $this->setMwGlobals( 'wgEnableAutoRotation', false ); $file = $this->dataFile( $name, $type ); $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); - - $wgEnableAutoRotation = true; } /** @@ -154,8 +141,7 @@ class ExifRotationTest extends MediaWikiTestCase { * @dataProvider provideFilesNoAutoRotate */ function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) { - global $wgEnableAutoRotation; - $wgEnableAutoRotation = false; + $this->setMwGlobals( 'wgEnableAutoRotation', false ); foreach ( $thumbs as $size => $out ) { if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { @@ -187,7 +173,6 @@ class ExifRotationTest extends MediaWikiTestCase { $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); } } - $wgEnableAutoRotation = true; } public static function provideFilesNoAutoRotate() { diff --git a/tests/phpunit/includes/upload/UploadFromUrlTest.php b/tests/phpunit/includes/upload/UploadFromUrlTest.php index 4d2d8ce374..ac93aa721a 100644 --- a/tests/phpunit/includes/upload/UploadFromUrlTest.php +++ b/tests/phpunit/includes/upload/UploadFromUrlTest.php @@ -8,12 +8,13 @@ class UploadFromUrlTest extends ApiTestCase { protected function setUp() { - global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads; parent::setUp(); - $wgEnableUploads = true; - $wgAllowCopyUploads = true; - $wgAllowAsyncCopyUploads = true; + $this->setMwGlobals( array( + 'wgEnableUploads' => true, + 'wgAllowCopyUploads' => true, + 'wgAllowAsyncCopyUploads' => true, + ) ); wfSetupSession(); if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) { diff --git a/tests/phpunit/includes/upload/UploadStashTest.php b/tests/phpunit/includes/upload/UploadStashTest.php index 8fcaa21423..7a0fea48fc 100644 --- a/tests/phpunit/includes/upload/UploadStashTest.php +++ b/tests/phpunit/includes/upload/UploadStashTest.php @@ -44,8 +44,7 @@ class UploadStashTest extends MediaWikiTestCase { } public function testBug29408() { - global $wgUser; - $wgUser = self::$users['uploader']->user; + $this->setMwGlobals( 'wgUser', self::$users['uploader']->user ); $repo = RepoGroup::singleton()->getLocalRepo(); $stash = new UploadStash( $repo ); diff --git a/tests/phpunit/maintenance/DumpTestCase.php b/tests/phpunit/maintenance/DumpTestCase.php index 40d24fc518..f82898ffad 100644 --- a/tests/phpunit/maintenance/DumpTestCase.php +++ b/tests/phpunit/maintenance/DumpTestCase.php @@ -73,8 +73,6 @@ abstract class DumpTestCase extends MediaWikiLangTestCase { * Clears $wgUser, and reports errors from addDBData to PHPUnit */ protected function setUp() { - global $wgUser; - parent::setUp(); // Check if any Exception is stored for rethrowing from addDBData @@ -83,7 +81,7 @@ abstract class DumpTestCase extends MediaWikiLangTestCase { throw $this->exceptionFromAddDBData; } - $wgUser = new User(); + $this->setMwGlobals( 'wgUser', new User() ); } /** -- 2.20.1