2019-06-17 21:15:25 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Testing
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-13 08:52:13 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2019-09-05 07:14:28 +00:00
|
|
|
use PHPUnit\Framework\Exception;
|
2020-01-10 00:00:51 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-08-13 08:52:13 +00:00
|
|
|
use Wikimedia\ObjectFactory;
|
2019-06-17 21:15:25 +00:00
|
|
|
|
2019-06-26 02:33:14 +00:00
|
|
|
/**
|
|
|
|
|
* Base class for unit tests.
|
|
|
|
|
*
|
|
|
|
|
* Extend this class if you are testing classes which use dependency injection and do not access
|
|
|
|
|
* global functions, variables, services or a storage backend.
|
2019-08-18 18:19:05 +00:00
|
|
|
*
|
2020-03-12 11:13:22 +00:00
|
|
|
* @stable for subclassing
|
2019-08-18 18:19:05 +00:00
|
|
|
* @since 1.34
|
2019-06-26 02:33:14 +00:00
|
|
|
*/
|
2019-06-17 21:15:25 +00:00
|
|
|
abstract class MediaWikiUnitTestCase extends TestCase {
|
|
|
|
|
use MediaWikiCoversValidator;
|
2019-08-18 18:19:05 +00:00
|
|
|
use MediaWikiTestCaseTrait;
|
2019-06-30 13:23:53 +00:00
|
|
|
|
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
2019-08-07 13:40:55 +00:00
|
|
|
private static $originalGlobals;
|
|
|
|
|
private static $unitGlobals;
|
2019-07-08 13:25:31 +00:00
|
|
|
|
2019-09-06 09:43:39 +00:00
|
|
|
/**
|
2021-03-29 04:21:28 +00:00
|
|
|
* List of allowed globals to allow in MediaWikiUnitTestCase.
|
2019-09-06 09:43:39 +00:00
|
|
|
*
|
|
|
|
|
* Please, keep this list to the bare minimum.
|
|
|
|
|
*
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2021-03-29 04:21:28 +00:00
|
|
|
private static function getAllowedGlobalsList() {
|
2019-09-06 09:43:39 +00:00
|
|
|
return [
|
|
|
|
|
// The autoloader may change between bootstrap and the first test,
|
|
|
|
|
// so (lazily) capture these here instead.
|
|
|
|
|
'wgAutoloadClasses',
|
|
|
|
|
'wgAutoloadLocalClasses',
|
|
|
|
|
// Need for LoggerFactory. Default is NullSpi.
|
|
|
|
|
'wgMWLoggerDefaultSpi',
|
2019-05-02 14:23:42 +00:00
|
|
|
'wgAutoloadAttemptLowercase',
|
2019-10-29 11:44:14 +00:00
|
|
|
'wgLegalTitleChars',
|
|
|
|
|
'wgDevelopmentWarnings',
|
2020-08-22 19:26:19 +00:00
|
|
|
// Dependency of wfParseUrl()
|
|
|
|
|
'wgUrlProtocols',
|
2019-09-06 09:43:39 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 11:13:22 +00:00
|
|
|
/**
|
2021-04-27 19:33:14 +00:00
|
|
|
* The annotation causes this to be called immediately before setUpBeforeClass()
|
|
|
|
|
* @beforeClass
|
2020-03-12 11:13:22 +00:00
|
|
|
*/
|
2021-04-27 19:33:14 +00:00
|
|
|
final public static function mediaWikiSetUpBeforeClass() : void {
|
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
2019-08-07 13:40:55 +00:00
|
|
|
$reflection = new ReflectionClass( static::class );
|
2019-07-17 17:52:18 +00:00
|
|
|
$dirSeparator = DIRECTORY_SEPARATOR;
|
2020-05-26 13:14:46 +00:00
|
|
|
if ( stripos( $reflection->getFileName(), "${dirSeparator}unit${dirSeparator}" ) === false ) {
|
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
2019-08-07 13:40:55 +00:00
|
|
|
self::fail( 'This unit test needs to be in "tests/phpunit/unit"!' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$unitGlobals =& TestSetup::$bootstrapGlobals;
|
2019-09-06 09:43:39 +00:00
|
|
|
|
2021-03-29 04:21:28 +00:00
|
|
|
foreach ( self::getAllowedGlobalsList() as $global ) {
|
2019-09-06 09:43:39 +00:00
|
|
|
self::$unitGlobals[ $global ] =& $GLOBALS[ $global ];
|
|
|
|
|
}
|
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
2019-08-07 13:40:55 +00:00
|
|
|
|
2020-04-15 19:30:38 +00:00
|
|
|
// Would be nice if we could simply replace $GLOBALS as a whole,
|
|
|
|
|
// but un-setting or re-assigning that breaks the reference of this magic
|
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
2019-08-07 13:40:55 +00:00
|
|
|
// variable. Thus we have to modify it in place.
|
|
|
|
|
self::$originalGlobals = [];
|
|
|
|
|
foreach ( $GLOBALS as $key => $_ ) {
|
|
|
|
|
// Stash current values
|
|
|
|
|
self::$originalGlobals[$key] =& $GLOBALS[$key];
|
|
|
|
|
|
|
|
|
|
// Remove globals not part of the snapshot (see bootstrap.php, phpunit.php).
|
|
|
|
|
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) {
|
|
|
|
|
unset( $GLOBALS[$key] );
|
|
|
|
|
}
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
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
2019-08-07 13:40:55 +00:00
|
|
|
// Restore values from the early snapshot
|
|
|
|
|
// Not by ref because tests must not be able to modify the snapshot.
|
|
|
|
|
foreach ( self::$unitGlobals as $key => $value ) {
|
|
|
|
|
$GLOBALS[ $key ] = $value;
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 07:14:28 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
protected function runTest() {
|
|
|
|
|
try {
|
|
|
|
|
return parent::runTest();
|
|
|
|
|
} catch ( ConfigException $exception ) {
|
|
|
|
|
throw new Exception(
|
|
|
|
|
'Config variables must be mocked, they cannot be accessed directly in tests which extend '
|
|
|
|
|
. self::class,
|
|
|
|
|
$exception->getCode(),
|
|
|
|
|
$exception
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-13 08:52:13 +00:00
|
|
|
|
|
|
|
|
// Don't let LoggerFactory::getProvider() access globals or other things we don't want.
|
|
|
|
|
LoggerFactory::registerProvider( ObjectFactory::getObjectFromSpec( [
|
|
|
|
|
'class' => \MediaWiki\Logger\NullSpi::class
|
|
|
|
|
] ) );
|
2019-09-05 07:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-12 11:13:22 +00:00
|
|
|
/**
|
2021-04-27 19:33:14 +00:00
|
|
|
* The annotation causes this to be called immediately after tearDown()
|
|
|
|
|
* @after
|
2020-03-12 11:13:22 +00:00
|
|
|
*/
|
2021-04-27 19:33:14 +00:00
|
|
|
final protected function mediaWikiTearDown() : void {
|
2019-10-03 12:22:24 +00:00
|
|
|
// Quick reset between tests
|
|
|
|
|
foreach ( $GLOBALS as $key => $_ ) {
|
|
|
|
|
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) {
|
|
|
|
|
unset( $GLOBALS[$key] );
|
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
2019-08-07 13:40:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-03 12:22:24 +00:00
|
|
|
foreach ( self::$unitGlobals as $key => $value ) {
|
|
|
|
|
$GLOBALS[ $key ] = $value;
|
|
|
|
|
}
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
2019-08-18 18:19:05 +00:00
|
|
|
|
2020-03-12 11:13:22 +00:00
|
|
|
/**
|
2021-04-27 19:33:14 +00:00
|
|
|
* The annotation causes this to be called immediately after tearDownAfterClass()
|
|
|
|
|
* @afterClass
|
2020-03-12 11:13:22 +00:00
|
|
|
*/
|
2021-04-27 19:33:14 +00:00
|
|
|
final public static function mediaWikiTearDownAfterClass() : void {
|
2019-10-03 12:22:24 +00:00
|
|
|
// Remove globals created by the test
|
|
|
|
|
foreach ( $GLOBALS as $key => $_ ) {
|
|
|
|
|
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$originalGlobals ) ) {
|
|
|
|
|
unset( $GLOBALS[$key] );
|
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
2019-08-07 13:40:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-03 12:22:24 +00:00
|
|
|
// Restore values (including reference!)
|
|
|
|
|
foreach ( self::$originalGlobals as $key => &$value ) {
|
|
|
|
|
$GLOBALS[ $key ] =& $value;
|
|
|
|
|
}
|
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
2019-08-07 13:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 21:15:25 +00:00
|
|
|
}
|