Reset the cache used in User::idFromName(), otherwise tests will try to add the user...
[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 if ($name !== null) {
27 $this->setName($name);
28 }
29
30 $this->data = $data;
31 $this->dataName = $dataName;
32
33 $this->backupGlobals = false;
34 $this->backupStaticAttributes = false;
35 }
36
37 function run( PHPUnit_Framework_TestResult $result = NULL ) {
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 needsDB() {
73 $rc = new ReflectionClass( $this );
74 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
75 }
76
77 /**
78 * Stub. If a test needs to add additional data to the database, it should
79 * implement this method and do so
80 */
81 function addDBData() {}
82
83 private function addCoreDBData() {
84
85 User::resetIdByNameCache();
86
87 //Make sysop user
88 $user = User::newFromName( 'UTSysop' );
89
90 if ( $user->idForName() == 0 ) {
91 $user->addToDatabase();
92 $user->setPassword( 'UTSysopPassword' );
93
94 $user->addGroup( 'sysop' );
95 $user->addGroup( 'bureaucrat' );
96 $user->saveSettings();
97 }
98
99
100 //Make 1 page with 1 revision
101 $article = new Article( Title::newFromText( 'UTPage' ) );
102 $article->doEdit( 'UTContent',
103 'UTPageSummary',
104 EDIT_NEW,
105 false,
106 User::newFromName( 'UTSysop' ) );
107 }
108
109 private function initDB() {
110 global $wgDBprefix;
111
112 $dbType = $this->db->getType();
113
114 if ( $wgDBprefix === self::DB_PREFIX || ( $dbType == 'oracle' && $wgDBprefix === self::ORA_DB_PREFIX ) ) {
115 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
116 }
117
118 $tables = $this->listTables();
119
120 $prefix = $dbType != 'oracle' ? self::DB_PREFIX : self::ORA_DB_PREFIX;
121
122 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
123 $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
124 $this->dbClone->cloneTableStructure();
125
126 if ( $dbType == 'oracle' )
127 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
128
129 if ( $dbType == 'oracle' ) {
130 # Insert 0 user to prevent FK violations
131
132 # Anonymous user
133 $this->db->insert( 'user', array(
134 'user_id' => 0,
135 'user_name' => 'Anonymous' ) );
136 }
137
138 }
139
140 protected function destroyDB() {
141
142 if ( $this->useTemporaryTables ) {
143 # Don't need to do anything
144 //return;
145 //Temporary tables seem to be broken ATM, delete anyway
146 }
147
148 if( is_null( $this->db ) ) {
149 return;
150 }
151
152 if( $this->db->getType() == 'oracle' ) {
153 $tables = $this->db->listTables( self::ORA_DB_PREFIX, __METHOD__ );
154 }
155 else {
156 $tables = $this->db->listTables( self::DB_PREFIX, __METHOD__ );
157 }
158
159 foreach ( $tables as $table ) {
160 try {
161 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
162 $this->db->query( $sql, __METHOD__ );
163 } catch( Exception $e ) {
164 }
165 }
166
167 if ( $this->db->getType() == 'oracle' )
168 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
169
170 CloneDatabase::changePrefix( $this->oldTablePrefix );
171 }
172
173 function __call( $func, $args ) {
174 static $compatibility = array(
175 'assertInternalType' => 'assertType',
176 'assertNotInternalType' => 'assertNotType',
177 'assertInstanceOf' => 'assertType',
178 );
179
180 if ( method_exists( $this->suite, $func ) ) {
181 return call_user_func_array( array( $this->suite, $func ), $args);
182 } elseif ( isset( $compatibility[$func] ) ) {
183 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
184 } else {
185 throw new MWException( "Called non-existant $func method on "
186 . get_class( $this ) );
187 }
188 }
189
190 static private function unprefixTable( $tableName ) {
191 global $wgDBprefix;
192 return substr( $tableName, strlen( $wgDBprefix ) );
193 }
194
195 protected function listTables() {
196 global $wgDBprefix;
197
198 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
199 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
200 return $tables;
201
202 }
203
204 protected function checkDbIsSupported() {
205 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
206 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
207 }
208 }
209
210 public function getCliArg( $offset ) {
211
212 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
213 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
214 }
215
216 }
217
218 public function setCliArg( $offset, $value ) {
219
220 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
221
222 }
223 }
224