Per Reedy, fix for r107386: fix usage of undefined variable
[mediawiki.git] / tests / phpunit / MediaWikiTestCase.php
blob0a2f58be4b9daf8e02e2ace5fb7a534747fcd21c
1 <?php
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
8 /**
9 * @var DatabaseBase
11 protected $db;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 protected $tablesUsed = array(); // tables with data
17 private static $dbSetup = false;
19 /**
20 * Table name prefixes. Oracle likes it shorter.
22 const DB_PREFIX = 'unittest_';
23 const ORA_DB_PREFIX = 'ut_';
25 protected $supportedDBs = array(
26 'mysql',
27 'sqlite',
28 'postgres',
29 'oracle'
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() ) {
47 global $wgDBprefix;
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 ) {
59 $this->initDB();
60 self::$dbSetup = true;
63 $this->addCoreDBData();
64 $this->addDBData();
66 parent::run( $result );
68 $this->resetDB();
69 } else {
70 parent::run( $result );
74 function dbPrefix() {
75 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
78 function needsDB() {
79 $rc = new ReflectionClass( $this );
80 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
83 /**
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
93 # Anonymous user
94 $this->db->insert( 'user', array(
95 'user_id' => 0,
96 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
98 # Insert 0 page to prevent FK violations
99 # Blank page
100 $this->db->insert( 'page', array(
101 'page_id' => 0,
102 'page_namespace' => 0,
103 'page_title' => ' ',
104 'page_restrictions' => NULL,
105 'page_counter' => 0,
106 'page_is_redirect' => 0,
107 'page_is_new' => 0,
108 'page_random' => 0,
109 'page_touched' => $this->db->timestamp(),
110 'page_latest' => 0,
111 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
115 User::resetIdByNameCache();
117 //Make sysop user
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',
133 'UTPageSummary',
134 EDIT_NEW,
135 false,
136 User::newFromName( 'UTSysop' ) );
139 private function initDB() {
140 global $wgDBprefix;
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() );
151 $this->resetDB();
152 return;
153 } else {
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() {
166 if( $this->db ) {
167 if ( $this->db->getType() == 'oracle' ) {
168 if ( $this->useTemporaryTables ) {
169 wfGetLB()->closeAll();
170 $this->db = wfGetDB( DB_MASTER );
171 } else {
172 foreach( $this->tablesUsed as $tbl ) {
173 if( $tbl == 'interwiki') continue;
174 $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
177 } else {
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);
198 } else {
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 ) {
209 global $wgDBprefix;
210 return substr( $tableName, strlen( $wgDBprefix ) );
213 static private function isNotUnittest( $table ) {
214 return strpos( $table, 'unittest_' ) !== 0;
217 protected function listTables() {
218 global $wgDBprefix;
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 );
234 return $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 ) {
258 return false;
262 * Don't throw a warning if $function is deprecated and called later
264 * @param $function String
265 * @return null
267 function hideDeprecated( $function ) {
268 wfSuppressWarnings();
269 wfDeprecated( $function );
270 wfRestoreWarnings();