3 * Bootstrapping for MediaWiki PHPUnit tests
4 * This file is included by phpunit and is NOT in the global scope.
9 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
11 You are running these tests directly from phpunit. You may not have all globals correctly set.
12 Running phpunit.php instead is recommended.
14 require_once ( dirname( __FILE__
) . "/phpunit.php" );
17 // Output a notice when running with older versions of PHPUnit
18 if ( !version_compare( PHPUnit_Runner_Version
::id(), "3.4.1", ">" ) ) {
20 ********************************************************************************
22 These tests run best with version PHPUnit 3.4.2 or better. Earlier versions may
23 show failures because earlier versions of PHPUnit do not properly implement
26 ********************************************************************************
31 global $wgLocalisationCacheConf, $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
32 global $wgMessageCache, $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry;
33 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
34 $wgMainCacheType = CACHE_NONE
;
35 $wgMessageCacheType = CACHE_NONE
;
36 $wgParserCacheType = CACHE_NONE
;
37 $wgUseDatabaseMessages = false; # Set for future resets
39 # The message cache was already created in Setup.php
40 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
41 array( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry ) );
45 abstract class MediaWikiTestSetup
extends PHPUnit_Framework_TestCase
{
48 public $runDisabled = false;
50 protected static $databaseSetupDone = false;
53 protected $oldTablePrefix;
54 protected $useTemporaryTables = true;
56 function __construct( PHPUnit_Framework_TestSuite
$suite = null ) {
57 if ( null !== $suite ) {
58 $this->suite
= $suite;
60 parent
::__construct();
62 if( $this->needsDB() && !is_object( $this->dbClone
) ) {
64 $this->addCoreDBData();
69 function __destruct() {
70 if( $this->needsDB() && is_object( $this->dbClone
) && $this->dbClone
instanceof CloneDatabase
) {
75 function needsDB() { return false; }
77 function addDBData() {}
79 private function addCoreDBData() {
82 $user = User
::newFromName( 'UTSysop' );
83 if ( $user->idForName() == 0 ) {
84 $user->addToDatabase();
85 $user->setPassword( 'UTSysopPassword' );
87 $user->addGroup( 'sysop' );
88 $user->addGroup( 'bureaucrat' );
89 $user->saveSettings();
93 //Make 1 page with 1 revision
94 $article = new Article( Title
::newFromText( 'UTPage' ) );
95 $article->doEdit( 'UTContent',
99 User
::newFromName( 'UTSysop' ) );
102 private function initDB() {
105 if ( self
::$databaseSetupDone ) {
109 $this->db
= wfGetDB( DB_MASTER
);
110 $dbType = $this->db
->getType();
112 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
113 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
116 self
::$databaseSetupDone = true;
117 $this->oldTablePrefix
= $wgDBprefix;
119 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
120 # It seems to have been fixed since (r55079?).
121 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
123 # CREATE TEMPORARY TABLE breaks if there is more than one server
124 if ( wfGetLB()->getServerCount() != 1 ) {
125 $this->useTemporaryTables
= false;
128 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
130 $tables = $this->listTables();
132 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
134 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
135 $this->dbClone
->useTemporaryTables( $temporary );
136 $this->dbClone
->cloneTableStructure();
138 if ( $dbType == 'oracle' )
139 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
141 if ( $dbType == 'oracle' ) {
142 # Insert 0 user to prevent FK violations
145 $this->db
->insert( 'user', array(
147 'user_name' => 'Anonymous' ) );
152 private function destroyDB() {
153 if ( !self
::$databaseSetupDone ) {
157 $this->dbClone
->destroy();
158 self
::$databaseSetupDone = false;
160 if ( $this->useTemporaryTables
) {
161 # Don't need to do anything
163 //Temporary tables seem to be broken ATM, delete anyway
166 if( $this->db
->getType() == 'oracle' ) {
167 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
170 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
173 foreach ( $tables as $table ) {
174 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
175 $this->db
->query( $sql );
178 if ( $this->db
->getType() == 'oracle' )
179 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
184 function __call( $func, $args ) {
185 if ( method_exists( $this->suite
, $func ) ) {
186 return call_user_func_array( array( $this->suite
, $func ), $args);
188 throw new MWException( "Called non-existant $func method on "
189 . get_class( $this ) );
193 protected function listTables() {
196 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
197 $tables = array_map( create_function( '$table', 'global $wgDBprefix; return substr( $table, strlen( $wgDBprefix ) );' ), $tables );