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
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\ConfigException;
|
2024-06-13 21:21:02 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2023-11-21 21:08:14 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
|
|
|
|
use MediaWiki\Deferred\DeferredUpdatesScopeMediaWikiStack;
|
|
|
|
|
use MediaWiki\Deferred\DeferredUpdatesScopeStack;
|
2019-08-13 08:52:13 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-07-12 08:50:58 +00:00
|
|
|
use MediaWiki\Logger\NullSpi;
|
2023-07-13 23:40:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-08-10 05:17:59 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2023-07-15 15:21:58 +00:00
|
|
|
use MediaWiki\Settings\SettingsBuilder;
|
2019-09-05 07:14:28 +00:00
|
|
|
use PHPUnit\Framework\Exception;
|
2020-01-10 00:00:51 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-03-09 22:16:22 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
2024-04-24 15:48:19 +00:00
|
|
|
use Wikimedia\Services\NoSuchServiceException;
|
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
|
|
|
*
|
2021-07-08 02:51:13 +00:00
|
|
|
* @stable to extend
|
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
|
|
|
|
2024-09-13 20:51:51 +00:00
|
|
|
/** @var array */
|
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;
|
2024-09-13 20:51:51 +00:00
|
|
|
/** @var array */
|
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 $unitGlobals;
|
2019-07-08 13:25:31 +00:00
|
|
|
|
2024-04-24 15:48:19 +00:00
|
|
|
private ?MediaWikiServices $serviceContainer = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array<string,object>
|
|
|
|
|
*/
|
|
|
|
|
private array $services = [];
|
|
|
|
|
|
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-10-29 11:44:14 +00:00
|
|
|
'wgLegalTitleChars',
|
|
|
|
|
'wgDevelopmentWarnings',
|
2020-08-22 19:26:19 +00:00
|
|
|
// Dependency of wfParseUrl()
|
|
|
|
|
'wgUrlProtocols',
|
2021-09-27 07:51:50 +00:00
|
|
|
// For LegacyLogger, injected by DevelopmentSettings.php
|
|
|
|
|
'wgDebugLogFile',
|
|
|
|
|
'wgDebugLogGroups',
|
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-07-22 03:11:47 +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;
|
2022-07-29 00:45:09 +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
|
|
|
}
|
2023-07-13 23:40:18 +00:00
|
|
|
|
2023-08-24 23:38:02 +00:00
|
|
|
// Set DeferredUpdates into standalone mode
|
|
|
|
|
DeferredUpdates::setScopeStack( new DeferredUpdatesScopeStack() );
|
2023-07-13 23:40:18 +00:00
|
|
|
MediaWikiServices::disallowGlobalInstanceInUnitTests();
|
2023-07-15 14:51:26 +00:00
|
|
|
ExtensionRegistry::disableForTest();
|
2023-07-15 15:21:58 +00:00
|
|
|
SettingsBuilder::disableAccessForUnitTests();
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 07:14:28 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
protected function runTest() {
|
|
|
|
|
try {
|
2022-07-12 08:50:58 +00:00
|
|
|
// Don't let LoggerFactory::getProvider() access globals or other things we don't want.
|
|
|
|
|
LoggerFactory::registerProvider( ObjectFactory::getObjectFromSpec( [
|
|
|
|
|
'class' => NullSpi::class
|
|
|
|
|
] ) );
|
2019-09-05 07:14:28 +00:00
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-07-22 03:11:47 +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-07-22 03:11:47 +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;
|
|
|
|
|
}
|
2023-07-13 23:40:18 +00:00
|
|
|
unset( $value );
|
|
|
|
|
|
|
|
|
|
MediaWikiServices::allowGlobalInstanceAfterUnitTests();
|
2023-08-24 23:38:02 +00:00
|
|
|
DeferredUpdates::setScopeStack( new DeferredUpdatesScopeMediaWikiStack() );
|
2023-07-15 14:51:26 +00:00
|
|
|
ExtensionRegistry::enableForTest();
|
2023-07-15 15:21:58 +00:00
|
|
|
SettingsBuilder::enableAccessAfterUnitTests();
|
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
|
|
|
}
|
|
|
|
|
|
2024-04-24 15:48:19 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a mock service container.
|
|
|
|
|
* To populate the service container with service objects, use setService().
|
|
|
|
|
*
|
|
|
|
|
* @since 1.43
|
|
|
|
|
*/
|
|
|
|
|
protected function getServiceContainer(): MediaWikiServices {
|
|
|
|
|
if ( !$this->serviceContainer ) {
|
|
|
|
|
$this->serviceContainer = $this->getMockBuilder( MediaWikiServices::class )
|
|
|
|
|
->setConstructorArgs( [ new HashConfig() ] )
|
|
|
|
|
->onlyMethods( [
|
|
|
|
|
'getService',
|
|
|
|
|
'disableStorage',
|
|
|
|
|
'isStorageDisabled',
|
|
|
|
|
'redefineService',
|
|
|
|
|
'resetServiceForTesting',
|
|
|
|
|
'resetChildProcessServices',
|
|
|
|
|
'peekService'
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->serviceContainer
|
|
|
|
|
->method( 'getService' )
|
|
|
|
|
->willReturnCallback( function ( $name ) {
|
|
|
|
|
return $this->getService( $name );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->serviceContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a service previously defined with setService().
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
*
|
|
|
|
|
* @return mixed The service instance
|
|
|
|
|
*/
|
|
|
|
|
protected function getService( string $name ) {
|
|
|
|
|
if ( !isset( $this->services[$name] ) ) {
|
|
|
|
|
throw new NoSuchServiceException( $name );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_callable( $this->services[$name] ) ) {
|
|
|
|
|
$func = $this->services[$name];
|
|
|
|
|
$this->services[$name] = $func( $this->serviceContainer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->services[$name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register a service object with the service container returned by
|
|
|
|
|
* getServiceContainer().
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $service
|
|
|
|
|
*
|
|
|
|
|
* @since 1.43
|
|
|
|
|
*/
|
|
|
|
|
protected function setService( string $name, $service ) {
|
|
|
|
|
$this->services[$name] = $service;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 21:15:25 +00:00
|
|
|
}
|