* Made resetDB() actually clear out the tables rather than leave the contents
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
1 <?php
2
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
7
8 /**
9 * @var DatabaseBase
10 */
11 protected $db;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 private static $dbSetup = false;
16 private static $tablesCloned = array();
17
18 /**
19 * Table name prefixes. Oracle likes it shorter.
20 */
21 const DB_PREFIX = 'unittest_';
22 const ORA_DB_PREFIX = 'ut_';
23
24 protected $supportedDBs = array(
25 'mysql',
26 'sqlite',
27 'postgres',
28 'oracle'
29 );
30
31 function __construct( $name = null, array $data = array(), $dataName = '' ) {
32 parent::__construct( $name, $data, $dataName );
33
34 $this->backupGlobals = false;
35 $this->backupStaticAttributes = false;
36 }
37
38 function run( PHPUnit_Framework_TestResult $result = NULL ) {
39 /* Some functions require some kind of caching, and will end up using the db,
40 * which we can't allow, as that would open a new connection for mysql.
41 * Replace with a HashBag. They would not be going to persist anyway.
42 */
43 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
44
45 if( $this->needsDB() ) {
46 global $wgDBprefix;
47
48 $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
49 $this->reuseDB = $this->getCliArg('reuse-db');
50
51 $this->db = wfGetDB( DB_MASTER );
52
53 $this->checkDbIsSupported();
54
55 $this->oldTablePrefix = $wgDBprefix;
56
57 if( !self::$dbSetup ) {
58 self::$tablesCloned = $this->initDB();
59 self::$dbSetup = true;
60 }
61
62 $this->addCoreDBData();
63 $this->addDBData();
64
65 parent::run( $result );
66
67 $this->resetDB();
68 } else {
69 parent::run( $result );
70 }
71 }
72
73 function dbPrefix() {
74 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
75 }
76
77 function needsDB() {
78 $rc = new ReflectionClass( $this );
79 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
80 }
81
82 /**
83 * Stub. If a test needs to add additional data to the database, it should
84 * implement this method and do so
85 */
86 function addDBData() {}
87
88 private function addCoreDBData() {
89 if ( $this->db->getType() == 'oracle' ) {
90
91 # Insert 0 user to prevent FK violations
92 # Anonymous user
93 $this->db->insert( 'user', array(
94 'user_id' => 0,
95 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
96
97 # Insert 0 page to prevent FK violations
98 # Blank page
99 $this->db->insert( 'page', array(
100 'page_id' => 0,
101 'page_namespace' => 0,
102 'page_title' => ' ',
103 'page_restrictions' => NULL,
104 'page_counter' => 0,
105 'page_is_redirect' => 0,
106 'page_is_new' => 0,
107 'page_random' => 0,
108 'page_touched' => $this->db->timestamp(),
109 'page_latest' => 0,
110 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
111
112 }
113
114 User::resetIdByNameCache();
115
116 //Make sysop user
117 $user = User::newFromName( 'UTSysop' );
118
119 if ( $user->idForName() == 0 ) {
120 $user->addToDatabase();
121 $user->setPassword( 'UTSysopPassword' );
122
123 $user->addGroup( 'sysop' );
124 $user->addGroup( 'bureaucrat' );
125 $user->saveSettings();
126 }
127
128
129 //Make 1 page with 1 revision
130 $article = new Article( Title::newFromText( 'UTPage' ) );
131 $article->doEdit( 'UTContent',
132 'UTPageSummary',
133 EDIT_NEW,
134 false,
135 User::newFromName( 'UTSysop' ) );
136 }
137
138 private function initDB() {
139 global $wgDBprefix;
140 if ( $wgDBprefix === $this->dbPrefix() ) {
141 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
142 }
143
144 $tablesCloned = $this->listTables();
145 $dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
146 $dbClone->useTemporaryTables( $this->useTemporaryTables );
147
148 if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
149 CloneDatabase::changePrefix( $this->dbPrefix() );
150 $this->resetDB();
151 return;
152 } else {
153 $dbClone->cloneTableStructure();
154 }
155
156 if ( $this->db->getType() == 'oracle' ) {
157 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
158 }
159
160 return $tablesCloned;
161 }
162
163 /**
164 * Empty all tables so they can be repopulated for tests
165 */
166 private function resetDB() {
167 if( $this->db ) {
168 if ( $this->db->getType() == 'oracle' ) {
169 if ( $this->useTemporaryTables ) {
170 wfGetLB()->closeAll();
171 $this->db = wfGetDB( DB_MASTER );
172 } else {
173 foreach( self::$tablesCloned as $tbl ) {
174 if( $tbl == 'interwiki') continue;
175 $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
176 }
177 }
178 } else {
179 foreach( self::$tablesCloned as $tbl ) {
180 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
181 $this->db->delete( $tbl, '*', __METHOD__ );
182 }
183 }
184 }
185 }
186
187 function __call( $func, $args ) {
188 static $compatibility = array(
189 'assertInternalType' => 'assertType',
190 'assertNotInternalType' => 'assertNotType',
191 'assertInstanceOf' => 'assertType',
192 'assertEmpty' => 'assertEmpty2',
193 );
194
195 if ( method_exists( $this->suite, $func ) ) {
196 return call_user_func_array( array( $this->suite, $func ), $args);
197 } elseif ( isset( $compatibility[$func] ) ) {
198 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
199 } else {
200 throw new MWException( "Called non-existant $func method on "
201 . get_class( $this ) );
202 }
203 }
204
205 private function assertEmpty2( $value, $msg ) {
206 return $this->assertTrue( $value == '', $msg );
207 }
208
209 static private function unprefixTable( $tableName ) {
210 global $wgDBprefix;
211 return substr( $tableName, strlen( $wgDBprefix ) );
212 }
213
214 static private function isNotUnittest( $table ) {
215 return strpos( $table, 'unittest_' ) !== 0;
216 }
217
218 protected function listTables() {
219 global $wgDBprefix;
220
221 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
222 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
223
224 // Don't duplicate test tables from the previous fataled run
225 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
226
227 if ( $this->db->getType() == 'sqlite' ) {
228 $tables = array_flip( $tables );
229 // these are subtables of searchindex and don't need to be duped/dropped separately
230 unset( $tables['searchindex_content'] );
231 unset( $tables['searchindex_segdir'] );
232 unset( $tables['searchindex_segments'] );
233 $tables = array_flip( $tables );
234 }
235 return $tables;
236 }
237
238 protected function checkDbIsSupported() {
239 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
240 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
241 }
242 }
243
244 public function getCliArg( $offset ) {
245
246 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
247 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
248 }
249
250 }
251
252 public function setCliArg( $offset, $value ) {
253
254 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
255
256 }
257
258 public static function disableInterwikis( $prefix, &$data ) {
259 return false;
260 }
261
262 /**
263 * Don't throw a warning if $function is deprecated and called later
264 *
265 * @param $function String
266 * @return null
267 */
268 function hideDeprecated( $function ) {
269 wfSuppressWarnings();
270 wfDeprecated( $function );
271 wfRestoreWarnings();
272 }
273 }