Exclude Block tests until somebody fixes them, also cache listTables() result so...
[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 private static $dbTables = null;
16
17 /**
18 * Table name prefixes. Oracle likes it shorter.
19 */
20 const DB_PREFIX = 'unittest_';
21 const ORA_DB_PREFIX = 'ut_';
22
23 protected $supportedDBs = array(
24 'mysql',
25 'sqlite',
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 }
130
131 /**
132 * Empty all tables so they can be repopulated for tests
133 */
134 private function resetDB() {
135 if( $this->db ) {
136 foreach( $this->listTables() as $tbl ) {
137 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
138 $this->db->delete( $tbl, '*', __METHOD__ );
139 }
140 }
141 }
142
143 protected function destroyDB() {
144 if ( $this->useTemporaryTables || is_null( $this->db ) ) {
145 # Don't need to do anything
146 return;
147 }
148
149 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
150
151 foreach ( $tables as $table ) {
152 try {
153 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
154 $this->db->query( $sql, __METHOD__ );
155 } catch( MWException $mwe ) {}
156 }
157
158 if ( $this->db->getType() == 'oracle' )
159 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
160
161 CloneDatabase::changePrefix( $this->oldTablePrefix );
162 }
163
164
165 function __call( $func, $args ) {
166 static $compatibility = array(
167 'assertInternalType' => 'assertType',
168 'assertNotInternalType' => 'assertNotType',
169 'assertInstanceOf' => 'assertType',
170 'assertEmpty' => 'assertEmpty2',
171 );
172
173 if ( method_exists( $this->suite, $func ) ) {
174 return call_user_func_array( array( $this->suite, $func ), $args);
175 } elseif ( isset( $compatibility[$func] ) ) {
176 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
177 } else {
178 throw new MWException( "Called non-existant $func method on "
179 . get_class( $this ) );
180 }
181 }
182
183 private function assertEmpty2( $value, $msg ) {
184 return $this->assertTrue( $value == '', $msg );
185 }
186
187 static private function unprefixTable( $tableName ) {
188 global $wgDBprefix;
189 return substr( $tableName, strlen( $wgDBprefix ) );
190 }
191
192 protected function listTables() {
193 global $wgDBprefix;
194
195 if( is_null( self::$dbTables ) ) {
196 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
197 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
198
199 if ( $this->db->getType() == 'sqlite' ) {
200 $tables = array_flip( $tables );
201 // these are subtables of searchindex and don't need to be duped/dropped separately
202 unset( $tables['searchindex_content'] );
203 unset( $tables['searchindex_segdir'] );
204 unset( $tables['searchindex_segments'] );
205 $tables = array_flip( $tables );
206 }
207 self::$dbTables = $tables;
208 }
209 return self::$dbTables;
210
211 }
212
213 protected function checkDbIsSupported() {
214 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
215 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
216 }
217 }
218
219 public function getCliArg( $offset ) {
220
221 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
222 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
223 }
224
225 }
226
227 public function setCliArg( $offset, $value ) {
228
229 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
230
231 }
232 }
233