2010-12-29 15:52:07 +00:00
|
|
|
<?php
|
2017-04-19 19:37:35 +00:00
|
|
|
|
2016-02-07 08:12:10 +00:00
|
|
|
use MediaWiki\Logger\LegacySpi;
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
use MediaWiki\Logger\MonologSpi;
|
2016-05-01 19:29:11 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-02-07 08:12:10 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-16 16:48:25 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-06-29 00:14:18 +00:00
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase;
|
|
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2017-12-22 07:24:06 +00:00
|
|
|
use MediaWikiCoversValidator;
|
2018-03-02 10:42:16 +00:00
|
|
|
use PHPUnit4And6Compat;
|
2017-12-22 07:24:06 +00:00
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
2018-08-30 11:35:25 +00:00
|
|
|
* The original service locator. This is overridden during setUp().
|
2016-05-01 19:29:11 +00:00
|
|
|
*
|
|
|
|
|
* @var MediaWikiServices|null
|
|
|
|
|
*/
|
2018-08-30 11:35:25 +00:00
|
|
|
private static $originalServices;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The local service locator, created during setUp().
|
2018-09-11 17:01:08 +00:00
|
|
|
* @var MediaWikiServices
|
2018-08-30 11:35:25 +00:00
|
|
|
*/
|
|
|
|
|
private $localServices;
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2012-10-22 23:53:51 +00:00
|
|
|
/**
|
|
|
|
|
* $called tracks whether the setUp and tearDown method has been called.
|
|
|
|
|
* class extending MediaWikiTestCase usually override setUp and tearDown
|
|
|
|
|
* but forget to call the parent.
|
|
|
|
|
*
|
|
|
|
|
* The array format takes a method name as key and anything as a value.
|
|
|
|
|
* By asserting the key exist, we know the child class has called the
|
|
|
|
|
* parent.
|
|
|
|
|
*
|
|
|
|
|
* This property must be private, we do not want child to override it,
|
|
|
|
|
* they should call the appropriate parent method instead.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private $called = [];
|
2012-10-22 23:53:51 +00:00
|
|
|
|
2012-09-11 00:07:18 +00:00
|
|
|
/**
|
2014-03-05 14:41:41 +00:00
|
|
|
* @var TestUser[]
|
|
|
|
|
* @since 1.20
|
2012-09-11 00:07:18 +00:00
|
|
|
*/
|
|
|
|
|
public static $users;
|
|
|
|
|
|
2011-05-24 21:22:36 +00:00
|
|
|
/**
|
2016-03-03 07:30:24 +00:00
|
|
|
* Primary database
|
|
|
|
|
*
|
2016-09-26 22:40:07 +00:00
|
|
|
* @var Database
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.18
|
2011-05-24 21:22:36 +00:00
|
|
|
*/
|
2010-12-29 15:52:07 +00:00
|
|
|
protected $db;
|
2014-03-05 14:41:41 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $tablesUsed = []; // tables with data
|
2011-12-10 05:47:46 +00:00
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
private static $useTemporaryTables = true;
|
|
|
|
|
private static $reuseDB = false;
|
2011-05-24 21:22:36 +00:00
|
|
|
private static $dbSetup = false;
|
2016-09-19 01:39:59 +00:00
|
|
|
private static $oldTablePrefix = '';
|
2011-01-10 18:34:59 +00:00
|
|
|
|
2013-07-17 16:42:24 +00:00
|
|
|
/**
|
|
|
|
|
* Original value of PHP's error_reporting setting.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $phpErrorLevel;
|
|
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
/**
|
|
|
|
|
* Holds the paths of temporary files/directories created through getNewTempFile,
|
|
|
|
|
* and getNewTempDirectory
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private $tmpFiles = [];
|
2012-04-10 14:28:42 +00:00
|
|
|
|
2012-10-08 10:59:55 +00:00
|
|
|
/**
|
|
|
|
|
* Holds original values of MediaWiki configuration settings
|
|
|
|
|
* to be restored in tearDown().
|
2013-12-27 11:50:27 +00:00
|
|
|
* See also setMwGlobals().
|
2012-10-08 10:59:55 +00:00
|
|
|
* @var array
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private $mwGlobals = [];
|
2012-04-10 14:28:42 +00:00
|
|
|
|
2016-11-18 13:55:21 +00:00
|
|
|
/**
|
|
|
|
|
* Holds list of MediaWiki configuration settings to be unset in tearDown().
|
|
|
|
|
* See also setMwGlobals().
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $mwGlobalsToUnset = [];
|
|
|
|
|
|
2016-02-07 08:12:10 +00:00
|
|
|
/**
|
|
|
|
|
* Holds original loggers which have been replaced by setLogger()
|
|
|
|
|
* @var LoggerInterface[]
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private $loggers = [];
|
2016-02-07 08:12:10 +00:00
|
|
|
|
2018-08-28 02:39:33 +00:00
|
|
|
/**
|
2018-09-03 07:02:44 +00:00
|
|
|
* The CLI arguments passed through from phpunit.php
|
|
|
|
|
* @var array
|
2018-08-28 02:39:33 +00:00
|
|
|
*/
|
2018-09-03 07:02:44 +00:00
|
|
|
private $cliArgs = [];
|
2018-08-28 02:39:33 +00:00
|
|
|
|
2011-01-10 18:34:59 +00:00
|
|
|
/**
|
|
|
|
|
* Table name prefixes. Oracle likes it shorter.
|
|
|
|
|
*/
|
|
|
|
|
const DB_PREFIX = 'unittest_';
|
|
|
|
|
const ORA_DB_PREFIX = 'ut_';
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $supportedDBs = [
|
2010-12-31 21:10:36 +00:00
|
|
|
'mysql',
|
2011-01-10 19:22:27 +00:00
|
|
|
'sqlite',
|
2011-10-24 19:28:31 +00:00
|
|
|
'postgres',
|
2011-01-10 19:22:27 +00:00
|
|
|
'oracle'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
public function __construct( $name = null, array $data = [], $dataName = '' ) {
|
2011-02-05 16:25:18 +00:00
|
|
|
parent::__construct( $name, $data, $dataName );
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2010-12-31 20:47:48 +00:00
|
|
|
$this->backupGlobals = false;
|
2011-02-20 17:52:09 +00:00
|
|
|
$this->backupStaticAttributes = false;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-09-14 19:07:14 +00:00
|
|
|
public function __destruct() {
|
|
|
|
|
// Complain if self::setUp() was called, but not self::tearDown()
|
|
|
|
|
// $this->called['setUp'] will be checked by self::testMediaWikiTestCaseParentSetupCalled()
|
|
|
|
|
if ( isset( $this->called['setUp'] ) && !isset( $this->called['tearDown'] ) ) {
|
2016-02-25 20:00:22 +00:00
|
|
|
throw new MWException( static::class . "::tearDown() must call parent::tearDown()" );
|
2014-09-14 19:07:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
|
2018-08-30 11:35:25 +00:00
|
|
|
// Get the original service locator
|
|
|
|
|
if ( !self::$originalServices ) {
|
|
|
|
|
self::$originalServices = MediaWikiServices::getInstance();
|
|
|
|
|
}
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
/**
|
|
|
|
|
* Convenience method for getting an immutable test user
|
|
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $groups Groups the test user should be in.
|
|
|
|
|
* @return TestUser
|
|
|
|
|
*/
|
|
|
|
|
public static function getTestUser( $groups = [] ) {
|
|
|
|
|
return TestUserRegistry::getImmutableTestUser( $groups );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience method for getting a mutable test user
|
|
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $groups Groups the test user should be added in.
|
|
|
|
|
* @return TestUser
|
|
|
|
|
*/
|
|
|
|
|
public static function getMutableTestUser( $groups = [] ) {
|
|
|
|
|
return TestUserRegistry::getMutableTestUser( __CLASS__, $groups );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience method for getting an immutable admin test user
|
|
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $groups Groups the test user should be added to.
|
|
|
|
|
* @return TestUser
|
|
|
|
|
*/
|
|
|
|
|
public static function getTestSysop() {
|
|
|
|
|
return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 16:36:34 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a WikiPage representing an existing page.
|
|
|
|
|
*
|
2018-06-14 07:46:18 +00:00
|
|
|
* @since 1.32
|
|
|
|
|
*
|
2018-06-12 16:36:34 +00:00
|
|
|
* @param Title|string|null $title
|
|
|
|
|
* @return WikiPage
|
2018-09-14 18:30:30 +00:00
|
|
|
* @throws MWException If this test cases's needsDB() method doesn't return true.
|
|
|
|
|
* Test cases can use "@group Database" to enable database test support,
|
|
|
|
|
* or list the tables under testing in $this->tablesUsed, or override the
|
|
|
|
|
* needsDB() method.
|
2018-06-12 16:36:34 +00:00
|
|
|
*/
|
|
|
|
|
protected function getExistingTestPage( $title = null ) {
|
2018-09-14 18:30:30 +00:00
|
|
|
if ( !$this->needsDB() ) {
|
|
|
|
|
throw new MWException( 'When testing which pages, the test cases\'s needsDB()' .
|
|
|
|
|
' method should return true. Use @group Database or $this->tablesUsed.' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 16:36:34 +00:00
|
|
|
$title = ( $title === null ) ? 'UTPage' : $title;
|
|
|
|
|
$title = is_string( $title ) ? Title::newFromText( $title ) : $title;
|
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
|
|
|
|
|
if ( !$page->exists() ) {
|
|
|
|
|
$user = self::getTestSysop()->getUser();
|
|
|
|
|
$page->doEditContent(
|
|
|
|
|
new WikitextContent( 'UTContent' ),
|
|
|
|
|
'UTPageSummary',
|
|
|
|
|
EDIT_NEW | EDIT_SUPPRESS_RC,
|
|
|
|
|
false,
|
|
|
|
|
$user
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a WikiPage representing a non-existing page.
|
|
|
|
|
*
|
2018-06-14 07:46:18 +00:00
|
|
|
* @since 1.32
|
|
|
|
|
*
|
2018-06-12 16:36:34 +00:00
|
|
|
* @param Title|string|null $title
|
|
|
|
|
* @return WikiPage
|
2018-09-14 18:30:30 +00:00
|
|
|
* @throws MWException If this test cases's needsDB() method doesn't return true.
|
|
|
|
|
* Test cases can use "@group Database" to enable database test support,
|
|
|
|
|
* or list the tables under testing in $this->tablesUsed, or override the
|
|
|
|
|
* needsDB() method.
|
2018-06-12 16:36:34 +00:00
|
|
|
*/
|
|
|
|
|
protected function getNonexistingTestPage( $title = null ) {
|
2018-09-14 18:30:30 +00:00
|
|
|
if ( !$this->needsDB() ) {
|
|
|
|
|
throw new MWException( 'When testing which pages, the test cases\'s needsDB()' .
|
|
|
|
|
' method should return true. Use @group Database or $this->tablesUsed.' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 16:36:34 +00:00
|
|
|
$title = ( $title === null ) ? 'UTPage-' . rand( 0, 100000 ) : $title;
|
|
|
|
|
$title = is_string( $title ) ? Title::newFromText( $title ) : $title;
|
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
|
|
|
|
|
if ( $page->exists() ) {
|
|
|
|
|
$page->doDeleteArticle( 'Testing' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
2018-08-30 11:35:25 +00:00
|
|
|
* @deprecated since 1.32
|
2016-05-01 19:29:11 +00:00
|
|
|
*/
|
|
|
|
|
public static function prepareServices( Config $bootstrapConfig ) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a config suitable for testing, based on a base config, default overrides,
|
|
|
|
|
* and custom overrides.
|
|
|
|
|
*
|
|
|
|
|
* @param Config|null $baseConfig
|
|
|
|
|
* @param Config|null $customOverrides
|
|
|
|
|
*
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
private static function makeTestConfig(
|
|
|
|
|
Config $baseConfig = null,
|
|
|
|
|
Config $customOverrides = null
|
|
|
|
|
) {
|
|
|
|
|
$defaultOverrides = new HashConfig();
|
|
|
|
|
|
|
|
|
|
if ( !$baseConfig ) {
|
2018-08-30 11:35:25 +00:00
|
|
|
$baseConfig = self::$originalServices->getBootstrapConfig();
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
/* Some functions require some kind of caching, and will end up using the db,
|
|
|
|
|
* which we can't allow, as that would open a new connection for mysql.
|
|
|
|
|
* Replace with a HashBag. They would not be going to persist anyway.
|
|
|
|
|
*/
|
2018-01-13 00:02:09 +00:00
|
|
|
$hashCache = [ 'class' => HashBagOStuff::class, 'reportDupes' => false ];
|
2016-05-01 19:29:11 +00:00
|
|
|
$objectCaches = [
|
|
|
|
|
CACHE_DB => $hashCache,
|
|
|
|
|
CACHE_ACCEL => $hashCache,
|
|
|
|
|
CACHE_MEMCACHED => $hashCache,
|
|
|
|
|
'apc' => $hashCache,
|
2016-10-12 14:39:46 +00:00
|
|
|
'apcu' => $hashCache,
|
2016-05-01 19:29:11 +00:00
|
|
|
'wincache' => $hashCache,
|
|
|
|
|
] + $baseConfig->get( 'ObjectCaches' );
|
|
|
|
|
|
|
|
|
|
$defaultOverrides->set( 'ObjectCaches', $objectCaches );
|
|
|
|
|
$defaultOverrides->set( 'MainCacheType', CACHE_NONE );
|
2018-01-13 00:02:09 +00:00
|
|
|
$defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => JobQueueMemory::class ] ] );
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2016-05-12 10:40:41 +00:00
|
|
|
// Use a fast hash algorithm to hash passwords.
|
|
|
|
|
$defaultOverrides->set( 'PasswordDefault', 'A' );
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
$testConfig = $customOverrides
|
|
|
|
|
? new MultiConfig( [ $customOverrides, $defaultOverrides, $baseConfig ] )
|
|
|
|
|
: new MultiConfig( [ $defaultOverrides, $baseConfig ] );
|
|
|
|
|
|
|
|
|
|
return $testConfig;
|
|
|
|
|
}
|
2011-04-29 16:26:20 +00:00
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
|
|
|
|
* @param ConfigFactory $oldFactory
|
|
|
|
|
* @param Config[] $configurations
|
|
|
|
|
*
|
|
|
|
|
* @return Closure
|
|
|
|
|
*/
|
|
|
|
|
private static function makeTestConfigFactoryInstantiator(
|
|
|
|
|
ConfigFactory $oldFactory,
|
|
|
|
|
array $configurations
|
|
|
|
|
) {
|
2017-06-26 16:35:31 +00:00
|
|
|
return function ( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
|
2016-05-01 19:29:11 +00:00
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
|
|
|
|
|
// clone configurations from $oldFactory that are not overwritten by $configurations
|
|
|
|
|
$namesToClone = array_diff(
|
|
|
|
|
$oldFactory->getConfigNames(),
|
|
|
|
|
array_keys( $configurations )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $namesToClone as $name ) {
|
|
|
|
|
$factory->register( $name, $oldFactory->makeConfig( $name ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $configurations as $name => $config ) {
|
|
|
|
|
$factory->register( $name, $config );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $factory;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-08-30 11:35:25 +00:00
|
|
|
* Resets some non-service singleton instances and other static caches. It's not necessary to
|
|
|
|
|
* reset services here.
|
2016-05-01 19:29:11 +00:00
|
|
|
*/
|
2018-09-11 17:01:08 +00:00
|
|
|
public static function resetNonServiceCaches() {
|
2018-04-23 23:46:11 +00:00
|
|
|
global $wgRequest, $wgJobClasses;
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2018-04-23 23:46:11 +00:00
|
|
|
foreach ( $wgJobClasses as $type => $class ) {
|
|
|
|
|
JobQueueGroup::singleton()->get( $type )->delete();
|
|
|
|
|
}
|
2016-05-01 19:29:11 +00:00
|
|
|
JobQueueGroup::destroySingletons();
|
2018-04-23 23:46:11 +00:00
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
ObjectCache::clear();
|
|
|
|
|
FileBackendGroup::destroySingleton();
|
2018-04-23 23:46:11 +00:00
|
|
|
DeferredUpdates::clearPendingUpdates();
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
// TODO: move global state into MediaWikiServices
|
|
|
|
|
RequestContext::resetMain();
|
|
|
|
|
if ( session_id() !== '' ) {
|
|
|
|
|
session_write_close();
|
|
|
|
|
session_id( '' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgRequest = new FauxRequest();
|
|
|
|
|
MediaWiki\Session\SessionManager::resetCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function run( PHPUnit_Framework_TestResult $result = null ) {
|
2018-09-03 07:02:44 +00:00
|
|
|
if ( $result instanceof MediaWikiTestResult ) {
|
|
|
|
|
$this->cliArgs = $result->getMediaWikiCliArgs();
|
|
|
|
|
}
|
2018-08-30 11:35:25 +00:00
|
|
|
$this->overrideMwServices();
|
|
|
|
|
|
2018-09-11 17:16:26 +00:00
|
|
|
if ( $this->needsDB() && !$this->isTestInDatabaseGroup() ) {
|
|
|
|
|
throw new Exception(
|
|
|
|
|
get_class( $this ) . ' apparently needsDB but is not in the Database group'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-22 18:34:32 +00:00
|
|
|
$needsResetDB = false;
|
2016-05-18 09:19:20 +00:00
|
|
|
if ( !self::$dbSetup || $this->needsDB() ) {
|
2012-11-13 16:48:15 +00:00
|
|
|
// set up a DB connection for this test to use
|
|
|
|
|
|
|
|
|
|
self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
|
2013-02-14 11:22:13 +00:00
|
|
|
self::$reuseDB = $this->getCliArg( 'reuse-db' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 20:42:39 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 21:10:36 +00:00
|
|
|
$this->checkDbIsSupported();
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( !self::$dbSetup ) {
|
2016-03-03 07:30:24 +00:00
|
|
|
$this->setupAllTestDBs();
|
2016-03-07 17:26:25 +00:00
|
|
|
$this->addCoreDBData();
|
2011-05-24 21:22:36 +00:00
|
|
|
}
|
2016-03-07 17:26:25 +00:00
|
|
|
|
|
|
|
|
// TODO: the DB setup should be done in setUpBeforeClass(), so the test DB
|
2016-03-03 07:30:24 +00:00
|
|
|
// is available in subclass's setUpBeforeClass() and setUp() methods.
|
2016-03-07 17:26:25 +00:00
|
|
|
// This would also remove the need for the HACK that is oncePerClass().
|
|
|
|
|
if ( $this->oncePerClass() ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
$this->setUpSchema( $this->db );
|
2018-06-07 12:36:06 +00:00
|
|
|
$this->resetDB( $this->db, $this->tablesUsed );
|
2016-03-07 17:26:25 +00:00
|
|
|
$this->addDBDataOnce();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
$this->addDBData();
|
2012-11-22 18:34:32 +00:00
|
|
|
$needsResetDB = true;
|
|
|
|
|
}
|
2011-05-28 21:19:24 +00:00
|
|
|
|
2012-11-22 18:34:32 +00:00
|
|
|
parent::run( $result );
|
|
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( $needsResetDB ) {
|
2016-03-03 07:30:24 +00:00
|
|
|
$this->resetDB( $this->db, $this->tablesUsed );
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2018-08-30 11:35:25 +00:00
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
self::restoreMwServices();
|
2018-08-30 11:35:25 +00:00
|
|
|
$this->localServices = null;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-03-05 15:44:28 +00:00
|
|
|
|
2016-03-07 17:26:25 +00:00
|
|
|
/**
|
2016-03-24 08:44:09 +00:00
|
|
|
* @return bool
|
2016-03-07 17:26:25 +00:00
|
|
|
*/
|
|
|
|
|
private function oncePerClass() {
|
|
|
|
|
// Remember current test class in the database connection,
|
|
|
|
|
// so we know when we need to run addData.
|
|
|
|
|
|
|
|
|
|
$class = static::class;
|
|
|
|
|
|
|
|
|
|
$first = !isset( $this->db->_hasDataForTestClass )
|
|
|
|
|
|| $this->db->_hasDataForTestClass !== $class;
|
|
|
|
|
|
|
|
|
|
$this->db->_hasDataForTestClass = $class;
|
|
|
|
|
return $first;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function usesTemporaryTables() {
|
2013-03-18 15:09:18 +00:00
|
|
|
return self::$useTemporaryTables;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
/**
|
2014-03-05 14:41:41 +00:00
|
|
|
* Obtains a new temporary file name
|
2012-04-10 14:28:42 +00:00
|
|
|
*
|
|
|
|
|
* The obtained filename is enlisted to be removed upon tearDown
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @return string Absolute name of the temporary file
|
2012-04-10 14:28:42 +00:00
|
|
|
*/
|
|
|
|
|
protected function getNewTempFile() {
|
2017-03-07 02:14:14 +00:00
|
|
|
$fileName = tempnam( wfTempDir(), 'MW_PHPUnit_' . static::class . '_' );
|
2014-03-05 14:41:41 +00:00
|
|
|
$this->tmpFiles[] = $fileName;
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
return $fileName;
|
2012-04-10 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* obtains a new temporary directory
|
|
|
|
|
*
|
|
|
|
|
* The obtained directory is enlisted to be removed (recursively with all its contained
|
|
|
|
|
* files) upon tearDown.
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @return string Absolute name of the temporary directory
|
2012-04-10 14:28:42 +00:00
|
|
|
*/
|
|
|
|
|
protected function getNewTempDirectory() {
|
|
|
|
|
// Starting of with a temporary /file/.
|
2014-03-05 14:41:41 +00:00
|
|
|
$fileName = $this->getNewTempFile();
|
2012-04-10 14:28:42 +00:00
|
|
|
|
|
|
|
|
// Converting the temporary /file/ to a /directory/
|
|
|
|
|
// The following is not atomic, but at least we now have a single place,
|
|
|
|
|
// where temporary directory creation is bundled and can be improved
|
2014-03-05 14:41:41 +00:00
|
|
|
unlink( $fileName );
|
|
|
|
|
$this->assertTrue( wfMkdirParents( $fileName ) );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
return $fileName;
|
2012-04-10 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:59:55 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
2014-09-14 19:07:14 +00:00
|
|
|
$this->called['setUp'] = true;
|
2012-10-08 10:59:55 +00:00
|
|
|
|
2013-07-17 16:42:24 +00:00
|
|
|
$this->phpErrorLevel = intval( ini_get( 'error_reporting' ) );
|
|
|
|
|
|
2012-10-08 10:59:55 +00:00
|
|
|
// Cleaning up temporary files
|
2014-03-05 14:41:41 +00:00
|
|
|
foreach ( $this->tmpFiles as $fileName ) {
|
|
|
|
|
if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
|
|
|
|
|
unlink( $fileName );
|
|
|
|
|
} elseif ( is_dir( $fileName ) ) {
|
|
|
|
|
wfRecursiveRemoveDir( $fileName );
|
2012-10-08 10:59:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->needsDB() && $this->db ) {
|
2012-12-05 16:13:21 +00:00
|
|
|
// Clean up open transactions
|
2013-02-14 11:22:13 +00:00
|
|
|
while ( $this->db->trxLevel() > 0 ) {
|
2015-10-21 19:13:22 +00:00
|
|
|
$this->db->rollback( __METHOD__, 'flush' );
|
2012-10-08 10:59:55 +00:00
|
|
|
}
|
2016-09-02 16:57:40 +00:00
|
|
|
// Check for unsafe queries
|
|
|
|
|
if ( $this->db->getType() === 'mysql' ) {
|
2018-08-28 02:39:33 +00:00
|
|
|
$this->db->query( "SET sql_mode = 'STRICT_ALL_TABLES'", __METHOD__ );
|
2016-09-02 16:57:40 +00:00
|
|
|
}
|
2012-10-08 10:59:55 +00:00
|
|
|
}
|
2012-11-22 18:34:32 +00:00
|
|
|
|
2018-04-23 23:46:11 +00:00
|
|
|
// Reset all caches between tests.
|
2018-09-11 17:01:08 +00:00
|
|
|
self::resetNonServiceCaches();
|
2014-11-03 21:40:49 +00:00
|
|
|
|
2016-09-01 16:43:01 +00:00
|
|
|
// XXX: reset maintenance triggers
|
|
|
|
|
// Hook into period lag checks which often happen in long-running scripts
|
2018-08-30 11:35:25 +00:00
|
|
|
$lbFactory = $this->localServices->getDBLoadBalancerFactory();
|
|
|
|
|
Maintenance::setLBFactoryTriggers( $lbFactory, $this->localServices->getMainConfig() );
|
2016-09-01 16:43:01 +00:00
|
|
|
|
2015-08-21 16:01:10 +00:00
|
|
|
ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' );
|
2012-10-08 10:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-11 01:42:33 +00:00
|
|
|
protected function addTmpFiles( $files ) {
|
|
|
|
|
$this->tmpFiles = array_merge( $this->tmpFiles, (array)$files );
|
|
|
|
|
}
|
2012-11-22 18:34:32 +00:00
|
|
|
|
2015-02-11 01:42:33 +00:00
|
|
|
protected function tearDown() {
|
2016-09-02 16:57:40 +00:00
|
|
|
global $wgRequest, $wgSQLMode;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2015-08-21 16:01:10 +00:00
|
|
|
$status = ob_get_status();
|
2015-09-26 11:26:35 +00:00
|
|
|
if ( isset( $status['name'] ) &&
|
|
|
|
|
$status['name'] === 'MediaWikiTestCase::wfResetOutputBuffersBarrier'
|
|
|
|
|
) {
|
2015-08-21 16:01:10 +00:00
|
|
|
ob_end_flush();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 19:07:14 +00:00
|
|
|
$this->called['tearDown'] = true;
|
2012-08-27 12:38:25 +00:00
|
|
|
// Cleaning up temporary files
|
2014-03-05 14:41:41 +00:00
|
|
|
foreach ( $this->tmpFiles as $fileName ) {
|
|
|
|
|
if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
|
|
|
|
|
unlink( $fileName );
|
|
|
|
|
} elseif ( is_dir( $fileName ) ) {
|
|
|
|
|
wfRecursiveRemoveDir( $fileName );
|
2012-04-10 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:59:55 +00:00
|
|
|
if ( $this->needsDB() && $this->db ) {
|
2012-12-05 16:13:21 +00:00
|
|
|
// Clean up open transactions
|
2013-02-14 11:22:13 +00:00
|
|
|
while ( $this->db->trxLevel() > 0 ) {
|
2015-10-21 19:13:22 +00:00
|
|
|
$this->db->rollback( __METHOD__, 'flush' );
|
2012-08-27 12:38:25 +00:00
|
|
|
}
|
2016-09-02 16:57:40 +00:00
|
|
|
if ( $this->db->getType() === 'mysql' ) {
|
2018-08-28 02:39:33 +00:00
|
|
|
$this->db->query( "SET sql_mode = " . $this->db->addQuotes( $wgSQLMode ),
|
|
|
|
|
__METHOD__ );
|
2016-09-02 16:57:40 +00:00
|
|
|
}
|
2012-08-27 12:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-21 15:20:36 +00:00
|
|
|
// Re-enable any disabled deprecation warnings
|
|
|
|
|
MWDebug::clearLog();
|
2012-10-08 10:59:55 +00:00
|
|
|
// Restore mw globals
|
|
|
|
|
foreach ( $this->mwGlobals as $key => $value ) {
|
|
|
|
|
$GLOBALS[$key] = $value;
|
|
|
|
|
}
|
2016-11-18 13:55:21 +00:00
|
|
|
foreach ( $this->mwGlobalsToUnset as $value ) {
|
|
|
|
|
unset( $GLOBALS[$value] );
|
|
|
|
|
}
|
2018-08-01 12:40:47 +00:00
|
|
|
if (
|
|
|
|
|
array_key_exists( 'wgExtraNamespaces', $this->mwGlobals ) ||
|
|
|
|
|
in_array( 'wgExtraNamespaces', $this->mwGlobalsToUnset )
|
|
|
|
|
) {
|
|
|
|
|
$this->resetNamespaces();
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mwGlobals = [];
|
2016-11-18 13:55:21 +00:00
|
|
|
$this->mwGlobalsToUnset = [];
|
2016-02-07 08:12:10 +00:00
|
|
|
$this->restoreLoggers();
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
// TODO: move global state into MediaWikiServices
|
2014-06-27 17:59:47 +00:00
|
|
|
RequestContext::resetMain();
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( session_id() !== '' ) {
|
|
|
|
|
session_write_close();
|
|
|
|
|
session_id( '' );
|
|
|
|
|
}
|
|
|
|
|
$wgRequest = new FauxRequest();
|
|
|
|
|
MediaWiki\Session\SessionManager::resetCache();
|
2015-11-22 20:17:00 +00:00
|
|
|
MediaWiki\Auth\AuthManager::resetCache();
|
2012-10-08 10:59:55 +00:00
|
|
|
|
2013-07-17 16:42:24 +00:00
|
|
|
$phpErrorLevel = intval( ini_get( 'error_reporting' ) );
|
|
|
|
|
|
|
|
|
|
if ( $phpErrorLevel !== $this->phpErrorLevel ) {
|
|
|
|
|
ini_set( 'error_reporting', $this->phpErrorLevel );
|
|
|
|
|
|
|
|
|
|
$oldHex = strtoupper( dechex( $this->phpErrorLevel ) );
|
|
|
|
|
$newHex = strtoupper( dechex( $phpErrorLevel ) );
|
2014-04-24 09:57:41 +00:00
|
|
|
$message = "PHP error_reporting setting was left dirty: "
|
|
|
|
|
. "was 0x$oldHex before test, 0x$newHex after test!";
|
2013-07-17 16:42:24 +00:00
|
|
|
|
|
|
|
|
$this->fail( $message );
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-22 23:53:51 +00:00
|
|
|
/**
|
|
|
|
|
* Make sure MediaWikiTestCase extending classes have called their
|
|
|
|
|
* parent setUp method
|
2016-12-11 20:16:11 +00:00
|
|
|
*
|
|
|
|
|
* With strict coverage activated in PHP_CodeCoverage, this test would be
|
|
|
|
|
* marked as risky without the following annotation (T152923).
|
|
|
|
|
* @coversNothing
|
2012-10-22 23:53:51 +00:00
|
|
|
*/
|
|
|
|
|
final public function testMediaWikiTestCaseParentSetupCalled() {
|
|
|
|
|
$this->assertArrayHasKey( 'setUp', $this->called,
|
2016-02-25 20:00:22 +00:00
|
|
|
static::class . '::setUp() must call parent::setUp()'
|
2012-10-22 23:53:51 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
|
|
|
|
* Sets a service, maintaining a stashed version of the previous service to be
|
|
|
|
|
* restored in tearDown
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param object $object
|
|
|
|
|
*/
|
|
|
|
|
protected function setService( $name, $object ) {
|
2018-08-30 11:35:25 +00:00
|
|
|
if ( !$this->localServices ) {
|
|
|
|
|
throw new Exception( __METHOD__ . ' must be called after MediaWikiTestCase::run()' );
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
if ( $this->localServices !== MediaWikiServices::getInstance() ) {
|
|
|
|
|
throw new Exception( __METHOD__ . ' will not work because the global MediaWikiServices '
|
|
|
|
|
. 'instance has been replaced by test code.' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 11:35:25 +00:00
|
|
|
$this->localServices->disableService( $name );
|
|
|
|
|
$this->localServices->redefineService(
|
2016-05-01 19:29:11 +00:00
|
|
|
$name,
|
|
|
|
|
function () use ( $object ) {
|
|
|
|
|
return $object;
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-07-25 14:37:16 +00:00
|
|
|
|
|
|
|
|
if ( $name === 'ContentLanguage' ) {
|
|
|
|
|
$this->doSetMwGlobals( [ 'wgContLang' => $object ] );
|
|
|
|
|
}
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:59:55 +00:00
|
|
|
/**
|
2014-03-05 20:19:16 +00:00
|
|
|
* Sets a global, maintaining a stashed version of the previous global to be
|
|
|
|
|
* restored in tearDown
|
|
|
|
|
*
|
|
|
|
|
* The key is added to the array of globals that will be reset afterwards
|
|
|
|
|
* in the tearDown().
|
2012-10-08 10:59:55 +00:00
|
|
|
*
|
2017-07-18 01:24:32 +00:00
|
|
|
* @par Example
|
|
|
|
|
* @code
|
2012-10-08 10:59:55 +00:00
|
|
|
* protected function setUp() {
|
|
|
|
|
* $this->setMwGlobals( 'wgRestrictStuff', true );
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* function testFoo() {}
|
|
|
|
|
*
|
|
|
|
|
* function testBar() {}
|
|
|
|
|
* $this->assertTrue( self::getX()->doStuff() );
|
|
|
|
|
*
|
|
|
|
|
* $this->setMwGlobals( 'wgRestrictStuff', false );
|
|
|
|
|
* $this->assertTrue( self::getX()->doStuff() );
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* function testQuux() {}
|
2017-07-18 01:24:32 +00:00
|
|
|
* @endcode
|
2012-10-08 10:59:55 +00:00
|
|
|
*
|
|
|
|
|
* @param array|string $pairs Key to the global variable, or an array
|
|
|
|
|
* of key/value pairs.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param mixed|null $value Value to set the global to (ignored
|
2012-10-08 10:59:55 +00:00
|
|
|
* if an array is given as first argument).
|
2014-03-05 20:19:16 +00:00
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @note To allow changes to global variables to take effect on global service instances,
|
|
|
|
|
* call overrideMwServices().
|
|
|
|
|
*
|
2014-03-05 20:19:16 +00:00
|
|
|
* @since 1.21
|
2012-10-08 10:59:55 +00:00
|
|
|
*/
|
|
|
|
|
protected function setMwGlobals( $pairs, $value = null ) {
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( is_string( $pairs ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$pairs = [ $pairs => $value ];
|
2012-10-25 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-25 14:37:16 +00:00
|
|
|
if ( isset( $pairs['wgContLang'] ) ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
'No setting $wgContLang, use setContentLang() or setService( \'ContentLanguage\' )'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->doSetMwGlobals( $pairs, $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An internal method that allows setService() to set globals that tests are not supposed to
|
|
|
|
|
* touch.
|
|
|
|
|
*/
|
|
|
|
|
private function doSetMwGlobals( $pairs, $value = null ) {
|
2014-03-05 20:19:16 +00:00
|
|
|
$this->stashMwGlobals( array_keys( $pairs ) );
|
|
|
|
|
|
2012-10-25 15:29:10 +00:00
|
|
|
foreach ( $pairs as $key => $value ) {
|
2014-03-05 20:19:16 +00:00
|
|
|
$GLOBALS[$key] = $value;
|
|
|
|
|
}
|
2018-08-01 12:40:47 +00:00
|
|
|
|
|
|
|
|
if ( array_key_exists( 'wgExtraNamespaces', $pairs ) ) {
|
|
|
|
|
$this->resetNamespaces();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Must be called whenever namespaces are changed, e.g., $wgExtraNamespaces is altered.
|
|
|
|
|
* Otherwise old namespace data will lurk and cause bugs.
|
|
|
|
|
*/
|
|
|
|
|
private function resetNamespaces() {
|
2018-08-30 11:35:25 +00:00
|
|
|
if ( !$this->localServices ) {
|
|
|
|
|
throw new Exception( __METHOD__ . ' must be called after MediaWikiTestCase::run()' );
|
|
|
|
|
}
|
2018-09-11 17:01:08 +00:00
|
|
|
|
|
|
|
|
if ( $this->localServices !== MediaWikiServices::getInstance() ) {
|
|
|
|
|
throw new Exception( __METHOD__ . ' will not work because the global MediaWikiServices '
|
|
|
|
|
. 'instance has been replaced by test code.' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 12:40:47 +00:00
|
|
|
MWNamespace::clearCaches();
|
|
|
|
|
Language::clearCaches();
|
|
|
|
|
|
|
|
|
|
// We can't have the TitleFormatter holding on to an old Language object either
|
|
|
|
|
// @todo We shouldn't need to reset all the aliases here.
|
2018-08-30 11:35:25 +00:00
|
|
|
$this->localServices->resetServiceForTesting( 'TitleFormatter' );
|
|
|
|
|
$this->localServices->resetServiceForTesting( 'TitleParser' );
|
|
|
|
|
$this->localServices->resetServiceForTesting( '_MediaWikiTitleCodec' );
|
2014-03-05 20:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-11 11:12:46 +00:00
|
|
|
/**
|
|
|
|
|
* Check if we can back up a value by performing a shallow copy.
|
|
|
|
|
* Values which fail this test are copied recursively.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return bool True if a shallow copy will do; false if a deep copy
|
|
|
|
|
* is required.
|
|
|
|
|
*/
|
|
|
|
|
private static function canShallowCopy( $value ) {
|
|
|
|
|
if ( is_scalar( $value ) || $value === null ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
foreach ( $value as $subValue ) {
|
|
|
|
|
if ( !is_scalar( $subValue ) && $subValue !== null ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 20:19:16 +00:00
|
|
|
/**
|
|
|
|
|
* Stashes the global, will be restored in tearDown()
|
|
|
|
|
*
|
|
|
|
|
* Individual test functions may override globals through the setMwGlobals() function
|
|
|
|
|
* or directly. When directly overriding globals their keys should first be passed to this
|
|
|
|
|
* method in setUp to avoid breaking global state for other tests
|
|
|
|
|
*
|
|
|
|
|
* That way all other tests are executed with the same settings (instead of using the
|
|
|
|
|
* unreliable local settings for most tests and fix it only for some tests).
|
|
|
|
|
*
|
|
|
|
|
* @param array|string $globalKeys Key to the global variable, or an array of keys.
|
|
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @note To allow changes to global variables to take effect on global service instances,
|
|
|
|
|
* call overrideMwServices().
|
|
|
|
|
*
|
2014-03-05 20:19:16 +00:00
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
protected function stashMwGlobals( $globalKeys ) {
|
|
|
|
|
if ( is_string( $globalKeys ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$globalKeys = [ $globalKeys ];
|
2014-03-05 20:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $globalKeys as $globalKey ) {
|
2012-10-25 15:29:10 +00:00
|
|
|
// NOTE: make sure we only save the global once or a second call to
|
|
|
|
|
// setMwGlobals() on the same global would override the original
|
|
|
|
|
// value.
|
2016-11-18 13:55:21 +00:00
|
|
|
if (
|
|
|
|
|
!array_key_exists( $globalKey, $this->mwGlobals ) &&
|
|
|
|
|
!array_key_exists( $globalKey, $this->mwGlobalsToUnset )
|
|
|
|
|
) {
|
2014-03-05 20:25:49 +00:00
|
|
|
if ( !array_key_exists( $globalKey, $GLOBALS ) ) {
|
2016-11-18 13:55:21 +00:00
|
|
|
$this->mwGlobalsToUnset[$globalKey] = $globalKey;
|
|
|
|
|
continue;
|
2014-03-05 20:25:49 +00:00
|
|
|
}
|
2014-03-05 13:25:07 +00:00
|
|
|
// NOTE: we serialize then unserialize the value in case it is an object
|
|
|
|
|
// this stops any objects being passed by reference. We could use clone
|
|
|
|
|
// and if is_object but this does account for objects within objects!
|
2016-05-11 11:12:46 +00:00
|
|
|
if ( self::canShallowCopy( $GLOBALS[$globalKey] ) ) {
|
2014-03-05 20:19:16 +00:00
|
|
|
$this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
|
2016-05-11 11:12:46 +00:00
|
|
|
} elseif (
|
|
|
|
|
// Many MediaWiki types are safe to clone. These are the
|
|
|
|
|
// ones that are most commonly stashed.
|
|
|
|
|
$GLOBALS[$globalKey] instanceof Language ||
|
|
|
|
|
$GLOBALS[$globalKey] instanceof User ||
|
|
|
|
|
$GLOBALS[$globalKey] instanceof FauxRequest
|
|
|
|
|
) {
|
|
|
|
|
$this->mwGlobals[$globalKey] = clone $GLOBALS[$globalKey];
|
2017-05-19 21:06:58 +00:00
|
|
|
} elseif ( $this->containsClosure( $GLOBALS[$globalKey] ) ) {
|
2017-05-19 18:50:58 +00:00
|
|
|
// Serializing Closure only gives a warning on HHVM while
|
|
|
|
|
// it throws an Exception on Zend.
|
|
|
|
|
// Workaround for https://github.com/facebook/hhvm/issues/6206
|
|
|
|
|
$this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
|
2016-05-11 11:12:46 +00:00
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
$this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
$this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
|
|
|
|
|
}
|
2014-03-05 19:50:20 +00:00
|
|
|
}
|
2012-10-08 10:59:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 21:06:58 +00:00
|
|
|
/**
|
|
|
|
|
* @param mixed $var
|
|
|
|
|
* @param int $maxDepth
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function containsClosure( $var, $maxDepth = 15 ) {
|
|
|
|
|
if ( $var instanceof Closure ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( !is_array( $var ) || $maxDepth === 0 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $var as $value ) {
|
|
|
|
|
if ( $this->containsClosure( $value, $maxDepth - 1 ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 09:34:24 +00:00
|
|
|
/**
|
|
|
|
|
* Merges the given values into a MW global array variable.
|
|
|
|
|
* Useful for setting some entries in a configuration array, instead of
|
|
|
|
|
* setting the entire array.
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @param string $name The name of the global, as in wgFooBar
|
|
|
|
|
* @param array $values The array containing the entries to set in that global
|
2012-10-09 09:34:24 +00:00
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @throws MWException If the designated global is not an array.
|
2014-03-05 20:19:16 +00:00
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @note To allow changes to global variables to take effect on global service instances,
|
|
|
|
|
* call overrideMwServices().
|
|
|
|
|
*
|
2014-03-05 20:19:16 +00:00
|
|
|
* @since 1.21
|
2012-10-09 09:34:24 +00:00
|
|
|
*/
|
|
|
|
|
protected function mergeMwGlobalArrayValue( $name, $values ) {
|
|
|
|
|
if ( !isset( $GLOBALS[$name] ) ) {
|
|
|
|
|
$merged = $values;
|
|
|
|
|
} else {
|
|
|
|
|
if ( !is_array( $GLOBALS[$name] ) ) {
|
|
|
|
|
throw new MWException( "MW global $name is not an array." );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
// NOTE: do not use array_merge, it screws up for numeric keys.
|
2012-10-09 09:34:24 +00:00
|
|
|
$merged = $GLOBALS[$name];
|
|
|
|
|
foreach ( $values as $k => $v ) {
|
|
|
|
|
$merged[$k] = $v;
|
|
|
|
|
}
|
2012-09-05 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-09 09:34:24 +00:00
|
|
|
$this->setMwGlobals( $name, $merged );
|
2012-04-10 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
|
|
|
|
* Stashes the global instance of MediaWikiServices, and installs a new one,
|
|
|
|
|
* allowing test cases to override settings and services.
|
|
|
|
|
* The previous instance of MediaWikiServices will be restored on tearDown.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param Config|null $configOverrides Configuration overrides for the new MediaWikiServices
|
|
|
|
|
* instance.
|
2016-05-01 19:29:11 +00:00
|
|
|
* @param callable[] $services An associative array of services to re-define. Keys are service
|
|
|
|
|
* names, values are callables.
|
|
|
|
|
*
|
|
|
|
|
* @return MediaWikiServices
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
2018-08-30 11:35:25 +00:00
|
|
|
protected function overrideMwServices(
|
2018-08-07 10:58:31 +00:00
|
|
|
Config $configOverrides = null, array $services = []
|
|
|
|
|
) {
|
2018-09-11 17:01:08 +00:00
|
|
|
$newInstance = self::installMockMwServices( $configOverrides );
|
|
|
|
|
|
|
|
|
|
if ( $this->localServices ) {
|
|
|
|
|
$this->localServices->destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->localServices = $newInstance;
|
|
|
|
|
|
|
|
|
|
foreach ( $services as $name => $callback ) {
|
|
|
|
|
$newInstance->redefineService( $name, $callback );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $newInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new "mock" MediaWikiServices instance, and installs it.
|
|
|
|
|
* This effectively resets all cached states in services, with the exception of
|
|
|
|
|
* the ConfigFactory and the DBLoadBalancerFactory service, which are inherited from
|
|
|
|
|
* the original MediaWikiServices.
|
|
|
|
|
*
|
|
|
|
|
* @note The new original MediaWikiServices instance can later be restored by calling
|
|
|
|
|
* restoreMwServices(). That original is determined by the first call to this method, or
|
|
|
|
|
* by setUpBeforeClass, whichever is called first. The caller is responsible for managing
|
|
|
|
|
* and, when appropriate, destroying any other MediaWikiServices instances that may get
|
|
|
|
|
* replaced when calling this method.
|
|
|
|
|
*
|
|
|
|
|
* @param Config|null $configOverrides Configuration overrides for the new MediaWikiServices
|
|
|
|
|
* instance.
|
|
|
|
|
*
|
|
|
|
|
* @return MediaWikiServices the new mock service locator.
|
|
|
|
|
*/
|
|
|
|
|
public static function installMockMwServices( Config $configOverrides = null ) {
|
|
|
|
|
// Make sure we have the original service locator
|
|
|
|
|
if ( !self::$originalServices ) {
|
|
|
|
|
self::$originalServices = MediaWikiServices::getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
if ( !$configOverrides ) {
|
|
|
|
|
$configOverrides = new HashConfig();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 11:35:25 +00:00
|
|
|
$oldConfigFactory = self::$originalServices->getConfigFactory();
|
|
|
|
|
$oldLoadBalancerFactory = self::$originalServices->getDBLoadBalancerFactory();
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
$testConfig = self::makeTestConfig( null, $configOverrides );
|
2018-09-11 17:01:08 +00:00
|
|
|
$newServices = new MediaWikiServices( $testConfig );
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
// Load the default wiring from the specified files.
|
|
|
|
|
// NOTE: this logic mirrors the logic in MediaWikiServices::newInstance.
|
|
|
|
|
$wiringFiles = $testConfig->get( 'ServiceWiringFiles' );
|
2018-09-11 17:01:08 +00:00
|
|
|
$newServices->loadWiringFiles( $wiringFiles );
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
// Provide a traditional hook point to allow extensions to configure services.
|
2018-09-11 17:01:08 +00:00
|
|
|
Hooks::run( 'MediaWikiServices', [ $newServices ] );
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
// Use bootstrap config for all configuration.
|
|
|
|
|
// This allows config overrides via global variables to take effect.
|
|
|
|
|
$bootstrapConfig = $newServices->getBootstrapConfig();
|
|
|
|
|
$newServices->resetServiceForTesting( 'ConfigFactory' );
|
|
|
|
|
$newServices->redefineService(
|
|
|
|
|
'ConfigFactory',
|
|
|
|
|
self::makeTestConfigFactoryInstantiator(
|
|
|
|
|
$oldConfigFactory,
|
|
|
|
|
[ 'main' => $bootstrapConfig ]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
|
|
|
|
|
$newServices->redefineService(
|
|
|
|
|
'DBLoadBalancerFactory',
|
|
|
|
|
function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
|
|
|
|
|
return $oldLoadBalancerFactory;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
MediaWikiServices::forceGlobalInstance( $newServices );
|
|
|
|
|
return $newServices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restores the original, non-mock MediaWikiServices instance.
|
|
|
|
|
* The previously active MediaWikiServices instance is destroyed,
|
|
|
|
|
* if it is different from the original that is to be restored.
|
|
|
|
|
*
|
|
|
|
|
* @note this if for internal use by test framework code. It should never be
|
|
|
|
|
* called from inside a test case, a data provider, or a setUp or tearDown method.
|
|
|
|
|
*
|
|
|
|
|
* @return bool true if the original service locator was restored,
|
|
|
|
|
* false if there was nothing too do.
|
|
|
|
|
*/
|
|
|
|
|
public static function restoreMwServices() {
|
|
|
|
|
if ( !self::$originalServices ) {
|
|
|
|
|
return false;
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
$currentServices = MediaWikiServices::getInstance();
|
2018-08-30 11:35:25 +00:00
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
if ( self::$originalServices === $currentServices ) {
|
|
|
|
|
return false;
|
2018-08-30 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
MediaWikiServices::forceGlobalInstance( self::$originalServices );
|
|
|
|
|
$currentServices->destroy();
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2018-09-11 17:01:08 +00:00
|
|
|
return true;
|
2016-05-01 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-09 16:47:58 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.27
|
|
|
|
|
* @param string|Language $lang
|
|
|
|
|
*/
|
|
|
|
|
public function setUserLang( $lang ) {
|
|
|
|
|
RequestContext::getMain()->setLanguage( $lang );
|
|
|
|
|
$this->setMwGlobals( 'wgLang', RequestContext::getMain()->getLanguage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.27
|
|
|
|
|
* @param string|Language $lang
|
|
|
|
|
*/
|
|
|
|
|
public function setContentLang( $lang ) {
|
|
|
|
|
if ( $lang instanceof Language ) {
|
|
|
|
|
$langCode = $lang->getCode();
|
|
|
|
|
$langObj = $lang;
|
|
|
|
|
} else {
|
|
|
|
|
$langCode = $lang;
|
|
|
|
|
$langObj = Language::factory( $langCode );
|
|
|
|
|
}
|
2018-07-25 14:37:16 +00:00
|
|
|
$this->setMwGlobals( 'wgLanguageCode', $langCode );
|
|
|
|
|
$this->setService( 'ContentLanguage', $langObj );
|
2016-03-09 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-26 14:38:41 +00:00
|
|
|
/**
|
|
|
|
|
* Alters $wgGroupPermissions for the duration of the test. Can be called
|
|
|
|
|
* with an array, like
|
|
|
|
|
* [ '*' => [ 'read' => false ], 'user' => [ 'read' => false ] ]
|
|
|
|
|
* or three values to set a single permission, like
|
|
|
|
|
* $this->setGroupPermissions( '*', 'read', false );
|
|
|
|
|
*
|
|
|
|
|
* @since 1.31
|
|
|
|
|
* @param array|string $newPerms Either an array of permissions to change,
|
|
|
|
|
* in which case the next two parameters are ignored; or a single string
|
|
|
|
|
* identifying a group, to use with the next two parameters.
|
|
|
|
|
* @param string|null $newKey
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param mixed|null $newValue
|
2018-03-26 14:38:41 +00:00
|
|
|
*/
|
|
|
|
|
public function setGroupPermissions( $newPerms, $newKey = null, $newValue = null ) {
|
|
|
|
|
global $wgGroupPermissions;
|
|
|
|
|
|
|
|
|
|
$this->stashMwGlobals( 'wgGroupPermissions' );
|
|
|
|
|
|
|
|
|
|
if ( is_string( $newPerms ) ) {
|
|
|
|
|
$newPerms = [ $newPerms => [ $newKey => $newValue ] ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $newPerms as $group => $permissions ) {
|
|
|
|
|
foreach ( $permissions as $key => $value ) {
|
|
|
|
|
$wgGroupPermissions[$group][$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-07 08:12:10 +00:00
|
|
|
/**
|
|
|
|
|
* Sets the logger for a specified channel, for the duration of the test.
|
|
|
|
|
* @since 1.27
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
*/
|
|
|
|
|
protected function setLogger( $channel, LoggerInterface $logger ) {
|
2016-05-01 19:29:11 +00:00
|
|
|
// TODO: Once loggers are managed by MediaWikiServices, use
|
|
|
|
|
// overrideMwServices() to set loggers.
|
|
|
|
|
|
2016-02-07 08:12:10 +00:00
|
|
|
$provider = LoggerFactory::getProvider();
|
|
|
|
|
$wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
|
|
|
|
|
$singletons = $wrappedProvider->singletons;
|
|
|
|
|
if ( $provider instanceof MonologSpi ) {
|
|
|
|
|
if ( !isset( $this->loggers[$channel] ) ) {
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->loggers[$channel] = $singletons['loggers'][$channel] ?? null;
|
2016-02-07 08:12:10 +00:00
|
|
|
}
|
|
|
|
|
$singletons['loggers'][$channel] = $logger;
|
|
|
|
|
} elseif ( $provider instanceof LegacySpi ) {
|
|
|
|
|
if ( !isset( $this->loggers[$channel] ) ) {
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->loggers[$channel] = $singletons[$channel] ?? null;
|
2016-02-07 08:12:10 +00:00
|
|
|
}
|
|
|
|
|
$singletons[$channel] = $logger;
|
|
|
|
|
} else {
|
|
|
|
|
throw new LogicException( __METHOD__ . ': setting a logger for ' . get_class( $provider )
|
|
|
|
|
. ' is not implemented' );
|
|
|
|
|
}
|
|
|
|
|
$wrappedProvider->singletons = $singletons;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restores loggers replaced by setLogger().
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
private function restoreLoggers() {
|
|
|
|
|
$provider = LoggerFactory::getProvider();
|
|
|
|
|
$wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
|
|
|
|
|
$singletons = $wrappedProvider->singletons;
|
|
|
|
|
foreach ( $this->loggers as $channel => $logger ) {
|
|
|
|
|
if ( $provider instanceof MonologSpi ) {
|
|
|
|
|
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;
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->loggers = [];
|
2016-02-07 08:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
public function dbPrefix() {
|
2018-07-04 15:20:31 +00:00
|
|
|
return self::getTestPrefixFor( $this->db );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param IDatabase $db
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*/
|
|
|
|
|
public static function getTestPrefixFor( IDatabase $db ) {
|
|
|
|
|
return $db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
|
2011-03-05 15:44:28 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
public function needsDB() {
|
2017-11-23 19:19:14 +00:00
|
|
|
// If the test says it uses database tables, it needs the database
|
2018-09-11 17:16:26 +00:00
|
|
|
return $this->tablesUsed || $this->isTestInDatabaseGroup();
|
|
|
|
|
}
|
2012-05-04 14:02:09 +00:00
|
|
|
|
2018-09-11 17:16:26 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*/
|
|
|
|
|
protected function isTestInDatabaseGroup() {
|
2017-11-23 19:19:14 +00:00
|
|
|
// If the test class says it belongs to the Database group, it needs the database.
|
|
|
|
|
// NOTE: This ONLY checks for the group in the class level doc comment.
|
2010-12-29 15:52:07 +00:00
|
|
|
$rc = new ReflectionClass( $this );
|
2018-09-11 17:16:26 +00:00
|
|
|
return (bool)preg_match( '/@group +Database/im', $rc->getDocComment() );
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-01-10 18:34:59 +00:00
|
|
|
|
2014-09-18 00:02:24 +00:00
|
|
|
/**
|
|
|
|
|
* Insert a new page.
|
|
|
|
|
*
|
|
|
|
|
* Should be called from addDBData().
|
|
|
|
|
*
|
2014-06-02 18:34:52 +00:00
|
|
|
* @since 1.25 ($namespace in 1.28)
|
2018-06-12 18:20:49 +00:00
|
|
|
* @param string|Title $pageName Page name or title
|
2014-09-18 00:02:24 +00:00
|
|
|
* @param string $text Page's content
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param int|null $namespace Namespace id (name cannot already contain namespace)
|
|
|
|
|
* @param User|null $user If null, static::getTestSysop()->getUser() is used.
|
2014-09-18 00:02:24 +00:00
|
|
|
* @return array Title object and page id
|
2018-09-14 18:30:30 +00:00
|
|
|
* @throws MWException If this test cases's needsDB() method doesn't return true.
|
|
|
|
|
* Test cases can use "@group Database" to enable database test support,
|
|
|
|
|
* or list the tables under testing in $this->tablesUsed, or override the
|
|
|
|
|
* needsDB() method.
|
2014-09-18 00:02:24 +00:00
|
|
|
*/
|
2014-06-02 18:34:52 +00:00
|
|
|
protected function insertPage(
|
|
|
|
|
$pageName,
|
|
|
|
|
$text = 'Sample page for unit test.',
|
2018-06-29 19:56:22 +00:00
|
|
|
$namespace = null,
|
|
|
|
|
User $user = null
|
2014-06-02 18:34:52 +00:00
|
|
|
) {
|
2018-09-14 18:30:30 +00:00
|
|
|
if ( !$this->needsDB() ) {
|
|
|
|
|
throw new MWException( 'When testing which pages, the test cases\'s needsDB()' .
|
|
|
|
|
' method should return true. Use @group Database or $this->tablesUsed.' );
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 18:34:52 +00:00
|
|
|
if ( is_string( $pageName ) ) {
|
|
|
|
|
$title = Title::newFromText( $pageName, $namespace );
|
|
|
|
|
} else {
|
|
|
|
|
$title = $pageName;
|
|
|
|
|
}
|
2014-09-18 00:02:24 +00:00
|
|
|
|
2018-06-29 19:56:22 +00:00
|
|
|
if ( !$user ) {
|
|
|
|
|
$user = static::getTestSysop()->getUser();
|
|
|
|
|
}
|
2014-09-18 00:02:24 +00:00
|
|
|
$comment = __METHOD__ . ': Sample page for unit test.';
|
|
|
|
|
|
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
$page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 00:02:24 +00:00
|
|
|
'title' => $title,
|
|
|
|
|
'id' => $page->getId(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-09-18 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-10 18:34:59 +00:00
|
|
|
/**
|
2016-03-07 17:26:25 +00:00
|
|
|
* Stub. If a test suite needs to add additional data to the database, it should
|
|
|
|
|
* implement this method and do so. This method is called once per test suite
|
|
|
|
|
* (i.e. once per class).
|
|
|
|
|
*
|
|
|
|
|
* Note data added by this method may be removed by resetDB() depending on
|
|
|
|
|
* the contents of $tablesUsed.
|
|
|
|
|
*
|
|
|
|
|
* To add additional data between test function runs, override prepareDB().
|
|
|
|
|
*
|
|
|
|
|
* @see addDBData()
|
|
|
|
|
* @see resetDB()
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public function addDBDataOnce() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stub. Subclasses may override this to prepare the database.
|
|
|
|
|
* Called before every test run (test function or data set).
|
|
|
|
|
*
|
|
|
|
|
* @see addDBDataOnce()
|
|
|
|
|
* @see resetDB()
|
2014-03-05 14:41:41 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.18
|
2011-01-10 18:34:59 +00:00
|
|
|
*/
|
2014-03-05 14:41:41 +00:00
|
|
|
public function addDBData() {
|
2013-04-26 12:00:22 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-06-14 07:46:18 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*/
|
2018-06-12 16:36:34 +00:00
|
|
|
protected function addCoreDBData() {
|
2011-11-10 13:29:32 +00:00
|
|
|
if ( $this->db->getType() == 'oracle' ) {
|
|
|
|
|
# Insert 0 user to prevent FK violations
|
|
|
|
|
# Anonymous user
|
2016-05-18 09:19:20 +00:00
|
|
|
if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
|
|
|
|
|
$this->db->insert( 'user', [
|
|
|
|
|
'user_id' => 0,
|
|
|
|
|
'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
|
|
|
|
|
}
|
2011-11-10 13:29:32 +00:00
|
|
|
|
|
|
|
|
# Insert 0 page to prevent FK violations
|
|
|
|
|
# Blank page
|
2016-05-18 09:19:20 +00:00
|
|
|
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' ] );
|
|
|
|
|
}
|
2011-11-10 13:29:32 +00:00
|
|
|
}
|
2011-01-25 16:32:43 +00:00
|
|
|
|
2018-01-31 21:39:44 +00:00
|
|
|
SiteStatsInit::doPlaceholderInit();
|
|
|
|
|
|
2011-01-25 16:32:43 +00:00
|
|
|
User::resetIdByNameCache();
|
|
|
|
|
|
2014-09-18 00:02:24 +00:00
|
|
|
// Make sysop user
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2014-09-18 00:02:24 +00:00
|
|
|
// Make 1 page with 1 revision
|
2011-12-16 18:55:20 +00:00
|
|
|
$page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
|
2014-03-10 02:04:53 +00:00
|
|
|
if ( $page->getId() == 0 ) {
|
2012-08-28 13:58:36 +00:00
|
|
|
$page->doEditContent(
|
|
|
|
|
new WikitextContent( 'UTContent' ),
|
|
|
|
|
'UTPageSummary',
|
2017-05-29 21:58:33 +00:00
|
|
|
EDIT_NEW | EDIT_SUPPRESS_RC,
|
2012-08-28 13:58:36 +00:00
|
|
|
false,
|
2015-03-23 05:36:20 +00:00
|
|
|
$user
|
2014-09-18 00:02:24 +00:00
|
|
|
);
|
2017-05-29 21:58:33 +00:00
|
|
|
// an edit always attempt to purge backlink links such as history
|
2018-08-15 23:33:12 +00:00
|
|
|
// pages. That is unnecessary.
|
2017-05-29 21:58:33 +00:00
|
|
|
JobQueueGroup::singleton()->get( 'htmlCacheUpdate' )->delete();
|
|
|
|
|
// WikiPages::doEditUpdates randomly adds RC purges
|
|
|
|
|
JobQueueGroup::singleton()->get( 'recentChangesUpdate' )->delete();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// doEditContent() probably started the session via
|
|
|
|
|
// User::loadFromSession(). Close it now.
|
|
|
|
|
if ( session_id() !== '' ) {
|
|
|
|
|
session_write_close();
|
|
|
|
|
session_id( '' );
|
|
|
|
|
}
|
2012-01-25 23:11:53 +00:00
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Restores MediaWiki to using the table set (table prefix) it was using before
|
|
|
|
|
* setupTestDB() was called. Useful if we need to perform database operations
|
|
|
|
|
* after the test run has finished (such as saving logs or profiling info).
|
2014-03-05 14:41:41 +00:00
|
|
|
*
|
2018-04-23 23:46:11 +00:00
|
|
|
* This is called by phpunit/bootstrap.php after the last test.
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.21
|
2012-11-13 16:48:15 +00:00
|
|
|
*/
|
|
|
|
|
public static function teardownTestDB() {
|
2015-12-11 23:45:05 +00:00
|
|
|
global $wgJobClasses;
|
|
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
if ( !self::$dbSetup ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-29 00:14:18 +00:00
|
|
|
Hooks::run( 'UnitTestsBeforeDatabaseTeardown' );
|
|
|
|
|
|
2015-12-11 23:45:05 +00:00
|
|
|
foreach ( $wgJobClasses as $type => $class ) {
|
|
|
|
|
// Delete any jobs under the clone DB (or old prefix in other stores)
|
|
|
|
|
JobQueueGroup::singleton()->get( $type )->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
CloneDatabase::changePrefix( self::$oldTablePrefix );
|
|
|
|
|
|
|
|
|
|
self::$oldTablePrefix = false;
|
|
|
|
|
self::$dbSetup = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 15:20:31 +00:00
|
|
|
/**
|
|
|
|
|
* Setups a database with cloned tables using the given prefix.
|
2016-03-03 07:30:24 +00:00
|
|
|
*
|
|
|
|
|
* If reuseDB is true and certain conditions apply, it will just change the prefix.
|
|
|
|
|
* Otherwise, it will clone the tables and change the prefix.
|
|
|
|
|
*
|
2016-11-28 18:26:14 +00:00
|
|
|
* @param IMaintainableDatabase $db Database to use
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $prefix Prefix to use for test tables. If not given, the prefix is determined
|
2018-07-04 15:20:31 +00:00
|
|
|
* automatically for $db.
|
2016-03-03 07:30:24 +00:00
|
|
|
* @return bool True if tables were cloned, false if only the prefix was changed
|
|
|
|
|
*/
|
2018-07-04 15:20:31 +00:00
|
|
|
protected static function setupDatabaseWithTestPrefix(
|
|
|
|
|
IMaintainableDatabase $db,
|
|
|
|
|
$prefix = null
|
|
|
|
|
) {
|
|
|
|
|
if ( $prefix === null ) {
|
|
|
|
|
$prefix = self::getTestPrefixFor( $db );
|
|
|
|
|
}
|
2017-11-16 16:48:25 +00:00
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
if ( ( $db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
|
2018-07-04 15:20:31 +00:00
|
|
|
$db->tablePrefix( $prefix );
|
2016-03-03 07:30:24 +00:00
|
|
|
return false;
|
2018-07-04 15:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $db->_originalTablePrefix ) ) {
|
|
|
|
|
$oldPrefix = $db->tablePrefix();
|
|
|
|
|
|
|
|
|
|
if ( $oldPrefix === $prefix ) {
|
|
|
|
|
// table already has the correct prefix, but presumably no cloned tables
|
|
|
|
|
$oldPrefix = self::$oldTablePrefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db->tablePrefix( $oldPrefix );
|
|
|
|
|
$tablesCloned = self::listTables( $db );
|
|
|
|
|
$dbClone = new CloneDatabase( $db, $tablesCloned, $prefix, $oldPrefix );
|
|
|
|
|
$dbClone->useTemporaryTables( self::$useTemporaryTables );
|
|
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
$dbClone->cloneTableStructure();
|
2018-07-04 15:20:31 +00:00
|
|
|
|
|
|
|
|
$db->tablePrefix( $prefix );
|
|
|
|
|
$db->_originalTablePrefix = $oldPrefix;
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
2018-07-04 15:20:31 +00:00
|
|
|
|
|
|
|
|
return true;
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up all test DBs
|
|
|
|
|
*/
|
|
|
|
|
public function setupAllTestDBs() {
|
|
|
|
|
global $wgDBprefix;
|
|
|
|
|
|
|
|
|
|
self::$oldTablePrefix = $wgDBprefix;
|
|
|
|
|
|
|
|
|
|
$testPrefix = $this->dbPrefix();
|
|
|
|
|
|
|
|
|
|
// switch to a temporary clone of the database
|
|
|
|
|
self::setupTestDB( $this->db, $testPrefix );
|
|
|
|
|
|
|
|
|
|
if ( self::isUsingExternalStoreDB() ) {
|
|
|
|
|
self::setupExternalStoreTestDBs( $testPrefix );
|
|
|
|
|
}
|
2018-07-04 15:20:31 +00:00
|
|
|
|
|
|
|
|
// NOTE: Change the prefix in the LBFactory and $wgDBprefix, to prevent
|
|
|
|
|
// *any* database connections to operate on live data.
|
|
|
|
|
CloneDatabase::changePrefix( $testPrefix );
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Creates an empty skeleton of the wiki database by cloning its structure
|
|
|
|
|
* to equivalent tables using the given $prefix. Then sets MediaWiki to
|
|
|
|
|
* use the new set of tables (aka schema) instead of the original set.
|
|
|
|
|
*
|
|
|
|
|
* This is used to generate a dummy table set, typically consisting of temporary
|
|
|
|
|
* tables, that will be used by tests instead of the original wiki database tables.
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*
|
2014-07-24 09:30:25 +00:00
|
|
|
* @note the original table prefix is stored in self::$oldTablePrefix. This is used
|
2012-11-13 16:48:15 +00:00
|
|
|
* by teardownTestDB() to return the wiki to using the original table set.
|
|
|
|
|
*
|
2014-07-24 09:30:25 +00:00
|
|
|
* @note this method only works when first called. Subsequent calls have no effect,
|
2012-11-13 16:48:15 +00:00
|
|
|
* even if using different parameters.
|
|
|
|
|
*
|
2016-09-26 22:40:07 +00:00
|
|
|
* @param Database $db The database connection
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $prefix The prefix to use for the new table set (aka schema).
|
2012-11-13 16:48:15 +00:00
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @throws MWException If the database table prefix is already $prefix
|
2012-11-13 16:48:15 +00:00
|
|
|
*/
|
2016-09-26 22:40:07 +00:00
|
|
|
public static function setupTestDB( Database $db, $prefix ) {
|
Refactor parser tests
Merge the PHPUnit parser test runner with the old parserTests.inc,
taking the good bits of both. Reviewed, pared down and documented the
setup code. parserTests.php is now a frontend to a fully featured
parser test system, with lots of developer options, whereas PHPUnit
provides a simpler interface with increased isolation between test
cases.
Performance of both frontends is much improved, perhaps 2x faster for
parserTests.php and 10x faster for PHPUnit.
General:
* Split out the pre-Setup.php global variable configuration from
phpunit.php into a new class called TestSetup, also called it from
parserTests.php.
* Factored out the setup of TestsAutoLoader into a static method in
Maintenance.
* In Setup.php improved "caches" debug output.
PHPUnit frontend:
* Delete the entire contents of NewParserTest and replace it with a
small wrapper around ParserTestRunner. It doesn't inherit from
MediaWikiTestCase anymore since integrating the setup code was an
unnecessary complication.
* Rename MediaWikiParserTest to ParserTestTopLevelSuite and made it an
instantiable TestSuite class instead of just a static method. Got rid
of the eval(), just construct TestCase objects directly with a
specified name, it works just as well.
* Introduce ParserTestFileSuite for per-file setup.
* Remove parser-related options from phpunit.php, since we don't
support them anymore. Note that --filter now works just as well as
--regex used to.
* Add CoreParserTestSuite, equivalent to ExtensionsParserTestSuite,
for clarity.
* Make it possible to call MediaWikiTestCase::setupTestDB() more than
once, as is implied by the documentation.
parserTests.php frontend:
* Made parserTests.php into a Maintenance subclass, moved CLI-specific
code to it.
* Renamed ParserTest to ParserTestRunner, this is now the generic
backend.
* Add --upload-dir option which sets up an FSFileBackend, similar
to the old default behaviour
Test file reading and interpretation:
* Rename TestFileIterator to TestFileReader, and make it read and buffer
an entire file, instead of iterating.
* The previous code had an associative array representation of test
specifications. Used this form more widely to pass around test data.
* Remove the idea of !!hooks copying hooks from $wgParser, this is
unnecessary now that all extensions use ParserFirstCallInit. Resurrect
an old interpretation of the feature which was accidentally broken: if
a named hook does not exist, skip all tests in the file.
* Got rid of the "subtest" idea for tidy variants, instead use a
human-readable description that appears in the output.
* When all tests in a file are filtered or skipped, don't create the
articles in them. This greatly speeds up execution time when --regex
matches a small number of tests. It may possibly break extensions, but
they would have been randomly broken anyway since there is no
guarantee of test file execution order.
* Remove integrated testing of OutputPage::addCategoryLinks() category
link formatting, life is complicated enough already. It can go in
OutputPageTest if that's a thing we really need.
Result recording and display:
* Make TestRecorder into a generic plugin interface for progress output
etc., which needs to be abstracted for PHPUnit integration.
* Introduce MultiTestRecorder for recorder chaining, instead of using
a long inheritance chain. All test recorders now directly inherit from
TestRecorder.
* Move all console-related code to the new ParserTestPrinter.
* Introduce PhpunitTestRecorder, which is the recorder for the PHPUnit
frontend. Most events are ignored since they are never emitted in the
PHPUnit frontend, which does not call runTests().
* Put more information into ParserTestResult and use it more often.
Setup and teardown:
* Introduce a new API for setup/teardown where setup functions return a
ScopedCallback object which automatically performs the corresponding
teardown when it goes out of scope.
* Rename setUp() to staticSetup(), rewrite. There was a lot of cruft in
here which was simply copied from Setup.php without review, and had
nothing to do with parser tests.
* Rename setupGlobals() to perTestSetup(), mostly rewrite. For
performance, give staticSetup() precedence in cases where they were
both setting up the same thing.
* In support of merged setup code, allow Hooks::clear() to be called
from parserTests.php.
* Remove wgFileExtensions -- it is only used by UploadBase which we
don't call.
* Remove wgUseImageResize -- superseded by MockMediaHandlerFactory which
I imported from NewParserTest.
* Import MockFileBackend from NewParserTest. But instead of
customising the configuration globals, I injected services.
* Remove thumbnail deletion from upload teardown. This makes glob
handling as in the old parserTests.php unnecessary.
* Remove math file from upload teardown, math is actually an extension
now! Also, the relevant parser tests were removed from the Math
extension two years ago in favour of unit tests.
* Make addArticle() private, and introduce addArticles() instead, which
allows setup/teardown to be done once for each batch of articles
instead of every time.
* Remove $wgNamespaceAliases and $wgNamespaceProtection setup. These were
copied in from Setup.php in 2010, and are redundant since we do
actually run Setup.php.
* Use NullLockManager, don't set up a temporary directory just for
this alone.
Fuzz tests:
* Use the new TestSetup class.
* Updated for ParserTestRunner interface change.
* Remove some obsolete references to fuzz tests from the two frontends
where they used to reside.
Bug: T41473
Change-Id: Ia8e17008cb9d9b62ce5645e15a41a3b402f4026a
2016-09-08 01:25:22 +00:00
|
|
|
if ( self::$dbSetup ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
if ( $db->tablePrefix() === $prefix ) {
|
2014-03-05 14:41:41 +00:00
|
|
|
throw new MWException(
|
|
|
|
|
'Cannot run unit tests, the database prefix is already "' . $prefix . '"' );
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
// TODO: the below should be re-written as soon as LBFactory, LoadBalancer,
|
2016-09-26 22:40:07 +00:00
|
|
|
// and Database no longer use global state.
|
2016-05-01 19:29:11 +00:00
|
|
|
|
2016-03-15 02:49:13 +00:00
|
|
|
self::$dbSetup = true;
|
|
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
if ( !self::setupDatabaseWithTestPrefix( $db, $prefix ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assuming this isn't needed for External Store database, and not sure if the procedure
|
|
|
|
|
// would be available there.
|
|
|
|
|
if ( $db->getType() == 'oracle' ) {
|
2018-08-28 02:39:33 +00:00
|
|
|
$db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
2017-06-29 00:14:18 +00:00
|
|
|
|
|
|
|
|
Hooks::run( 'UnitTestsAfterDatabaseSetup', [ $db, $prefix ] );
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
2012-11-13 16:48:15 +00:00
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
/**
|
|
|
|
|
* Clones the External Store database(s) for testing
|
|
|
|
|
*
|
2018-07-04 15:20:31 +00:00
|
|
|
* @param string|null $testPrefix Prefix for test tables. Will be determined automatically
|
|
|
|
|
* if not given.
|
2016-03-03 07:30:24 +00:00
|
|
|
*/
|
2018-07-04 15:20:31 +00:00
|
|
|
protected static function setupExternalStoreTestDBs( $testPrefix = null ) {
|
2016-03-03 07:30:24 +00:00
|
|
|
$connections = self::getExternalStoreDatabaseConnections();
|
|
|
|
|
foreach ( $connections as $dbw ) {
|
|
|
|
|
self::setupDatabaseWithTestPrefix( $dbw, $testPrefix );
|
2011-11-10 13:29:32 +00:00
|
|
|
}
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
/**
|
|
|
|
|
* Gets master database connections for all of the ExternalStoreDB
|
|
|
|
|
* stores configured in $wgDefaultExternalStore.
|
|
|
|
|
*
|
2016-09-26 22:40:07 +00:00
|
|
|
* @return Database[] Array of Database master connections
|
2016-03-03 07:30:24 +00:00
|
|
|
*/
|
|
|
|
|
protected static function getExternalStoreDatabaseConnections() {
|
|
|
|
|
global $wgDefaultExternalStore;
|
|
|
|
|
|
2016-09-23 22:59:22 +00:00
|
|
|
/** @var ExternalStoreDB $externalStoreDB */
|
2016-03-03 07:30:24 +00:00
|
|
|
$externalStoreDB = ExternalStore::getStoreObject( 'DB' );
|
2016-10-31 04:38:12 +00:00
|
|
|
$defaultArray = (array)$wgDefaultExternalStore;
|
2016-03-03 07:30:24 +00:00
|
|
|
$dbws = [];
|
|
|
|
|
foreach ( $defaultArray as $url ) {
|
|
|
|
|
if ( strpos( $url, 'DB://' ) === 0 ) {
|
|
|
|
|
list( $proto, $cluster ) = explode( '://', $url, 2 );
|
2016-09-23 22:59:22 +00:00
|
|
|
// Avoid getMaster() because setupDatabaseWithTestPrefix()
|
2016-09-26 22:40:07 +00:00
|
|
|
// requires Database instead of plain DBConnRef/IDatabase
|
2016-11-28 18:26:14 +00:00
|
|
|
$dbws[] = $externalStoreDB->getMaster( $cluster );
|
2016-03-03 07:30:24 +00:00
|
|
|
}
|
2011-06-01 22:43:19 +00:00
|
|
|
}
|
2016-03-03 07:30:24 +00:00
|
|
|
|
|
|
|
|
return $dbws;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether ExternalStoreDB is being used
|
|
|
|
|
*
|
|
|
|
|
* @return bool True if it's being used
|
|
|
|
|
*/
|
|
|
|
|
protected static function isUsingExternalStoreDB() {
|
|
|
|
|
global $wgDefaultExternalStore;
|
|
|
|
|
if ( !$wgDefaultExternalStore ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 04:38:12 +00:00
|
|
|
$defaultArray = (array)$wgDefaultExternalStore;
|
2016-03-03 07:30:24 +00:00
|
|
|
foreach ( $defaultArray as $url ) {
|
|
|
|
|
if ( strpos( $url, 'DB://' ) === 0 ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2011-05-24 21:22:36 +00:00
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2017-11-16 16:48:25 +00:00
|
|
|
/**
|
|
|
|
|
* @throws LogicException if the given database connection is not a set up to use
|
|
|
|
|
* mock tables.
|
2018-04-17 07:49:20 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.31 this is no longer private.
|
2017-11-16 16:48:25 +00:00
|
|
|
*/
|
2018-04-17 07:49:20 +00:00
|
|
|
protected function ensureMockDatabaseConnection( IDatabase $db ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
if ( $db->tablePrefix() !== $this->dbPrefix() ) {
|
|
|
|
|
throw new LogicException(
|
|
|
|
|
'Trying to delete mock tables, but table prefix does not indicate a mock database.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
private static $schemaOverrideDefaults = [
|
|
|
|
|
'scripts' => [],
|
|
|
|
|
'create' => [],
|
|
|
|
|
'drop' => [],
|
|
|
|
|
'alter' => [],
|
|
|
|
|
];
|
|
|
|
|
|
2017-11-16 16:48:25 +00:00
|
|
|
/**
|
|
|
|
|
* Stub. If a test suite needs to test against a specific database schema, it should
|
|
|
|
|
* override this method and return the appropriate information from it.
|
|
|
|
|
*
|
2018-03-28 20:02:42 +00:00
|
|
|
* @param IMaintainableDatabase $db The DB connection to use for the mock schema.
|
|
|
|
|
* May be used to check the current state of the schema, to determine what
|
|
|
|
|
* overrides are needed.
|
|
|
|
|
*
|
|
|
|
|
* @return array An associative array with the following fields:
|
|
|
|
|
* - 'scripts': any SQL scripts to run. If empty or not present, schema overrides are skipped.
|
|
|
|
|
* - 'create': A list of tables created (may or may not exist in the original schema).
|
|
|
|
|
* - 'drop': A list of tables dropped (expected to be present in the original schema).
|
|
|
|
|
* - 'alter': A list of tables altered (expected to be present in the original schema).
|
2017-11-16 16:48:25 +00:00
|
|
|
*/
|
2018-03-28 20:02:42 +00:00
|
|
|
protected function getSchemaOverrides( IMaintainableDatabase $db ) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-06-02 23:25:01 +00:00
|
|
|
* Undoes the specified schema overrides..
|
2018-03-28 20:02:42 +00:00
|
|
|
* Called once per test class, just before addDataOnce().
|
|
|
|
|
*
|
|
|
|
|
* @param IMaintainableDatabase $db
|
|
|
|
|
* @param array $oldOverrides
|
|
|
|
|
*/
|
|
|
|
|
private function undoSchemaOverrides( IMaintainableDatabase $db, $oldOverrides ) {
|
|
|
|
|
$this->ensureMockDatabaseConnection( $db );
|
|
|
|
|
|
|
|
|
|
$oldOverrides = $oldOverrides + self::$schemaOverrideDefaults;
|
2018-06-02 23:25:01 +00:00
|
|
|
$originalTables = $this->listOriginalTables( $db, 'unprefixed' );
|
2018-03-28 20:02:42 +00:00
|
|
|
|
|
|
|
|
// Drop tables that need to be restored or removed.
|
|
|
|
|
$tablesToDrop = array_merge( $oldOverrides['create'], $oldOverrides['alter'] );
|
|
|
|
|
|
|
|
|
|
// Restore tables that have been dropped or created or altered,
|
|
|
|
|
// if they exist in the original schema.
|
|
|
|
|
$tablesToRestore = array_merge( $tablesToDrop, $oldOverrides['drop'] );
|
|
|
|
|
$tablesToRestore = array_intersect( $originalTables, $tablesToRestore );
|
|
|
|
|
|
|
|
|
|
if ( $tablesToDrop ) {
|
|
|
|
|
$this->dropMockTables( $db, $tablesToDrop );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $tablesToRestore ) {
|
|
|
|
|
$this->recloneMockTables( $db, $tablesToRestore );
|
|
|
|
|
}
|
2017-11-16 16:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-28 20:02:42 +00:00
|
|
|
* Applies the schema overrides returned by getSchemaOverrides(),
|
|
|
|
|
* after undoing any previously applied schema overrides.
|
2017-11-16 16:48:25 +00:00
|
|
|
* Called once per test class, just before addDataOnce().
|
|
|
|
|
*/
|
|
|
|
|
private function setUpSchema( IMaintainableDatabase $db ) {
|
2018-03-28 20:02:42 +00:00
|
|
|
// Undo any active overrides.
|
2017-10-06 22:17:58 +00:00
|
|
|
$oldOverrides = $db->_schemaOverrides ?? self::$schemaOverrideDefaults;
|
2018-03-28 20:02:42 +00:00
|
|
|
|
|
|
|
|
if ( $oldOverrides['alter'] || $oldOverrides['create'] || $oldOverrides['drop'] ) {
|
|
|
|
|
$this->undoSchemaOverrides( $db, $oldOverrides );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine new overrides.
|
|
|
|
|
$overrides = $this->getSchemaOverrides( $db ) + self::$schemaOverrideDefaults;
|
2017-11-16 16:48:25 +00:00
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
$extraKeys = array_diff(
|
|
|
|
|
array_keys( $overrides ),
|
|
|
|
|
array_keys( self::$schemaOverrideDefaults )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $extraKeys ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
throw new InvalidArgumentException(
|
2018-03-28 20:02:42 +00:00
|
|
|
'Schema override contains extra keys: ' . var_export( $extraKeys, true )
|
2017-11-16 16:48:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
if ( !$overrides['scripts'] ) {
|
|
|
|
|
// no scripts to run
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$overrides['create'] && !$overrides['drop'] && !$overrides['alter'] ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
throw new InvalidArgumentException(
|
2018-03-28 20:02:42 +00:00
|
|
|
'Schema override scripts given, but no tables are declared to be '
|
|
|
|
|
. 'created, dropped or altered.'
|
2017-11-16 16:48:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->ensureMockDatabaseConnection( $db );
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
// Drop the tables that will be created by the schema scripts.
|
2018-06-02 23:25:01 +00:00
|
|
|
$originalTables = $this->listOriginalTables( $db, 'unprefixed' );
|
2018-03-28 20:02:42 +00:00
|
|
|
$tablesToDrop = array_intersect( $originalTables, $overrides['create'] );
|
2017-11-16 16:48:25 +00:00
|
|
|
|
|
|
|
|
if ( $tablesToDrop ) {
|
|
|
|
|
$this->dropMockTables( $db, $tablesToDrop );
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
// Run schema override scripts.
|
|
|
|
|
foreach ( $overrides['scripts'] as $script ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
$db->sourceFile(
|
|
|
|
|
$script,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
__METHOD__,
|
|
|
|
|
function ( $cmd ) {
|
|
|
|
|
return $this->mungeSchemaUpdateQuery( $cmd );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
$db->_schemaOverrides = $overrides;
|
2017-11-16 16:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function mungeSchemaUpdateQuery( $cmd ) {
|
|
|
|
|
return self::$useTemporaryTables
|
|
|
|
|
? preg_replace( '/\bCREATE\s+TABLE\b/i', 'CREATE TEMPORARY TABLE', $cmd )
|
|
|
|
|
: $cmd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drops the given mock tables.
|
|
|
|
|
*
|
|
|
|
|
* @param IMaintainableDatabase $db
|
|
|
|
|
* @param array $tables
|
|
|
|
|
*/
|
|
|
|
|
private function dropMockTables( IMaintainableDatabase $db, array $tables ) {
|
|
|
|
|
$this->ensureMockDatabaseConnection( $db );
|
|
|
|
|
|
|
|
|
|
foreach ( $tables as $tbl ) {
|
|
|
|
|
$tbl = $db->tableName( $tbl );
|
2018-01-06 10:16:13 +00:00
|
|
|
$db->query( "DROP TABLE IF EXISTS $tbl", __METHOD__ );
|
2017-11-16 16:48:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
/**
|
|
|
|
|
* Lists all tables in the live database schema.
|
|
|
|
|
*
|
|
|
|
|
* @param IMaintainableDatabase $db
|
2018-06-02 23:25:01 +00:00
|
|
|
* @param string $prefix Either 'prefixed' or 'unprefixed'
|
2018-03-28 20:02:42 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2018-06-02 23:25:01 +00:00
|
|
|
private function listOriginalTables( IMaintainableDatabase $db, $prefix = 'prefixed' ) {
|
2018-03-28 20:02:42 +00:00
|
|
|
if ( !isset( $db->_originalTablePrefix ) ) {
|
|
|
|
|
throw new LogicException( 'No original table prefix know, cannot list tables!' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$originalTables = $db->listTables( $db->_originalTablePrefix, __METHOD__ );
|
2018-06-02 23:25:01 +00:00
|
|
|
if ( $prefix === 'unprefixed' ) {
|
2018-09-01 12:39:56 +00:00
|
|
|
$originalPrefixRegex = '/^' . preg_quote( $db->_originalTablePrefix, '/' ) . '/';
|
2018-06-02 23:25:01 +00:00
|
|
|
$originalTables = array_map(
|
|
|
|
|
function ( $pt ) use ( $originalPrefixRegex ) {
|
|
|
|
|
return preg_replace( $originalPrefixRegex, '', $pt );
|
|
|
|
|
},
|
|
|
|
|
$originalTables
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 20:02:42 +00:00
|
|
|
return $originalTables;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-16 16:48:25 +00:00
|
|
|
/**
|
|
|
|
|
* Re-clones the given mock tables to restore them based on the live database schema.
|
2018-03-28 20:02:42 +00:00
|
|
|
* The tables listed in $tables are expected to currently not exist, so dropMockTables()
|
|
|
|
|
* should be called first.
|
2017-11-16 16:48:25 +00:00
|
|
|
*
|
|
|
|
|
* @param IMaintainableDatabase $db
|
|
|
|
|
* @param array $tables
|
|
|
|
|
*/
|
|
|
|
|
private function recloneMockTables( IMaintainableDatabase $db, array $tables ) {
|
|
|
|
|
$this->ensureMockDatabaseConnection( $db );
|
|
|
|
|
|
|
|
|
|
if ( !isset( $db->_originalTablePrefix ) ) {
|
|
|
|
|
throw new LogicException( 'No original table prefix know, cannot restore tables!' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-02 23:25:01 +00:00
|
|
|
$originalTables = $this->listOriginalTables( $db, 'unprefixed' );
|
2017-11-16 16:48:25 +00:00
|
|
|
$tables = array_intersect( $tables, $originalTables );
|
|
|
|
|
|
|
|
|
|
$dbClone = new CloneDatabase( $db, $tables, $db->tablePrefix(), $db->_originalTablePrefix );
|
|
|
|
|
$dbClone->useTemporaryTables( self::$useTemporaryTables );
|
|
|
|
|
|
|
|
|
|
$dbClone->cloneTableStructure();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-24 21:22:36 +00:00
|
|
|
/**
|
|
|
|
|
* Empty all tables so they can be repopulated for tests
|
2016-03-03 07:30:24 +00:00
|
|
|
*
|
2016-09-26 22:40:07 +00:00
|
|
|
* @param Database $db|null Database to reset
|
2016-03-03 07:30:24 +00:00
|
|
|
* @param array $tablesUsed Tables to reset
|
2011-05-24 21:22:36 +00:00
|
|
|
*/
|
2016-03-03 07:30:24 +00:00
|
|
|
private function resetDB( $db, $tablesUsed ) {
|
|
|
|
|
if ( $db ) {
|
2017-09-12 17:12:29 +00:00
|
|
|
$userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ];
|
2018-01-29 15:54:02 +00:00
|
|
|
$pageTables = [
|
|
|
|
|
'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive',
|
2018-09-04 01:59:03 +00:00
|
|
|
'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles',
|
2018-01-29 15:54:02 +00:00
|
|
|
];
|
2017-06-06 17:39:14 +00:00
|
|
|
$coreDBDataTables = array_merge( $userTables, $pageTables );
|
2016-04-11 17:36:38 +00:00
|
|
|
|
2017-06-06 17:39:14 +00:00
|
|
|
// If any of the user or page tables were marked as used, we should clear all of them.
|
2016-04-11 17:36:38 +00:00
|
|
|
if ( array_intersect( $tablesUsed, $userTables ) ) {
|
|
|
|
|
$tablesUsed = array_unique( array_merge( $tablesUsed, $userTables ) );
|
2016-05-18 09:19:20 +00:00
|
|
|
TestUserRegistry::clear();
|
2016-04-11 17:36:38 +00:00
|
|
|
}
|
2017-06-06 17:39:14 +00:00
|
|
|
if ( array_intersect( $tablesUsed, $pageTables ) ) {
|
|
|
|
|
$tablesUsed = array_unique( array_merge( $tablesUsed, $pageTables ) );
|
|
|
|
|
}
|
2016-04-11 17:36:38 +00:00
|
|
|
|
2018-06-02 03:31:52 +00:00
|
|
|
// Postgres, Oracle, and MSSQL all use mwuser/pagecontent
|
|
|
|
|
// instead of user/text. But Postgres does not remap the
|
|
|
|
|
// table name in tableExists(), so we mark the real table
|
|
|
|
|
// names as being used.
|
|
|
|
|
if ( $db->getType() === 'postgres' ) {
|
|
|
|
|
if ( in_array( 'user', $tablesUsed ) ) {
|
|
|
|
|
$tablesUsed[] = 'mwuser';
|
|
|
|
|
}
|
|
|
|
|
if ( in_array( 'text', $tablesUsed ) ) {
|
|
|
|
|
$tablesUsed[] = 'pagecontent';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
foreach ( $tablesUsed as $tbl ) {
|
2018-08-22 16:02:49 +00:00
|
|
|
$this->truncateTable( $tbl, $db );
|
2011-05-24 21:22:36 +00:00
|
|
|
}
|
2016-04-11 17:36:38 +00:00
|
|
|
|
|
|
|
|
if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) {
|
2018-09-04 01:59:03 +00:00
|
|
|
// Reset services that may contain information relating to the truncated tables
|
|
|
|
|
$this->overrideMwServices();
|
2016-04-11 17:36:38 +00:00
|
|
|
// Re-add core DB data that was deleted
|
|
|
|
|
$this->addCoreDBData();
|
|
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2018-08-22 16:02:49 +00:00
|
|
|
/**
|
|
|
|
|
* Empties the given table and resets any auto-increment counters.
|
|
|
|
|
* Will also purge caches associated with some well known tables.
|
|
|
|
|
* If the table is not know, this method just returns.
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @param IDatabase|null $db
|
|
|
|
|
*/
|
|
|
|
|
protected function truncateTable( $tableName, IDatabase $db = null ) {
|
|
|
|
|
if ( !$db ) {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$db->tableExists( $tableName ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
|
|
|
|
|
|
|
|
|
|
if ( $truncate ) {
|
|
|
|
|
$db->query( 'TRUNCATE TABLE ' . $db->tableName( $tableName ), __METHOD__ );
|
|
|
|
|
} else {
|
|
|
|
|
$db->delete( $tableName, '*', __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 01:59:03 +00:00
|
|
|
if ( $db instanceof DatabasePostgres || $db instanceof DatabaseSqlite ) {
|
2018-08-22 16:02:49 +00:00
|
|
|
// Reset the table's sequence too.
|
|
|
|
|
$db->resetSequenceForTable( $tableName, __METHOD__ );
|
|
|
|
|
}
|
2018-09-04 01:59:03 +00:00
|
|
|
|
|
|
|
|
// re-initialize site_stats table
|
|
|
|
|
if ( $tableName === 'site_stats' ) {
|
|
|
|
|
SiteStatsInit::doPlaceholderInit();
|
|
|
|
|
}
|
2018-08-22 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 07:30:24 +00:00
|
|
|
private static function unprefixTable( &$tableName, $ind, $prefix ) {
|
|
|
|
|
$tableName = substr( $tableName, strlen( $prefix ) );
|
2010-12-30 16:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-28 10:27:15 +00:00
|
|
|
private static function isNotUnittest( $table ) {
|
2017-11-16 16:48:25 +00:00
|
|
|
return strpos( $table, self::DB_PREFIX ) !== 0;
|
2011-06-02 17:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*
|
2016-11-28 18:26:14 +00:00
|
|
|
* @param IMaintainableDatabase $db
|
2014-03-05 14:41:41 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
public static function listTables( IMaintainableDatabase $db ) {
|
2016-03-03 07:30:24 +00:00
|
|
|
$prefix = $db->tablePrefix();
|
|
|
|
|
$tables = $db->listTables( $prefix, __METHOD__ );
|
2013-05-24 15:14:40 +00:00
|
|
|
|
|
|
|
|
if ( $db->getType() === 'mysql' ) {
|
2016-09-23 03:11:18 +00:00
|
|
|
static $viewListCache = null;
|
|
|
|
|
if ( $viewListCache === null ) {
|
|
|
|
|
$viewListCache = $db->listViews( null, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
// T45571: cannot clone VIEWs under MySQL
|
|
|
|
|
$tables = array_diff( $tables, $viewListCache );
|
2013-05-24 15:14:40 +00:00
|
|
|
}
|
2016-03-03 07:30:24 +00:00
|
|
|
array_walk( $tables, [ __CLASS__, 'unprefixTable' ], $prefix );
|
2011-06-01 23:17:29 +00:00
|
|
|
|
2011-06-02 17:58:34 +00:00
|
|
|
// Don't duplicate test tables from the previous fataled run
|
2016-02-17 09:09:32 +00:00
|
|
|
$tables = array_filter( $tables, [ __CLASS__, 'isNotUnittest' ] );
|
2011-06-02 17:58:34 +00:00
|
|
|
|
2012-11-13 16:48:15 +00:00
|
|
|
if ( $db->getType() == 'sqlite' ) {
|
2011-06-01 23:17:29 +00:00
|
|
|
$tables = array_flip( $tables );
|
|
|
|
|
// these are subtables of searchindex and don't need to be duped/dropped separately
|
|
|
|
|
unset( $tables['searchindex_content'] );
|
|
|
|
|
unset( $tables['searchindex_segdir'] );
|
|
|
|
|
unset( $tables['searchindex_segments'] );
|
|
|
|
|
$tables = array_flip( $tables );
|
2011-03-05 16:09:10 +00:00
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-06-01 23:17:29 +00:00
|
|
|
return $tables;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-07-04 15:20:31 +00:00
|
|
|
/**
|
|
|
|
|
* Copy test data from one database connection to another.
|
|
|
|
|
*
|
|
|
|
|
* This should only be used for small data sets.
|
|
|
|
|
*
|
|
|
|
|
* @param IDatabase $source
|
|
|
|
|
* @param IDatabase $target
|
|
|
|
|
*/
|
|
|
|
|
public function copyTestData( IDatabase $source, IDatabase $target ) {
|
|
|
|
|
$tables = self::listOriginalTables( $source, 'unprefixed' );
|
|
|
|
|
|
|
|
|
|
foreach ( $tables as $table ) {
|
|
|
|
|
$res = $source->select( $table, '*', [], __METHOD__ );
|
|
|
|
|
$allRows = [];
|
|
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$allRows[] = (array)$row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$target->insert( $table, $allRows, __METHOD__, [ 'IGNORE' ] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
2010-12-31 21:10:36 +00:00
|
|
|
protected function checkDbIsSupported() {
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
|
2010-12-31 21:10:36 +00:00
|
|
|
throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.18
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $offset
|
|
|
|
|
* @return mixed
|
2014-03-05 14:41:41 +00:00
|
|
|
*/
|
2011-01-01 05:53:04 +00:00
|
|
|
public function getCliArg( $offset ) {
|
2018-09-03 07:02:44 +00:00
|
|
|
return $this->cliArgs[$offset] ?? null;
|
2011-01-01 05:53:04 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-03-05 14:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.18
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $offset
|
|
|
|
|
* @param mixed $value
|
2014-03-05 14:41:41 +00:00
|
|
|
*/
|
2011-01-01 05:53:04 +00:00
|
|
|
public function setCliArg( $offset, $value ) {
|
2018-09-03 07:02:44 +00:00
|
|
|
$this->cliArgs[$offset] = $value;
|
2011-01-01 05:53:04 +00:00
|
|
|
}
|
2011-06-22 21:02:07 +00:00
|
|
|
|
2011-07-25 16:27:33 +00:00
|
|
|
/**
|
|
|
|
|
* Don't throw a warning if $function is deprecated and called later
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @since 1.19
|
|
|
|
|
*
|
|
|
|
|
* @param string $function
|
2011-07-25 16:27:33 +00:00
|
|
|
*/
|
2014-03-05 14:41:41 +00:00
|
|
|
public function hideDeprecated( $function ) {
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2011-07-25 16:27:33 +00:00
|
|
|
wfDeprecated( $function );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2011-07-25 16:27:33 +00:00
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts that the given database query yields the rows given by $expectedRows.
|
|
|
|
|
* The expected rows should be given as indexed (not associative) arrays, with
|
|
|
|
|
* the values given in the order of the columns in the $fields parameter.
|
|
|
|
|
* Note that the rows are sorted by the columns given in $fields.
|
|
|
|
|
*
|
2012-08-12 19:35:37 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @param string|array $table The table(s) to query
|
|
|
|
|
* @param string|array $fields The columns to include in the result (and to sort by)
|
|
|
|
|
* @param string|array $condition "where" condition(s)
|
|
|
|
|
* @param array $expectedRows An array of arrays giving the expected rows.
|
2018-01-05 16:26:56 +00:00
|
|
|
* @param array $options Options for the query
|
|
|
|
|
* @param array $join_conds Join conditions for the query
|
2012-05-10 20:56:34 +00:00
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @throws MWException If this test cases's needsDB() method doesn't return true.
|
2012-05-10 20:56:34 +00:00
|
|
|
* Test cases can use "@group Database" to enable database test support,
|
|
|
|
|
* or list the tables under testing in $this->tablesUsed, or override the
|
|
|
|
|
* needsDB() method.
|
|
|
|
|
*/
|
2018-01-05 16:26:56 +00:00
|
|
|
protected function assertSelect(
|
|
|
|
|
$table, $fields, $condition, array $expectedRows, array $options = [], array $join_conds = []
|
|
|
|
|
) {
|
2012-05-10 20:56:34 +00:00
|
|
|
if ( !$this->needsDB() ) {
|
|
|
|
|
throw new MWException( 'When testing database state, the test cases\'s needDB()' .
|
2013-02-14 11:22:13 +00:00
|
|
|
' method should return true. Use @group Database or $this->tablesUsed.' );
|
2012-05-10 20:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-04 18:53:34 +00:00
|
|
|
$db = wfGetDB( DB_REPLICA );
|
2012-05-10 20:56:34 +00:00
|
|
|
|
2018-01-05 16:26:56 +00:00
|
|
|
$res = $db->select(
|
|
|
|
|
$table,
|
|
|
|
|
$fields,
|
|
|
|
|
$condition,
|
|
|
|
|
wfGetCaller(),
|
|
|
|
|
$options + [ 'ORDER BY' => $fields ],
|
|
|
|
|
$join_conds
|
|
|
|
|
);
|
2012-05-10 20:56:34 +00:00
|
|
|
$this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
|
|
foreach ( $expectedRows as $expected ) {
|
|
|
|
|
$r = $res->fetchRow();
|
|
|
|
|
self::stripStringKeys( $r );
|
|
|
|
|
|
|
|
|
|
$i += 1;
|
|
|
|
|
$this->assertNotEmpty( $r, "row #$i missing" );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $r, "row #$i mismatches" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$r = $res->fetchRow();
|
|
|
|
|
self::stripStringKeys( $r );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $r, "found extra row (after #$i)" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 19:37:54 +00:00
|
|
|
/**
|
|
|
|
|
* Utility method taking an array of elements and wrapping
|
2014-11-08 19:07:19 +00:00
|
|
|
* each element in its own array. Useful for data providers
|
2012-08-12 19:37:54 +00:00
|
|
|
* that only return a single argument.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $elements
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function arrayWrap( array $elements ) {
|
|
|
|
|
return array_map(
|
2013-02-14 11:22:13 +00:00
|
|
|
function ( $element ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $element ];
|
2012-08-12 19:37:54 +00:00
|
|
|
},
|
|
|
|
|
$elements
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 14:24:10 +00:00
|
|
|
/**
|
|
|
|
|
* Assert that two arrays are equal. By default this means that both arrays need to hold
|
|
|
|
|
* the same set of values. Using additional arguments, order and associated key can also
|
|
|
|
|
* be set as relevant.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $expected
|
|
|
|
|
* @param array $actual
|
2014-03-05 14:41:41 +00:00
|
|
|
* @param bool $ordered If the order of the values should match
|
|
|
|
|
* @param bool $named If the keys should match
|
2012-07-02 14:24:10 +00:00
|
|
|
*/
|
2014-04-24 09:57:41 +00:00
|
|
|
protected function assertArrayEquals( array $expected, array $actual,
|
|
|
|
|
$ordered = false, $named = false
|
|
|
|
|
) {
|
2012-07-02 14:24:10 +00:00
|
|
|
if ( !$ordered ) {
|
2012-08-12 20:55:55 +00:00
|
|
|
$this->objectAssociativeSort( $expected );
|
|
|
|
|
$this->objectAssociativeSort( $actual );
|
2012-07-02 14:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
2012-07-26 18:08:54 +00:00
|
|
|
if ( !$named ) {
|
|
|
|
|
$expected = array_values( $expected );
|
|
|
|
|
$actual = array_values( $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 14:24:10 +00:00
|
|
|
call_user_func_array(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'assertEquals' ],
|
|
|
|
|
array_merge( [ $expected, $actual ], array_slice( func_get_args(), 4 ) )
|
2012-07-02 14:24:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 08:46:07 +00:00
|
|
|
/**
|
|
|
|
|
* Put each HTML element on its own line and then equals() the results
|
|
|
|
|
*
|
|
|
|
|
* Use for nicely formatting of PHPUnit diff output when comparing very
|
|
|
|
|
* simple HTML
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @param string $expected HTML on oneline
|
|
|
|
|
* @param string $actual HTML on oneline
|
|
|
|
|
* @param string $msg Optional message
|
2012-09-03 08:46:07 +00:00
|
|
|
*/
|
2013-02-14 11:22:13 +00:00
|
|
|
protected function assertHTMLEquals( $expected, $actual, $msg = '' ) {
|
2012-09-03 08:46:07 +00:00
|
|
|
$expected = str_replace( '>', ">\n", $expected );
|
2013-02-14 11:22:13 +00:00
|
|
|
$actual = str_replace( '>', ">\n", $actual );
|
2012-09-03 08:46:07 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual, $msg );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 20:55:55 +00:00
|
|
|
/**
|
|
|
|
|
* Does an associative sort that works for objects.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param array &$array
|
2012-08-12 20:55:55 +00:00
|
|
|
*/
|
|
|
|
|
protected function objectAssociativeSort( array &$array ) {
|
|
|
|
|
uasort(
|
|
|
|
|
$array,
|
2013-02-14 11:22:13 +00:00
|
|
|
function ( $a, $b ) {
|
Use PHP 7 '<=>' operator in 'sort()' callbacks
`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.
It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.
* Replace a common pattern like `return $a < $b ? -1 : 1` with the
new operator (and similar patterns with the variables, the numbers
or the comparison inverted). Some of the uses were previously not
correctly handling the variables being equal; this is now
automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
$a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
when both variables are strings, but if any of the variables is not,
'strcmp()' converts it to a string before comparison, which could
give different results than '<=>', so changing this would require
careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
of the time (returns `1` if `$b` is lesser, and `0` if they are
equal or `$a` is lesser) but is erroneous.
Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
2017-10-06 20:39:13 +00:00
|
|
|
return serialize( $a ) <=> serialize( $b );
|
2012-08-12 20:55:55 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-10 20:56:34 +00:00
|
|
|
/**
|
|
|
|
|
* Utility function for eliminating all string keys from an array.
|
|
|
|
|
* Useful to turn a database result row as returned by fetchRow() into
|
|
|
|
|
* a pure indexed array.
|
|
|
|
|
*
|
2012-08-12 19:35:37 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param mixed &$r The array to remove string keys from.
|
2012-05-10 20:56:34 +00:00
|
|
|
*/
|
|
|
|
|
protected static function stripStringKeys( &$r ) {
|
2012-08-12 19:35:37 +00:00
|
|
|
if ( !is_array( $r ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
|
|
|
|
|
foreach ( $r as $k => $v ) {
|
2012-08-12 19:35:37 +00:00
|
|
|
if ( is_string( $k ) ) {
|
|
|
|
|
unset( $r[$k] );
|
|
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-08-12 19:35:37 +00:00
|
|
|
|
2012-09-04 13:53:05 +00:00
|
|
|
/**
|
|
|
|
|
* Asserts that the provided variable is of the specified
|
|
|
|
|
* internal type or equals the $value argument. This is useful
|
|
|
|
|
* for testing return types of functions that return a certain
|
|
|
|
|
* type or *value* when not set or on error.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param mixed $actual
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param string $message
|
|
|
|
|
*/
|
|
|
|
|
protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) {
|
|
|
|
|
if ( $actual === $value ) {
|
|
|
|
|
$this->assertTrue( true, $message );
|
2013-02-14 11:22:13 +00:00
|
|
|
} else {
|
2012-09-04 13:53:05 +00:00
|
|
|
$this->assertType( $type, $actual, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts the type of the provided value. This can be either
|
|
|
|
|
* in internal type such as boolean or integer, or a class or
|
|
|
|
|
* interface the value extends or implements.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param mixed $actual
|
|
|
|
|
* @param string $message
|
|
|
|
|
*/
|
|
|
|
|
protected function assertType( $type, $actual, $message = '' ) {
|
2012-09-24 13:14:28 +00:00
|
|
|
if ( class_exists( $type ) || interface_exists( $type ) ) {
|
2012-09-04 13:53:05 +00:00
|
|
|
$this->assertInstanceOf( $type, $actual, $message );
|
2013-02-14 11:22:13 +00:00
|
|
|
} else {
|
2012-09-04 13:53:05 +00:00
|
|
|
$this->assertInternalType( $type, $actual, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-12 11:09:08 +00:00
|
|
|
/**
|
2013-06-11 23:01:52 +00:00
|
|
|
* Returns true if the given namespace defaults to Wikitext
|
2012-10-12 11:09:08 +00:00
|
|
|
* according to $wgNamespaceContentModels
|
|
|
|
|
*
|
|
|
|
|
* @param int $ns The namespace ID to check
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
protected function isWikitextNS( $ns ) {
|
|
|
|
|
global $wgNamespaceContentModels;
|
|
|
|
|
|
|
|
|
|
if ( isset( $wgNamespaceContentModels[$ns] ) ) {
|
|
|
|
|
return $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-10-12 15:34:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the ID of a namespace that defaults to Wikitext.
|
|
|
|
|
*
|
2014-03-05 14:41:41 +00:00
|
|
|
* @throws MWException If there is none.
|
|
|
|
|
* @return int The ID of the wikitext Namespace
|
2012-10-12 15:34:29 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
protected function getDefaultWikitextNS() {
|
|
|
|
|
global $wgNamespaceContentModels;
|
|
|
|
|
|
|
|
|
|
static $wikitextNS = null; // this is not going to change
|
|
|
|
|
if ( $wikitextNS !== null ) {
|
|
|
|
|
return $wikitextNS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// quickly short out on most common case:
|
|
|
|
|
if ( !isset( $wgNamespaceContentModels[NS_MAIN] ) ) {
|
|
|
|
|
return NS_MAIN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: prefer content namespaces
|
|
|
|
|
$namespaces = array_unique( array_merge(
|
|
|
|
|
MWNamespace::getContentNamespaces(),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ NS_MAIN, NS_HELP, NS_PROJECT ], // prefer these
|
2012-10-12 15:34:29 +00:00
|
|
|
MWNamespace::getValidNamespaces()
|
|
|
|
|
) );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$namespaces = array_diff( $namespaces, [
|
2012-10-12 15:34:29 +00:00
|
|
|
NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2012-10-12 15:34:29 +00:00
|
|
|
|
|
|
|
|
$talk = array_filter( $namespaces, function ( $ns ) {
|
|
|
|
|
return MWNamespace::isTalk( $ns );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
// prefer non-talk pages
|
|
|
|
|
$namespaces = array_diff( $namespaces, $talk );
|
|
|
|
|
$namespaces = array_merge( $namespaces, $talk );
|
|
|
|
|
|
|
|
|
|
// check default content model of each namespace
|
|
|
|
|
foreach ( $namespaces as $ns ) {
|
|
|
|
|
if ( !isset( $wgNamespaceContentModels[$ns] ) ||
|
2013-02-14 11:22:13 +00:00
|
|
|
$wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
|
|
|
|
|
) {
|
2012-10-12 15:34:29 +00:00
|
|
|
$wikitextNS = $ns;
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2012-10-12 15:34:29 +00:00
|
|
|
return $wikitextNS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// give up
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo Inside a test, we could skip the test as incomplete.
|
2012-10-12 15:34:29 +00:00
|
|
|
// But frequently, this is used in fixture setup.
|
|
|
|
|
throw new MWException( "No namespace defaults to wikitext!" );
|
|
|
|
|
}
|
2012-11-22 16:45:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check, if $wgDiff3 is set and ready to merge
|
|
|
|
|
* Will mark the calling test as skipped, if not ready
|
|
|
|
|
*
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
2016-01-27 18:56:09 +00:00
|
|
|
protected function markTestSkippedIfNoDiff3() {
|
2012-11-22 16:45:50 +00:00
|
|
|
global $wgDiff3;
|
|
|
|
|
|
|
|
|
|
# This check may also protect against code injection in
|
|
|
|
|
# case of broken installations.
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2012-11-22 16:45:50 +00:00
|
|
|
$haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2012-11-22 16:45:50 +00:00
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( !$haveDiff3 ) {
|
2012-11-22 16:45:50 +00:00
|
|
|
$this->markTestSkipped( "Skip test, since diff3 is not configured" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-29 17:03:03 +00:00
|
|
|
|
2013-01-28 09:27:31 +00:00
|
|
|
/**
|
|
|
|
|
* Check if $extName is a loaded PHP extension, will skip the
|
|
|
|
|
* test whenever it is not loaded.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.21
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $extName
|
|
|
|
|
* @return bool
|
2013-01-28 09:27:31 +00:00
|
|
|
*/
|
|
|
|
|
protected function checkPHPExtension( $extName ) {
|
|
|
|
|
$loaded = extension_loaded( $extName );
|
2013-02-14 11:22:13 +00:00
|
|
|
if ( !$loaded ) {
|
2013-01-28 09:27:31 +00:00
|
|
|
$this->markTestSkipped( "PHP extension '$extName' is not loaded, skipping." );
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-01-28 09:27:31 +00:00
|
|
|
return $loaded;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 07:19:10 +00:00
|
|
|
/**
|
|
|
|
|
* Skip the test if using the specified database type
|
|
|
|
|
*
|
|
|
|
|
* @param string $type Database type
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*/
|
|
|
|
|
protected function markTestSkippedIfDbType( $type ) {
|
|
|
|
|
if ( $this->db->getType() === $type ) {
|
|
|
|
|
$this->markTestSkipped( "The $type database type isn't supported for this test" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 16:01:10 +00:00
|
|
|
/**
|
|
|
|
|
* Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param string $buffer
|
2015-08-21 16:01:10 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function wfResetOutputBuffersBarrier( $buffer ) {
|
|
|
|
|
return $buffer;
|
|
|
|
|
}
|
2016-03-07 17:26:25 +00:00
|
|
|
|
2016-05-16 20:24:10 +00:00
|
|
|
/**
|
|
|
|
|
* Create a temporary hook handler which will be reset by tearDown.
|
|
|
|
|
* This replaces other handlers for the same hook.
|
|
|
|
|
* @param string $hookName Hook name
|
|
|
|
|
* @param mixed $handler Value suitable for a hook handler
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
protected function setTemporaryHook( $hookName, $handler ) {
|
|
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [ $hookName => [ $handler ] ] );
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 22:48:46 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether file contains given data.
|
|
|
|
|
* @param string $fileName
|
|
|
|
|
* @param string $actualData
|
|
|
|
|
* @param bool $createIfMissing If true, and file does not exist, create it with given data
|
|
|
|
|
* and skip the test.
|
|
|
|
|
* @param string $msg
|
|
|
|
|
* @since 1.30
|
|
|
|
|
*/
|
|
|
|
|
protected function assertFileContains(
|
|
|
|
|
$fileName,
|
|
|
|
|
$actualData,
|
2018-09-05 22:00:25 +00:00
|
|
|
$createIfMissing = false,
|
2016-12-16 22:48:46 +00:00
|
|
|
$msg = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( $createIfMissing ) {
|
|
|
|
|
if ( !file_exists( $fileName ) ) {
|
|
|
|
|
file_put_contents( $fileName, $actualData );
|
|
|
|
|
$this->markTestSkipped( 'Data file $fileName does not exist' );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self::assertFileExists( $fileName );
|
|
|
|
|
}
|
|
|
|
|
self::assertEquals( file_get_contents( $fileName ), $actualData, $msg );
|
|
|
|
|
}
|
2018-07-24 14:50:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Edits or creates a page/revision
|
|
|
|
|
* @param string $pageName Page title
|
|
|
|
|
* @param string $text Content of the page
|
|
|
|
|
* @param string $summary Optional summary string for the revision
|
|
|
|
|
* @param int $defaultNs Optional namespace id
|
|
|
|
|
* @return array Array as returned by WikiPage::doEditContent()
|
2018-09-14 18:30:30 +00:00
|
|
|
* @throws MWException If this test cases's needsDB() method doesn't return true.
|
|
|
|
|
* Test cases can use "@group Database" to enable database test support,
|
|
|
|
|
* or list the tables under testing in $this->tablesUsed, or override the
|
|
|
|
|
* needsDB() method.
|
2018-07-24 14:50:03 +00:00
|
|
|
*/
|
|
|
|
|
protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
|
2018-09-14 18:30:30 +00:00
|
|
|
if ( !$this->needsDB() ) {
|
|
|
|
|
throw new MWException( 'When testing which pages, the test cases\'s needsDB()' .
|
|
|
|
|
' method should return true. Use @group Database or $this->tablesUsed.' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 14:50:03 +00:00
|
|
|
$title = Title::newFromText( $pageName, $defaultNs );
|
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
|
|
|
|
|
return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Revision-deletes a revision.
|
|
|
|
|
*
|
|
|
|
|
* @param Revision|int $rev Revision to delete
|
|
|
|
|
* @param array $value Keys are Revision::DELETED_* flags. Values are 1 to set the bit, 0 to
|
|
|
|
|
* clear, -1 to leave alone. (All other values also clear the bit.)
|
|
|
|
|
* @param string $comment Deletion comment
|
|
|
|
|
*/
|
|
|
|
|
protected function revisionDelete(
|
|
|
|
|
$rev, array $value = [ Revision::DELETED_TEXT => 1 ], $comment = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( is_int( $rev ) ) {
|
|
|
|
|
$rev = Revision::newFromId( $rev );
|
|
|
|
|
}
|
|
|
|
|
RevisionDeleter::createList(
|
|
|
|
|
'revision', RequestContext::getMain(), $rev->getTitle(), [ $rev->getId() ]
|
|
|
|
|
)->setVisibility( [
|
|
|
|
|
'value' => $value,
|
|
|
|
|
'comment' => $comment,
|
|
|
|
|
] );
|
|
|
|
|
}
|
2011-07-25 16:27:33 +00:00
|
|
|
}
|