3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 private static $dbSetup = false;
17 * Table name prefixes. Oracle likes it shorter.
19 const DB_PREFIX
= 'unittest_';
20 const ORA_DB_PREFIX
= 'ut_';
22 protected $supportedDBs = array(
28 function __construct( $name = null, array $data = array(), $dataName = '' ) {
29 parent
::__construct( $name, $data, $dataName );
31 $this->backupGlobals
= false;
32 $this->backupStaticAttributes
= false;
35 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
36 /* Some functions require some kind of caching, and will end up using the db,
37 * which we can't allow, as that would open a new connection for mysql.
38 * Replace with a HashBag. They would not be going to persist anyway.
40 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
42 if( $this->needsDB() ) {
46 $this->db
= wfGetDB( DB_MASTER
);
48 $this->checkDbIsSupported();
50 $this->oldTablePrefix
= $wgDBprefix;
52 if( !self
::$dbSetup ) {
54 self
::$dbSetup = true;
57 $this->addCoreDBData();
60 parent
::run( $result );
64 parent
::run( $result );
69 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
73 $rc = new ReflectionClass( $this );
74 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
78 * Stub. If a test needs to add additional data to the database, it should
79 * implement this method and do so
81 function addDBData() {}
83 private function addCoreDBData() {
85 User
::resetIdByNameCache();
88 $user = User
::newFromName( 'UTSysop' );
90 if ( $user->idForName() == 0 ) {
91 $user->addToDatabase();
92 $user->setPassword( 'UTSysopPassword' );
94 $user->addGroup( 'sysop' );
95 $user->addGroup( 'bureaucrat' );
96 $user->saveSettings();
100 //Make 1 page with 1 revision
101 $article = new Article( Title
::newFromText( 'UTPage' ) );
102 $article->doEdit( 'UTContent',
106 User
::newFromName( 'UTSysop' ) );
109 private function initDB() {
111 if ( $wgDBprefix === $this->dbPrefix() ) {
112 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
115 $dbClone = new CloneDatabase( $this->db
, $this->listTables(), $this->dbPrefix() );
116 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
117 $dbClone->cloneTableStructure();
119 if ( $this->db
->getType() == 'oracle' ) {
120 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
122 # Insert 0 user to prevent FK violations
124 $this->db
->insert( 'user', array(
126 'user_name' => 'Anonymous' ) );
131 * Empty all tables so they can be repopulated for tests
133 private function resetDB() {
135 foreach( $this->listTables() as $tbl ) {
136 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
137 $this->db
->delete( $tbl, '*', __METHOD__
);
142 protected function destroyDB() {
143 if ( $this->useTemporaryTables ||
is_null( $this->db
) ) {
144 # Don't need to do anything
148 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
150 foreach ( $tables as $table ) {
152 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
153 $this->db
->query( $sql, __METHOD__
);
154 } catch( MWException
$mwe ) {}
157 if ( $this->db
->getType() == 'oracle' )
158 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
160 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
164 function __call( $func, $args ) {
165 static $compatibility = array(
166 'assertInternalType' => 'assertType',
167 'assertNotInternalType' => 'assertNotType',
168 'assertInstanceOf' => 'assertType',
169 'assertEmpty' => 'assertEmpty2',
172 if ( method_exists( $this->suite
, $func ) ) {
173 return call_user_func_array( array( $this->suite
, $func ), $args);
174 } elseif ( isset( $compatibility[$func] ) ) {
175 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
177 throw new MWException( "Called non-existant $func method on "
178 . get_class( $this ) );
182 private function assertEmpty2( $value, $msg ) {
183 return $this->assertTrue( $value == '', $msg );
186 static private function unprefixTable( $tableName ) {
188 return substr( $tableName, strlen( $wgDBprefix ) );
191 static private function isNotUnittest( $table ) {
192 return strpos( $table, 'unittest_' ) !== 0;
195 protected function listTables() {
198 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
199 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
201 // Don't duplicate test tables from the previous fataled run
202 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
204 if ( $this->db
->getType() == 'sqlite' ) {
205 $tables = array_flip( $tables );
206 // these are subtables of searchindex and don't need to be duped/dropped separately
207 unset( $tables['searchindex_content'] );
208 unset( $tables['searchindex_segdir'] );
209 unset( $tables['searchindex_segments'] );
210 $tables = array_flip( $tables );
215 protected function checkDbIsSupported() {
216 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
217 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
221 public function getCliArg( $offset ) {
223 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
224 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
229 public function setCliArg( $offset, $value ) {
231 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
235 public static function disableInterwikis( $prefix, &$data ) {