Sets a global, maintaining a stashed version of the previous global to be restored in tearDownThe key is added to the array of globals that will be reset afterwards in the tearDown().
use Psr\Log\LoggerInterface;
use Wikimedia\TestingAccessWrapper;
'mysql',
'sqlite',
'postgres',
'oracle'
];
parent::__construct(
$name, $data, $dataName );
$this->backupGlobals = false;
$this->backupStaticAttributes = false;
}
if ( isset( $this->called['setUp'] ) && !isset( $this->called['tearDown'] ) ) {
}
}
parent::setUpBeforeClass();
}
}
}
}
}
}
$oldServices = MediaWikiServices::getInstance();
$oldConfigFactory = $oldServices->getConfigFactory();
MediaWikiServices::resetGlobalInstance( $testConfig );
$oldConfigFactory,
);
}
Config $customOverrides =
null
) {
if ( !$baseConfig ) {
$baseConfig = MediaWikiServices::getInstance()->getBootstrapConfig();
}
$hashCache = [
'class' =>
'HashBagOStuff',
'reportDupes' =>
false ];
$objectCaches = [
'apc' => $hashCache,
'apcu' => $hashCache,
'xcache' => $hashCache,
'wincache' => $hashCache,
] + $baseConfig->get( 'ObjectCaches' );
$defaultOverrides->set( 'ObjectCaches', $objectCaches );
$defaultOverrides->set(
'MainCacheType',
CACHE_NONE );
$defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => 'JobQueueMemory' ] ] );
$defaultOverrides->set( 'PasswordDefault', 'A' );
$testConfig = $customOverrides
?
new MultiConfig( [ $customOverrides, $defaultOverrides, $baseConfig ] )
:
new MultiConfig( [ $defaultOverrides, $baseConfig ] );
return $testConfig;
}
) {
$bootstrapConfig = $newServices->getBootstrapConfig();
$newServices->resetServiceForTesting( 'ConfigFactory' );
$newServices->redefineService(
'ConfigFactory',
self::makeTestConfigFactoryInstantiator(
$oldConfigFactory,
[ 'main' => $bootstrapConfig ]
)
);
}
) {
$namesToClone = array_diff(
array_keys( $configurations )
);
foreach ( $namesToClone
as $name ) {
}
foreach ( $configurations
as $name => $config ) {
$factory->register(
$name, $config );
}
return $factory;
};
}
$services = MediaWikiServices::getInstance();
$services->resetServiceForTesting(
'MainObjectStash' );
$services->resetServiceForTesting(
'LocalServerObjectCache' );
$services->getMainWANObjectCache()->clearProcessCache();
if ( session_id() !== '' ) {
session_write_close();
session_id( '' );
}
}
public function run( PHPUnit_Framework_TestResult
$result =
null ) {
$needsResetDB = false;
if ( !self::$dbSetup || $this->
needsDB() ) {
self::$useTemporaryTables = !$this->
getCliArg(
'use-normal-tables' );
self::$reuseDB = $this->
getCliArg(
'reuse-db' );
if ( !self::$dbSetup ) {
$this->
resetDB( $this->db, $this->tablesUsed );
}
}
}
$needsResetDB = true;
}
if ( $needsResetDB ) {
$this->
resetDB( $this->db, $this->tablesUsed );
}
}
$first = !isset( $this->db->_hasDataForTestClass )
|| $this->db->_hasDataForTestClass !== $class;
$this->db->_hasDataForTestClass = $class;
return $first;
}
}
$this->tmpFiles[] = $fileName;
return $fileName;
}
unlink( $fileName );
return $fileName;
}
protected function setUp() {
parent::setUp();
$this->called['setUp'] = true;
$this->phpErrorLevel = intval( ini_get( 'error_reporting' ) );
foreach ( $this->tmpFiles
as $fileName ) {
if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
unlink( $fileName );
} elseif ( is_dir( $fileName ) ) {
}
}
if ( $this->
needsDB() && $this->db ) {
while ( $this->db->trxLevel() > 0 ) {
$this->db->rollback( __METHOD__, 'flush' );
}
if ( $this->db->getType() === 'mysql' ) {
$this->db->query( "SET sql_mode = 'STRICT_ALL_TABLES'" );
}
}
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' );
}
$this->tmpFiles = array_merge( $this->tmpFiles, (
array)$files );
}
$status[
'name'] ===
'MediaWikiTestCase::wfResetOutputBuffersBarrier'
) {
ob_end_flush();
}
$this->called['tearDown'] = true;
foreach ( $this->tmpFiles
as $fileName ) {
if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
unlink( $fileName );
} elseif ( is_dir( $fileName ) ) {
}
}
if ( $this->
needsDB() && $this->db ) {
while ( $this->db->trxLevel() > 0 ) {
$this->db->rollback( __METHOD__, 'flush' );
}
if ( $this->db->getType() === 'mysql' ) {
$this->db->query(
"SET sql_mode = " . $this->db->addQuotes(
$wgSQLMode ) );
}
}
foreach ( $this->mwGlobals
as $key =>
$value ) {
}
foreach ( $this->mwGlobalsToUnset
as $value ) {
}
$this->mwGlobals = [];
$this->mwGlobalsToUnset = [];
if ( self::$serviceLocator && MediaWikiServices::getInstance() !== self::$serviceLocator ) {
MediaWikiServices::forceGlobalInstance( self::$serviceLocator );
}
if ( session_id() !== '' ) {
session_write_close();
session_id( '' );
}
ini_set( 'error_reporting', $this->phpErrorLevel );
$oldHex = strtoupper( dechex( $this->phpErrorLevel ) );
$message = "PHP error_reporting setting was left dirty: "
. "was 0x$oldHex before test, 0x$newHex after test!";
$this->fail( $message );
}
parent::tearDown();
}
$this->assertArrayHasKey( 'setUp', $this->called,
);
}
if ( MediaWikiServices::getInstance() === self::$serviceLocator ) {
}
MediaWikiServices::getInstance()->disableService(
$name );
MediaWikiServices::getInstance()->redefineService(
function ()
use ( $object ) {
return $object;
}
);
}
if ( is_string( $pairs ) ) {
$pairs = [ $pairs =>
$value ];
}
}
}
return true;
}
if ( !is_scalar( $subValue ) && $subValue !== null ) {
return false;
}
}
return true;
}
return false;
}
if ( is_string( $globalKeys ) ) {
$globalKeys = [ $globalKeys ];
}
foreach ( $globalKeys
as $globalKey ) {
if (
!array_key_exists( $globalKey, $this->mwGlobals ) &&
!array_key_exists( $globalKey, $this->mwGlobalsToUnset )
) {
if ( !array_key_exists( $globalKey,
$GLOBALS ) ) {
$this->mwGlobalsToUnset[$globalKey] = $globalKey;
continue;
}
if ( self::canShallowCopy(
$GLOBALS[$globalKey] ) ) {
$this->mwGlobals[$globalKey] =
$GLOBALS[$globalKey];
} elseif (
) {
$this->mwGlobals[$globalKey] = clone
$GLOBALS[$globalKey];
} else {
try {
}
catch ( Exception
$e ) {
$this->mwGlobals[$globalKey] =
$GLOBALS[$globalKey];
}
}
}
}
}
$merged = $values;
} else {
throw new MWException(
"MW global $name is not an array." );
}
foreach ( $values
as $k => $v ) {
$merged[$k] = $v;
}
}
}
if ( !$configOverrides ) {
}
$oldInstance = MediaWikiServices::getInstance();
$oldConfigFactory = $oldInstance->getConfigFactory();
$wiringFiles = $testConfig->get( 'ServiceWiringFiles' );
$newInstance->loadWiringFiles( $wiringFiles );
Hooks::run(
'MediaWikiServices', [ $newInstance ] );
$newInstance->redefineService(
$name, $callback );
}
$oldConfigFactory,
$newInstance
);
MediaWikiServices::forceGlobalInstance( $newInstance );
return $newInstance;
}
}
$langCode =
$lang->getCode();
} else {
}
'wgLanguageCode' => $langCode,
'wgContLang' => $langObj,
] );
}
protected function setLogger( $channel, LoggerInterface $logger ) {
$provider = LoggerFactory::getProvider();
$wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
$singletons = $wrappedProvider->singletons;
if ( !isset( $this->loggers[$channel] ) ) {
$this->loggers[$channel] = isset( $singletons['loggers'][$channel] )
? $singletons['loggers'][$channel] : null;
}
$singletons['loggers'][$channel] = $logger;
} elseif ( $provider instanceof
LegacySpi ) {
if ( !isset( $this->loggers[$channel] ) ) {
$this->loggers[$channel] = isset( $singletons[$channel] ) ? $singletons[$channel] : null;
}
$singletons[$channel] = $logger;
} else {
throw new LogicException( __METHOD__ . ': setting a logger for ' . get_class( $provider )
. ' is not implemented' );
}
$wrappedProvider->singletons = $singletons;
}
$provider = LoggerFactory::getProvider();
$wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
$singletons = $wrappedProvider->singletons;
foreach ( $this->loggers
as $channel => $logger ) {
if ( $logger === null ) {
unset( $singletons['loggers'][$channel] );
} else {
$singletons['loggers'][$channel] = $logger;
}
} elseif ( $provider instanceof
LegacySpi ) {
if ( $logger === null ) {
unset( $singletons[$channel] );
} else {
$singletons[$channel] = $logger;
}
}
}
$wrappedProvider->singletons = $singletons;
$this->loggers = [];
}
}
# if the test says it uses database tables, it needs the database
if ( $this->tablesUsed ) {
return true;
}
# if the test says it belongs to the Database group, it needs the database
$rc = new ReflectionClass( $this );
if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
return true;
}
return false;
}
$pageName,
$text = 'Sample page for unit test.',
$namespace = null
) {
if ( is_string( $pageName ) ) {
} else {
}
$user = static::getTestSysop()->getUser();
$comment = __METHOD__ . ': Sample page for unit test.';
return [
];
}
}
}
if ( $this->db->getType() == 'oracle' ) {
# Insert 0 user to prevent FK violations
# Anonymous user
if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
$this->db->insert( 'user', [
'user_id' => 0,
'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
}
# Insert 0 page to prevent FK violations
# Blank page
if ( !$this->db->selectField( 'page', '1', [ 'page_id' => 0 ] ) ) {
$this->db->insert( 'page', [
'page_id' => 0,
'page_namespace' => 0,
'page_title' => ' ',
'page_restrictions' => null,
'page_is_redirect' => 0,
'page_is_new' => 0,
'page_random' => 0,
'page_touched' => $this->db->timestamp(),
'page_latest' => 0,
'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
}
}
$user = static::getTestSysop()->getUser();
if (
$page->getId() == 0 ) {
'UTPageSummary',
false,
);
if ( session_id() !== '' ) {
session_write_close();
session_id( '' );
}
}
}
if ( !self::$dbSetup ) {
return;
}
foreach ( $wgJobClasses
as $type => $class ) {
}
self::$oldTablePrefix = false;
self::$dbSetup = false;
}
$dbClone->useTemporaryTables( self::$useTemporaryTables );
return false;
} else {
$dbClone->cloneTableStructure();
return true;
}
}
if ( self::isUsingExternalStoreDB() ) {
}
}
if ( self::$dbSetup ) {
return;
}
if (
$db->tablePrefix() === $prefix ) {
'Cannot run unit tests, the database prefix is already "' . $prefix . '"' );
}
self::$dbSetup = true;
if ( !self::setupDatabaseWithTestPrefix(
$db, $prefix ) ) {
return;
}
if (
$db->getType() ==
'oracle' ) {
$db->query(
'BEGIN FILL_WIKI_INFO; END;' );
}
}
foreach ( $connections
as $dbw ) {
$dbw->tablePrefix( self::$oldTablePrefix );
}
}
$dbws = [];
foreach ( $defaultArray
as $url ) {
if ( strpos( $url, 'DB://' ) === 0 ) {
list( $proto, $cluster ) = explode(
'://', $url, 2 );
$dbws[] = $externalStoreDB->getMaster( $cluster );
}
}
return $dbws;
}
return false;
}
foreach ( $defaultArray
as $url ) {
if ( strpos( $url, 'DB://' ) === 0 ) {
return true;
}
}
return false;
}
$userTables = [ 'user', 'user_groups', 'user_properties' ];
$coreDBDataTables = array_merge( $userTables, [ 'page', 'revision' ] );
}
$truncate = in_array(
$db->getType(), [
'oracle',
'mysql' ] );
if ( $tbl == 'interwiki' ) {
continue;
}
if ( $truncate ) {
$db->query(
'TRUNCATE TABLE ' .
$db->tableName( $tbl ), __METHOD__ );
} else {
$db->delete( $tbl,
'*', __METHOD__ );
}
if ( $tbl === 'page' ) {
}
}
if ( array_intersect(
$tablesUsed, $coreDBDataTables ) ) {
}
}
}
static $compatibility = [
'createMock' => 'createMock2',
];
if ( isset( $compatibility[$func] ) ) {
return call_user_func_array( [ $this, $compatibility[$func] ],
$args );
} else {
}
}
return $this->getMockBuilder( $originalClassName )
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
}
private static function unprefixTable( &$tableName, $ind, $prefix ) {
$tableName = substr( $tableName, strlen( $prefix ) );
}
return strpos( $table, 'unittest_' ) !== 0;
}
$prefix =
$db->tablePrefix();
if (
$db->getType() ===
'mysql' ) {
static $viewListCache = null;
if ( $viewListCache === null ) {
$viewListCache =
$db->listViews(
null, __METHOD__ );
}
}
array_walk(
$tables, [ __CLASS__,
'unprefixTable' ], $prefix );
if (
$db->getType() ==
'sqlite' ) {
unset(
$tables[
'searchindex_content'] );
unset(
$tables[
'searchindex_segdir'] );
unset(
$tables[
'searchindex_segments'] );
}
}
throw new MWException( $this->db->getType() .
" is not currently supported for unit testing." );
}
}
}
return null;
}
}
MediaWiki\suppressWarnings();
MediaWiki\restoreWarnings();
}
protected function assertSelect( $table, $fields, $condition,
array $expectedRows ) {
throw new MWException(
'When testing database state, the test cases\'s needDB()' .
' method should return true. Use @group Database or $this->tablesUsed.' );
}
$res =
$db->select( $table, $fields, $condition,
wfGetCaller(), [
'ORDER BY' => $fields ] );
$this->assertNotEmpty(
$res,
"query failed: " .
$db->lastError() );
$i = 0;
foreach ( $expectedRows
as $expected ) {
$i += 1;
$this->assertNotEmpty( $r, "row #$i missing" );
$this->assertEquals( $expected, $r, "row #$i mismatches" );
}
$this->assertFalse( $r, "found extra row (after #$i)" );
}
return array_map(
function ( $element ) {
return [ $element ];
},
$elements
);
}
$ordered = false, $named = false
) {
if ( !$ordered ) {
}
if ( !$named ) {
$expected = array_values( $expected );
$actual = array_values( $actual );
}
call_user_func_array(
[ $this, 'assertEquals' ],
array_merge( [ $expected, $actual ], array_slice( func_get_args(), 4 ) )
);
}
$expected = str_replace( '>', ">\n", $expected );
$actual = str_replace( '>', ">\n", $actual );
$this->assertEquals( $expected, $actual, $msg );
}
uasort(
$array,
function ( $a, $b ) {
}
);
}
if ( !is_array( $r ) ) {
return;
}
foreach ( $r
as $k => $v ) {
if ( is_string( $k ) ) {
unset( $r[$k] );
}
}
}
$this->assertTrue( true, $message );
} else {
}
}
if ( class_exists(
$type ) || interface_exists(
$type ) ) {
$this->assertInstanceOf(
$type, $actual, $message );
} else {
$this->assertInternalType(
$type, $actual, $message );
}
}
global $wgNamespaceContentModels;
if ( isset( $wgNamespaceContentModels[$ns] ) ) {
}
return true;
}
global $wgNamespaceContentModels;
static $wikitextNS = null;
if ( $wikitextNS !== null ) {
return $wikitextNS;
}
if ( !isset( $wgNamespaceContentModels[
NS_MAIN] ) ) {
}
) );
] );
} );
if ( !isset( $wgNamespaceContentModels[$ns] ) ||
) {
$wikitextNS = $ns;
return $wikitextNS;
}
}
throw new MWException(
"No namespace defaults to wikitext!" );
}
# This check may also protect against code injection in
# case of broken installations.
MediaWiki\suppressWarnings();
$haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
MediaWiki\restoreWarnings();
if ( !$haveDiff3 ) {
$this->markTestSkipped( "Skip test, since diff3 is not configured" );
}
}
$loaded = extension_loaded( $extName );
if ( !$loaded ) {
$this->markTestSkipped( "PHP extension '$extName' is not loaded, skipping." );
}
return $loaded;
}
$html =
'<!DOCTYPE html><html><head><title>test</title></head><body>' .
$html .
'</body></html>';
}
$this->markTestSkipped( 'Tidy extension not installed' );
}
$errorBuffer = '';
$allErrors = preg_split( '/[\r\n]+/', $errorBuffer );
$errors = preg_grep(
'/^(.*Warning: (trimming empty|.* lacks ".*?" attribute).*|\s*)$/m',
$allErrors, PREG_GREP_INVERT
);
$this->assertEmpty( $errors, implode( "\n", $errors ) );
}
}
}
}