Follow-Up r85618:
[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 protected $db;
9 protected $dbClone;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
12
13 /**
14 * Table name prefixes. Oracle likes it shorter.
15 */
16 const DB_PREFIX = 'unittest_';
17 const ORA_DB_PREFIX = 'ut_';
18
19 protected $supportedDBs = array(
20 'mysql',
21 'sqlite',
22 'oracle'
23 );
24
25 function __construct( $name = null, array $data = array(), $dataName = '' ) {
26 parent::__construct( $name, $data, $dataName );
27
28 $this->backupGlobals = false;
29 $this->backupStaticAttributes = false;
30 }
31
32 function run( PHPUnit_Framework_TestResult $result = NULL ) {
33 /* Some functions require some kind of caching, and will end up using the db,
34 * which we can't allow, as that would open a new connection for mysql.
35 * Replace with a HashBag. They would not be going to persist anyway.
36 */
37 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
38
39 if( $this->needsDB() ) {
40
41 global $wgDBprefix;
42
43 $this->db = wfGetDB( DB_MASTER );
44
45 $this->checkDbIsSupported();
46
47 $this->oldTablePrefix = $wgDBprefix;
48
49 $this->destroyDB();
50
51 $this->initDB();
52 $this->addCoreDBData();
53 $this->addDBData();
54
55 parent::run( $result );
56
57 $this->destroyDB();
58 }
59 else {
60 parent::run( $result );
61
62 }
63
64 }
65
66 function __destruct() {
67 if( $this->needsDB() ) {
68 $this->destroyDB();
69 }
70 }
71
72 function dbPrefix() {
73 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
74 }
75
76 function needsDB() {
77 $rc = new ReflectionClass( $this );
78 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
79 }
80
81 /**
82 * Stub. If a test needs to add additional data to the database, it should
83 * implement this method and do so
84 */
85 function addDBData() {}
86
87 private function addCoreDBData() {
88
89 User::resetIdByNameCache();
90
91 //Make sysop user
92 $user = User::newFromName( 'UTSysop' );
93
94 if ( $user->idForName() == 0 ) {
95 $user->addToDatabase();
96 $user->setPassword( 'UTSysopPassword' );
97
98 $user->addGroup( 'sysop' );
99 $user->addGroup( 'bureaucrat' );
100 $user->saveSettings();
101 }
102
103
104 //Make 1 page with 1 revision
105 $article = new Article( Title::newFromText( 'UTPage' ) );
106 $article->doEdit( 'UTContent',
107 'UTPageSummary',
108 EDIT_NEW,
109 false,
110 User::newFromName( 'UTSysop' ) );
111 }
112
113 private function initDB() {
114 global $wgDBprefix;
115
116 $dbType = $this->db->getType();
117
118 if ( $wgDBprefix === $this->dbPrefix() ) {
119 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
120 }
121
122 $tables = $this->listTables();
123
124 $this->dbClone = new CloneDatabase( $this->db, $tables, $this->dbPrefix() );
125 $this->dbClone->useTemporaryTables( $this->useTemporaryTables );
126 $this->dbClone->cloneTableStructure();
127
128 if ( $dbType == 'oracle' )
129 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
130
131 if ( $dbType == 'oracle' ) {
132 # Insert 0 user to prevent FK violations
133
134 # Anonymous user
135 $this->db->insert( 'user', array(
136 'user_id' => 0,
137 'user_name' => 'Anonymous' ) );
138 }
139
140 }
141
142 protected function destroyDB() {
143
144 if ( $this->useTemporaryTables ) {
145 # Don't need to do anything
146 //return;
147 //Temporary tables seem to be broken ATM, delete anyway
148 }
149
150 if( is_null( $this->db ) ) {
151 return;
152 }
153
154 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
155
156 foreach ( $tables as $table ) {
157 try {
158 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
159 $this->db->query( $sql, __METHOD__ );
160 } catch( Exception $e ) {
161 }
162 }
163
164 if ( $this->db->getType() == 'oracle' )
165 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
166
167 CloneDatabase::changePrefix( $this->oldTablePrefix );
168 }
169
170 function __call( $func, $args ) {
171 static $compatibility = array(
172 'assertInternalType' => 'assertType',
173 'assertNotInternalType' => 'assertNotType',
174 'assertInstanceOf' => 'assertType',
175 'assertEmpty' => 'assertEmpty2',
176 );
177
178 if ( method_exists( $this->suite, $func ) ) {
179 return call_user_func_array( array( $this->suite, $func ), $args);
180 } elseif ( isset( $compatibility[$func] ) ) {
181 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
182 } else {
183 throw new MWException( "Called non-existant $func method on "
184 . get_class( $this ) );
185 }
186 }
187
188 private function assertEmpty2( $value, $msg ) {
189 return $this->assertTrue( $value == '', $msg );
190 }
191
192 static private function unprefixTable( $tableName ) {
193 global $wgDBprefix;
194 return substr( $tableName, strlen( $wgDBprefix ) );
195 }
196
197 protected function listTables() {
198 global $wgDBprefix;
199
200 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
201 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
202
203 if ( $this->db->getType() == 'sqlite' ) {
204 $tables = array_flip( $tables );
205 // these are subtables of searchindex and don't need to be duped/dropped separately
206 unset( $tables['searchindex_content'] );
207 unset( $tables['searchindex_segdir'] );
208 unset( $tables['searchindex_segments'] );
209 $tables = array_flip( $tables );
210 }
211 return $tables;
212
213 }
214
215 protected function checkDbIsSupported() {
216 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
217 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
218 }
219 }
220
221 public function getCliArg( $offset ) {
222
223 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
224 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
225 }
226
227 }
228
229 public function setCliArg( $offset, $value ) {
230
231 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
232
233 }
234 }
235