phpunit: Repair GLOBALS reset in MediaWikiUnitTestCase
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 7 Aug 2019 13:40:55 +0000 (14:40 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 2 Sep 2019 19:58:34 +0000 (20:58 +0100)
commit29e0183a56119956ac04fe6ffe2436265fa940d2
treea86162850417ea203646c9c5b8d315daefeb4b62
parent35243c574fa96b447d1683a9cae234ec7958d611
phpunit: Repair GLOBALS reset in MediaWikiUnitTestCase

This code didn't work because the $GLOBALS array is exposed by reference.
Once this reference was broken by unset(), the rest just manipulated a
local array that happens to be called "GLOBALS". It must not be unset or
re-assigned. It can only be changed in-place.

Before this, the execution of a MediaWikiUnitTestCase test stored a
copy of GLOBALS in unitGlobals, then lost the GLOBALS pointer and
created a new variable called "GLOBALS". As such, the tearDown() function
didn't do what it meant to do, either – which then results in odd
failures like T230023

Rewrite it as follows:

* In setup, store the current GLOBALS keys and values, then reduce
  GLOBALS to only the whitelisted keys and values.

* In teardown, restore the original state.

* As optimisation, do this from setUpBeforeClass as well, so that
  there are relatively few globals to reset between tests.
  (Thanks @Simetrical!)

The following tests were previously passing by accident under
MediaWikiUnitTestCase but actually did depend on global config.

* MainSlotRoleHandlerTest (…, ContentHandler, $wgContentHandlers)
* SlotRecordTest (…, ContentHandler, $wgContentHandlers)
* WikiReferenceTest (wfParseUrl, $wgUrlProtocols)
* DifferenceEngineSlotDiffRendererTest (DifferenceEngine, wfDebug, …)
* SlotDiffRendererTest (…, ContentHandler, $wgContentHandlers)
* FileBackendDBRepoWrapperTest (wfWikiID, "Backend domain ID not provided")
* JpegMetadataExtractorTest (…, wfDebug, …, LoggerFactory, …)
* ParserFactoryTest (…, wfDebug, …, LoggerFactory, InvalidArgumentException)
* MediaWikiPageNameNormalizerTest (…, wfDebug, …, LoggerFactory, …)
* SiteExporterTest (SiteImporter, wfLogWarning, …)
* SiteImporterTest (Site::newForType, $wgSiteTypes)
* ZipDirectoryReaderTest (…, wfDebug, …, LoggerFactory, …)

Bug: T230023
Change-Id: Ic22075bb5e81b7c2c4c1b8647547aa55306a10a7
30 files changed:
tests/common/TestSetup.php
tests/phpunit/MediaWikiUnitTestCase.php
tests/phpunit/bootstrap.php
tests/phpunit/includes/Revision/MainSlotRoleHandlerTest.php [new file with mode: 0644]
tests/phpunit/includes/Revision/SlotRecordTest.php [new file with mode: 0644]
tests/phpunit/includes/WikiReferenceTest.php [new file with mode: 0644]
tests/phpunit/includes/diff/DifferenceEngineSlotDiffRendererTest.php [new file with mode: 0644]
tests/phpunit/includes/diff/SlotDiffRendererTest.php [new file with mode: 0644]
tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php [new file with mode: 0644]
tests/phpunit/includes/media/JpegMetadataExtractorTest.php [new file with mode: 0644]
tests/phpunit/includes/parser/ParserFactoryTest.php [new file with mode: 0644]
tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php [new file with mode: 0644]
tests/phpunit/includes/site/SiteExporterTest.php [new file with mode: 0644]
tests/phpunit/includes/site/SiteImporterTest.php [new file with mode: 0644]
tests/phpunit/includes/site/SiteImporterTest.xml [new file with mode: 0644]
tests/phpunit/includes/utils/ZipDirectoryReaderTest.php [new file with mode: 0644]
tests/phpunit/phpunit.php
tests/phpunit/unit/includes/Revision/MainSlotRoleHandlerTest.php [deleted file]
tests/phpunit/unit/includes/Revision/SlotRecordTest.php [deleted file]
tests/phpunit/unit/includes/WikiReferenceTest.php [deleted file]
tests/phpunit/unit/includes/diff/DifferenceEngineSlotDiffRendererTest.php [deleted file]
tests/phpunit/unit/includes/diff/SlotDiffRendererTest.php [deleted file]
tests/phpunit/unit/includes/filerepo/FileBackendDBRepoWrapperTest.php [deleted file]
tests/phpunit/unit/includes/media/JpegMetadataExtractorTest.php [deleted file]
tests/phpunit/unit/includes/parser/ParserFactoryTest.php [deleted file]
tests/phpunit/unit/includes/site/MediaWikiPageNameNormalizerTest.php [deleted file]
tests/phpunit/unit/includes/site/SiteExporterTest.php [deleted file]
tests/phpunit/unit/includes/site/SiteImporterTest.php [deleted file]
tests/phpunit/unit/includes/site/SiteImporterTest.xml [deleted file]
tests/phpunit/unit/includes/utils/ZipDirectoryReaderTest.php [deleted file]