* phpunit fixes & optimizations for oracle backend
[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 private static $dbSetup = false;
15
16 /**
17 * Table name prefixes. Oracle likes it shorter.
18 */
19 const DB_PREFIX = 'unittest_';
20 const ORA_DB_PREFIX = 'ut_';
21
22 protected $supportedDBs = array(
23 'mysql',
24 'sqlite',
25 'postgres',
26 'oracle'
27 );
28
29 function __construct( $name = null, array $data = array(), $dataName = '' ) {
30 parent::__construct( $name, $data, $dataName );
31
32 $this->backupGlobals = false;
33 $this->backupStaticAttributes = false;
34 }
35
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.
40 */
41 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
42
43 if( $this->needsDB() ) {
44
45 global $wgDBprefix;
46
47 $this->db = wfGetDB( DB_MASTER );
48
49 $this->checkDbIsSupported();
50
51 $this->oldTablePrefix = $wgDBprefix;
52
53 if( !self::$dbSetup ) {
54 $this->initDB();
55 self::$dbSetup = true;
56 }
57
58 $this->addCoreDBData();
59 $this->addDBData();
60
61 parent::run( $result );
62
63 $this->resetDB();
64 } else {
65 parent::run( $result );
66 }
67 }
68
69 function dbPrefix() {
70 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
71 }
72
73 function needsDB() {
74 $rc = new ReflectionClass( $this );
75 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
76 }
77
78 /**
79 * Stub. If a test needs to add additional data to the database, it should
80 * implement this method and do so
81 */
82 function addDBData() {}
83
84 private function addCoreDBData() {
85
86 User::resetIdByNameCache();
87
88 //Make sysop user
89 $user = User::newFromName( 'UTSysop' );
90
91 if ( $user->idForName() == 0 ) {
92 $user->addToDatabase();
93 $user->setPassword( 'UTSysopPassword' );
94
95 $user->addGroup( 'sysop' );
96 $user->addGroup( 'bureaucrat' );
97 $user->saveSettings();
98 }
99
100
101 //Make 1 page with 1 revision
102 $article = new Article( Title::newFromText( 'UTPage' ) );
103 $article->doEdit( 'UTContent',
104 'UTPageSummary',
105 EDIT_NEW,
106 false,
107 User::newFromName( 'UTSysop' ) );
108 }
109
110 private function initDB() {
111 global $wgDBprefix;
112 if ( $wgDBprefix === $this->dbPrefix() ) {
113 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
114 }
115
116 $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
117 $dbClone->useTemporaryTables( $this->useTemporaryTables );
118 $dbClone->cloneTableStructure();
119
120 if ( $this->db->getType() == 'oracle' ) {
121 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
122
123 # Insert 0 user to prevent FK violations
124 # Anonymous user
125 $this->db->insert( 'user', array(
126 'user_id' => 0,
127 'user_name' => 'Anonymous' ) );
128
129 # Insert 0 page to prevent FK violations
130 # Blank page
131 $this->db->insert( 'page', array(
132 'page_id' => 0,
133 'page_namespace' => 0,
134 'page_title' => ' ',
135 'page_restrictions' => NULL,
136 'page_counter' => 0,
137 'page_is_redirect' => 0,
138 'page_is_new' => 0,
139 'page_random' => 0,
140 'page_touched' => $this->db->timestamp(),
141 'page_latest' => 0,
142 'page_len' => 0 ) );
143
144 }
145 }
146
147 /**
148 * Empty all tables so they can be repopulated for tests
149 */
150 private function resetDB() {
151 if( $this->db ) {
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__ );
156 else
157 $this->db->delete( $tbl, '*', __METHOD__ );
158 }
159
160 if ( $this->db->getType() == 'oracle' ) {
161 # Insert 0 page to prevent FK violations
162 # Blank page
163 $this->db->insert( 'page', array(
164 'page_id' => 0,
165 'page_namespace' => 0,
166 'page_title' => ' ',
167 'page_restrictions' => NULL,
168 'page_counter' => 0,
169 'page_is_redirect' => 0,
170 'page_is_new' => 0,
171 'page_random' => 0,
172 'page_touched' => $this->db->timestamp(),
173 'page_latest' => 0,
174 'page_len' => 0 ) );
175 }
176 }
177 }
178
179 protected function destroyDB() {
180 if ( $this->useTemporaryTables || is_null( $this->db ) ) {
181 # Don't need to do anything
182 return;
183 }
184
185 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
186
187 foreach ( $tables as $table ) {
188 try {
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 ) {}
192 }
193
194 if ( $this->db->getType() == 'oracle' )
195 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
196
197 CloneDatabase::changePrefix( $this->oldTablePrefix );
198 }
199
200
201 function __call( $func, $args ) {
202 static $compatibility = array(
203 'assertInternalType' => 'assertType',
204 'assertNotInternalType' => 'assertNotType',
205 'assertInstanceOf' => 'assertType',
206 'assertEmpty' => 'assertEmpty2',
207 );
208
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);
213 } else {
214 throw new MWException( "Called non-existant $func method on "
215 . get_class( $this ) );
216 }
217 }
218
219 private function assertEmpty2( $value, $msg ) {
220 return $this->assertTrue( $value == '', $msg );
221 }
222
223 static private function unprefixTable( $tableName ) {
224 global $wgDBprefix;
225 return substr( $tableName, strlen( $wgDBprefix ) );
226 }
227
228 static private function isNotUnittest( $table ) {
229 return strpos( $table, 'unittest_' ) !== 0;
230 }
231
232 protected function listTables() {
233 global $wgDBprefix;
234
235 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
236 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
237
238 // Don't duplicate test tables from the previous fataled run
239 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
240
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 );
248 }
249 return $tables;
250 }
251
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." );
255 }
256 }
257
258 public function getCliArg( $offset ) {
259
260 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
261 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
262 }
263
264 }
265
266 public function setCliArg( $offset, $value ) {
267
268 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
269
270 }
271
272 public static function disableInterwikis( $prefix, &$data ) {
273 return false;
274 }
275
276 /**
277 * Don't throw a warning if $function is deprecated and called later
278 *
279 * @param $function String
280 * @return null
281 */
282 function hideDeprecated( $function ) {
283 wfSuppressWarnings();
284 wfDeprecated( $function );
285 wfRestoreWarnings();
286 }
287 }