3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 protected $tablesUsed = array(); // tables with data
17 private static $dbSetup = false;
20 * Table name prefixes. Oracle likes it shorter.
22 const DB_PREFIX
= 'unittest_';
23 const ORA_DB_PREFIX
= 'ut_';
25 protected $supportedDBs = array(
32 function __construct( $name = null, array $data = array(), $dataName = '' ) {
33 parent
::__construct( $name, $data, $dataName );
35 $this->backupGlobals
= false;
36 $this->backupStaticAttributes
= false;
39 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
40 /* Some functions require some kind of caching, and will end up using the db,
41 * which we can't allow, as that would open a new connection for mysql.
42 * Replace with a HashBag. They would not be going to persist anyway.
44 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
46 if( $this->needsDB() ) {
49 $this->useTemporaryTables
= !$this->getCliArg( 'use-normal-tables' );
50 $this->reuseDB
= $this->getCliArg('reuse-db');
52 $this->db
= wfGetDB( DB_MASTER
);
54 $this->checkDbIsSupported();
56 $this->oldTablePrefix
= $wgDBprefix;
58 if( !self
::$dbSetup ) {
60 self
::$dbSetup = true;
63 $this->addCoreDBData();
66 parent
::run( $result );
70 parent
::run( $result );
75 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
79 $rc = new ReflectionClass( $this );
80 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
84 * Stub. If a test needs to add additional data to the database, it should
85 * implement this method and do so
87 function addDBData() {}
89 private function addCoreDBData() {
90 if ( $this->db
->getType() == 'oracle' ) {
92 # Insert 0 user to prevent FK violations
94 $this->db
->insert( 'user', array(
96 'user_name' => 'Anonymous' ), __METHOD__
, array( 'IGNORE' ) );
98 # Insert 0 page to prevent FK violations
100 $this->db
->insert( 'page', array(
102 'page_namespace' => 0,
104 'page_restrictions' => NULL,
106 'page_is_redirect' => 0,
109 'page_touched' => $this->db
->timestamp(),
111 'page_len' => 0 ), __METHOD__
, array( 'IGNORE' ) );
115 User
::resetIdByNameCache();
118 $user = User
::newFromName( 'UTSysop' );
120 if ( $user->idForName() == 0 ) {
121 $user->addToDatabase();
122 $user->setPassword( 'UTSysopPassword' );
124 $user->addGroup( 'sysop' );
125 $user->addGroup( 'bureaucrat' );
126 $user->saveSettings();
130 //Make 1 page with 1 revision
131 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
132 $page->doEdit( 'UTContent',
136 User
::newFromName( 'UTSysop' ) );
139 private function initDB() {
141 if ( $wgDBprefix === $this->dbPrefix() ) {
142 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
145 $tablesCloned = $this->listTables();
146 $dbClone = new CloneDatabase( $this->db
, $tablesCloned, $this->dbPrefix() );
147 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
149 if ( ( $this->db
->getType() == 'oracle' ||
!$this->useTemporaryTables
) && $this->reuseDB
) {
150 CloneDatabase
::changePrefix( $this->dbPrefix() );
154 $dbClone->cloneTableStructure();
157 if ( $this->db
->getType() == 'oracle' ) {
158 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
163 * Empty all tables so they can be repopulated for tests
165 private function resetDB() {
167 if ( $this->db
->getType() == 'oracle' ) {
168 if ( $this->useTemporaryTables
) {
169 wfGetLB()->closeAll();
170 $this->db
= wfGetDB( DB_MASTER
);
172 foreach( $this->tablesUsed
as $tbl ) {
173 if( $tbl == 'interwiki') continue;
174 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
178 foreach( $this->tablesUsed
as $tbl ) {
179 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
180 $this->db
->delete( $tbl, '*', __METHOD__
);
186 function __call( $func, $args ) {
187 static $compatibility = array(
188 'assertInternalType' => 'assertType',
189 'assertNotInternalType' => 'assertNotType',
190 'assertInstanceOf' => 'assertType',
191 'assertEmpty' => 'assertEmpty2',
194 if ( method_exists( $this->suite
, $func ) ) {
195 return call_user_func_array( array( $this->suite
, $func ), $args);
196 } elseif ( isset( $compatibility[$func] ) ) {
197 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
199 throw new MWException( "Called non-existant $func method on "
200 . get_class( $this ) );
204 private function assertEmpty2( $value, $msg ) {
205 return $this->assertTrue( $value == '', $msg );
208 static private function unprefixTable( $tableName ) {
210 return substr( $tableName, strlen( $wgDBprefix ) );
213 static private function isNotUnittest( $table ) {
214 return strpos( $table, 'unittest_' ) !== 0;
217 protected function listTables() {
220 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
221 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
223 // Don't duplicate test tables from the previous fataled run
224 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
226 if ( $this->db
->getType() == 'sqlite' ) {
227 $tables = array_flip( $tables );
228 // these are subtables of searchindex and don't need to be duped/dropped separately
229 unset( $tables['searchindex_content'] );
230 unset( $tables['searchindex_segdir'] );
231 unset( $tables['searchindex_segments'] );
232 $tables = array_flip( $tables );
237 protected function checkDbIsSupported() {
238 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
239 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
243 public function getCliArg( $offset ) {
245 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
246 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
251 public function setCliArg( $offset, $value ) {
253 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
257 public static function disableInterwikis( $prefix, &$data ) {
262 * Don't throw a warning if $function is deprecated and called later
264 * @param $function String
267 function hideDeprecated( $function ) {
268 wfSuppressWarnings();
269 wfDeprecated( $function );