r108300: updated parserTest.inc tests and re-enabled testBug29408()
[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 protected $tablesUsed = array(); // tables with data
16
17 private static $dbSetup = false;
18
19 /**
20 * Table name prefixes. Oracle likes it shorter.
21 */
22 const DB_PREFIX = 'unittest_';
23 const ORA_DB_PREFIX = 'ut_';
24
25 protected $supportedDBs = array(
26 'mysql',
27 'sqlite',
28 'postgres',
29 'oracle'
30 );
31
32 function __construct( $name = null, array $data = array(), $dataName = '' ) {
33 parent::__construct( $name, $data, $dataName );
34
35 $this->backupGlobals = false;
36 $this->backupStaticAttributes = false;
37 }
38
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.
43 */
44 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
45
46 if( $this->needsDB() ) {
47 global $wgDBprefix;
48
49 $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
50 $this->reuseDB = $this->getCliArg('reuse-db');
51
52 $this->db = wfGetDB( DB_MASTER );
53
54 $this->checkDbIsSupported();
55
56 $this->oldTablePrefix = $wgDBprefix;
57
58 if( !self::$dbSetup ) {
59 $this->initDB();
60 self::$dbSetup = true;
61 }
62
63 $this->addCoreDBData();
64 $this->addDBData();
65
66 parent::run( $result );
67
68 $this->resetDB();
69 } else {
70 parent::run( $result );
71 }
72 }
73
74 function dbPrefix() {
75 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
76 }
77
78 function needsDB() {
79 $rc = new ReflectionClass( $this );
80 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
81 }
82
83 /**
84 * Stub. If a test needs to add additional data to the database, it should
85 * implement this method and do so
86 */
87 function addDBData() {}
88
89 private function addCoreDBData() {
90 if ( $this->db->getType() == 'oracle' ) {
91
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' ) );
97
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' ) );
112
113 }
114
115 User::resetIdByNameCache();
116
117 //Make sysop user
118 $user = User::newFromName( 'UTSysop' );
119
120 if ( $user->idForName() == 0 ) {
121 $user->addToDatabase();
122 $user->setPassword( 'UTSysopPassword' );
123
124 $user->addGroup( 'sysop' );
125 $user->addGroup( 'bureaucrat' );
126 $user->saveSettings();
127 }
128
129
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' ) );
137 }
138
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_"' );
143 }
144
145 $tablesCloned = $this->listTables();
146 $dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
147 $dbClone->useTemporaryTables( $this->useTemporaryTables );
148
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();
155 }
156
157 if ( $this->db->getType() == 'oracle' ) {
158 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
159 }
160 }
161
162 /**
163 * Empty all tables so they can be repopulated for tests
164 */
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__ );
175 }
176 }
177 } else {
178 foreach( $this->tablesUsed as $tbl ) {
179 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
180 $this->db->delete( $tbl, '*', __METHOD__ );
181 }
182 }
183 }
184 }
185
186 function __call( $func, $args ) {
187 static $compatibility = array(
188 'assertInternalType' => 'assertType',
189 'assertNotInternalType' => 'assertNotType',
190 'assertInstanceOf' => 'assertType',
191 'assertEmpty' => 'assertEmpty2',
192 );
193
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 ) );
201 }
202 }
203
204 private function assertEmpty2( $value, $msg ) {
205 return $this->assertTrue( $value == '', $msg );
206 }
207
208 static private function unprefixTable( $tableName ) {
209 global $wgDBprefix;
210 return substr( $tableName, strlen( $wgDBprefix ) );
211 }
212
213 static private function isNotUnittest( $table ) {
214 return strpos( $table, 'unittest_' ) !== 0;
215 }
216
217 protected function listTables() {
218 global $wgDBprefix;
219
220 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
221 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
222
223 // Don't duplicate test tables from the previous fataled run
224 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
225
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 );
233 }
234 return $tables;
235 }
236
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." );
240 }
241 }
242
243 public function getCliArg( $offset ) {
244
245 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
246 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
247 }
248
249 }
250
251 public function setCliArg( $offset, $value ) {
252
253 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
254
255 }
256
257 public static function disableInterwikis( $prefix, &$data ) {
258 return false;
259 }
260
261 /**
262 * Don't throw a warning if $function is deprecated and called later
263 *
264 * @param $function String
265 * @return null
266 */
267 function hideDeprecated( $function ) {
268 wfSuppressWarnings();
269 wfDeprecated( $function );
270 wfRestoreWarnings();
271 }
272 }