3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 private static $dbSetup = false;
18 * Table name prefixes. Oracle likes it shorter.
20 const DB_PREFIX
= 'unittest_';
21 const ORA_DB_PREFIX
= 'ut_';
23 protected $supportedDBs = array(
30 function __construct( $name = null, array $data = array(), $dataName = '' ) {
31 parent
::__construct( $name, $data, $dataName );
33 $this->backupGlobals
= false;
34 $this->backupStaticAttributes
= false;
37 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
38 /* Some functions require some kind of caching, and will end up using the db,
39 * which we can't allow, as that would open a new connection for mysql.
40 * Replace with a HashBag. They would not be going to persist anyway.
42 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
44 if( $this->needsDB() ) {
47 $this->useTemporaryTables
= !$this->getCliArg( 'use-normal-tables' );
48 $this->reuseDB
= $this->getCliArg('reuse-db');
50 $this->db
= wfGetDB( DB_MASTER
);
52 $this->checkDbIsSupported();
54 $this->oldTablePrefix
= $wgDBprefix;
56 if( !self
::$dbSetup ) {
58 self
::$dbSetup = true;
61 $this->addCoreDBData();
64 parent
::run( $result );
68 parent
::run( $result );
73 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
77 $rc = new ReflectionClass( $this );
78 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
82 * Stub. If a test needs to add additional data to the database, it should
83 * implement this method and do so
85 function addDBData() {}
87 private function addCoreDBData() {
88 if ( $this->db
->getType() == 'oracle' ) {
90 # Insert 0 user to prevent FK violations
92 $this->db
->insert( 'user', array(
94 'user_name' => 'Anonymous' ), __METHOD__
, array( 'IGNORE' ) );
96 # Insert 0 page to prevent FK violations
98 $this->db
->insert( 'page', array(
100 'page_namespace' => 0,
102 'page_restrictions' => NULL,
104 'page_is_redirect' => 0,
107 'page_touched' => $this->db
->timestamp(),
109 'page_len' => 0 ), __METHOD__
, array( 'IGNORE' ) );
113 User
::resetIdByNameCache();
116 $user = User
::newFromName( 'UTSysop' );
118 if ( $user->idForName() == 0 ) {
119 $user->addToDatabase();
120 $user->setPassword( 'UTSysopPassword' );
122 $user->addGroup( 'sysop' );
123 $user->addGroup( 'bureaucrat' );
124 $user->saveSettings();
128 //Make 1 page with 1 revision
129 $article = new Article( Title
::newFromText( 'UTPage' ) );
130 $article->doEdit( 'UTContent',
134 User
::newFromName( 'UTSysop' ) );
137 private function initDB() {
139 if ( $wgDBprefix === $this->dbPrefix() ) {
140 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
143 $dbClone = new CloneDatabase( $this->db
, $this->listTables(), $this->dbPrefix() );
144 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
146 if ( ( $this->db
->getType() == 'oracle' ||
!$this->useTemporaryTables
) && $this->reuseDB
) {
147 CloneDatabase
::changePrefix( $this->dbPrefix() );
151 $dbClone->cloneTableStructure();
154 if ( $this->db
->getType() == 'oracle' ) {
155 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
160 * Empty all tables so they can be repopulated for tests
162 private function resetDB() {
164 if ( $this->db
->getType() == 'oracle' ) {
165 if ( $this->useTemporaryTables
) {
166 wfGetLB()->closeAll();
167 $this->db
= wfGetDB( DB_MASTER
);
169 foreach( $this->listTables() as $tbl ) {
170 if( $tbl == 'interwiki') continue;
171 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
175 foreach( $this->listTables() as $tbl ) {
176 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
177 $this->db
->delete( $tbl, '*', __METHOD__
);
183 protected function destroyDB() {
184 if ( is_null( $this->db
) ||
185 ( $this->useTemporaryTables
&& $this->db
->getType() != 'oracle' ) ||
186 ( $this->reuseDB
) ) {
187 # Don't need to do anything
191 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
193 foreach ( $tables as $table ) {
195 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
196 $this->db
->query( $sql, __METHOD__
);
197 } catch( MWException
$mwe ) {}
200 if ( $this->db
->getType() == 'oracle' )
201 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
203 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
207 function __call( $func, $args ) {
208 static $compatibility = array(
209 'assertInternalType' => 'assertType',
210 'assertNotInternalType' => 'assertNotType',
211 'assertInstanceOf' => 'assertType',
212 'assertEmpty' => 'assertEmpty2',
215 if ( method_exists( $this->suite
, $func ) ) {
216 return call_user_func_array( array( $this->suite
, $func ), $args);
217 } elseif ( isset( $compatibility[$func] ) ) {
218 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
220 throw new MWException( "Called non-existant $func method on "
221 . get_class( $this ) );
225 private function assertEmpty2( $value, $msg ) {
226 return $this->assertTrue( $value == '', $msg );
229 static private function unprefixTable( $tableName ) {
231 return substr( $tableName, strlen( $wgDBprefix ) );
234 static private function isNotUnittest( $table ) {
235 return strpos( $table, 'unittest_' ) !== 0;
238 protected function listTables() {
241 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
242 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
244 // Don't duplicate test tables from the previous fataled run
245 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
247 if ( $this->db
->getType() == 'sqlite' ) {
248 $tables = array_flip( $tables );
249 // these are subtables of searchindex and don't need to be duped/dropped separately
250 unset( $tables['searchindex_content'] );
251 unset( $tables['searchindex_segdir'] );
252 unset( $tables['searchindex_segments'] );
253 $tables = array_flip( $tables );
258 protected function checkDbIsSupported() {
259 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
260 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
264 public function getCliArg( $offset ) {
266 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
267 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
272 public function setCliArg( $offset, $value ) {
274 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
278 public static function disableInterwikis( $prefix, &$data ) {
283 * Don't throw a warning if $function is deprecated and called later
285 * @param $function String
288 function hideDeprecated( $function ) {
289 wfSuppressWarnings();
290 wfDeprecated( $function );