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(
29 function __construct( $name = null, array $data = array(), $dataName = '' ) {
30 parent
::__construct( $name, $data, $dataName );
32 $this->backupGlobals
= false;
33 $this->backupStaticAttributes
= false;
36 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
37 /* Some functions require some kind of caching, and will end up using the db,
38 * which we can't allow, as that would open a new connection for mysql.
39 * Replace with a HashBag. They would not be going to persist anyway.
41 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
43 if( $this->needsDB() ) {
47 $this->db
= wfGetDB( DB_MASTER
);
49 $this->checkDbIsSupported();
51 $this->oldTablePrefix
= $wgDBprefix;
53 if( !self
::$dbSetup ) {
55 self
::$dbSetup = true;
58 $this->addCoreDBData();
61 parent
::run( $result );
65 parent
::run( $result );
70 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
74 $rc = new ReflectionClass( $this );
75 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
79 * Stub. If a test needs to add additional data to the database, it should
80 * implement this method and do so
82 function addDBData() {}
84 private function addCoreDBData() {
86 User
::resetIdByNameCache();
89 $user = User
::newFromName( 'UTSysop' );
91 if ( $user->idForName() == 0 ) {
92 $user->addToDatabase();
93 $user->setPassword( 'UTSysopPassword' );
95 $user->addGroup( 'sysop' );
96 $user->addGroup( 'bureaucrat' );
97 $user->saveSettings();
101 //Make 1 page with 1 revision
102 $article = new Article( Title
::newFromText( 'UTPage' ) );
103 $article->doEdit( 'UTContent',
107 User
::newFromName( 'UTSysop' ) );
110 private function initDB() {
112 if ( $wgDBprefix === $this->dbPrefix() ) {
113 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
116 $dbClone = new CloneDatabase( $this->db
, $this->listTables(), $this->dbPrefix() );
117 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
118 $dbClone->cloneTableStructure();
120 if ( $this->db
->getType() == 'oracle' ) {
121 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
123 # Insert 0 user to prevent FK violations
125 $this->db
->insert( 'user', array(
127 'user_name' => 'Anonymous' ) );
129 # Insert 0 page to prevent FK violations
131 $this->db
->insert( 'page', array(
133 'page_namespace' => 0,
135 'page_restrictions' => NULL,
137 'page_is_redirect' => 0,
140 'page_touched' => $this->db
->timestamp(),
148 * Empty all tables so they can be repopulated for tests
150 private function resetDB() {
152 foreach( $this->listTables() as $tbl ) {
153 if( $tbl == 'interwiki' ||
$tbl == 'user' ||
$tbl == 'MWUSER' ) continue;
154 if ( $this->db
->getType() == 'oracle' )
155 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
157 $this->db
->delete( $tbl, '*', __METHOD__
);
160 if ( $this->db
->getType() == 'oracle' ) {
161 # Insert 0 page to prevent FK violations
163 $this->db
->insert( 'page', array(
165 'page_namespace' => 0,
167 'page_restrictions' => NULL,
169 'page_is_redirect' => 0,
172 'page_touched' => $this->db
->timestamp(),
179 protected function destroyDB() {
180 if ( $this->useTemporaryTables ||
is_null( $this->db
) ) {
181 # Don't need to do anything
185 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
187 foreach ( $tables as $table ) {
189 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
190 $this->db
->query( $sql, __METHOD__
);
191 } catch( MWException
$mwe ) {}
194 if ( $this->db
->getType() == 'oracle' )
195 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
197 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
201 function __call( $func, $args ) {
202 static $compatibility = array(
203 'assertInternalType' => 'assertType',
204 'assertNotInternalType' => 'assertNotType',
205 'assertInstanceOf' => 'assertType',
206 'assertEmpty' => 'assertEmpty2',
209 if ( method_exists( $this->suite
, $func ) ) {
210 return call_user_func_array( array( $this->suite
, $func ), $args);
211 } elseif ( isset( $compatibility[$func] ) ) {
212 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
214 throw new MWException( "Called non-existant $func method on "
215 . get_class( $this ) );
219 private function assertEmpty2( $value, $msg ) {
220 return $this->assertTrue( $value == '', $msg );
223 static private function unprefixTable( $tableName ) {
225 return substr( $tableName, strlen( $wgDBprefix ) );
228 static private function isNotUnittest( $table ) {
229 return strpos( $table, 'unittest_' ) !== 0;
232 protected function listTables() {
235 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
236 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
238 // Don't duplicate test tables from the previous fataled run
239 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
241 if ( $this->db
->getType() == 'sqlite' ) {
242 $tables = array_flip( $tables );
243 // these are subtables of searchindex and don't need to be duped/dropped separately
244 unset( $tables['searchindex_content'] );
245 unset( $tables['searchindex_segdir'] );
246 unset( $tables['searchindex_segments'] );
247 $tables = array_flip( $tables );
252 protected function checkDbIsSupported() {
253 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
254 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
258 public function getCliArg( $offset ) {
260 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
261 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
266 public function setCliArg( $offset, $value ) {
268 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
272 public static function disableInterwikis( $prefix, &$data ) {
277 * Don't throw a warning if $function is deprecated and called later
279 * @param $function String
282 function hideDeprecated( $function ) {
283 wfSuppressWarnings();
284 wfDeprecated( $function );