UploadTest::testTitleValidation accessed the database to fetch the interwikis.
[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 'oracle'
26 );
27
28 function __construct( $name = null, array $data = array(), $dataName = '' ) {
29 parent::__construct( $name, $data, $dataName );
30
31 $this->backupGlobals = false;
32 $this->backupStaticAttributes = false;
33 }
34
35 function run( PHPUnit_Framework_TestResult $result = NULL ) {
36 /* Some functions require some kind of caching, and will end up using the db,
37 * which we can't allow, as that would open a new connection for mysql.
38 * Replace with a HashBag. They would not be going to persist anyway.
39 */
40 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
41
42 if( $this->needsDB() ) {
43
44 global $wgDBprefix;
45
46 $this->db = wfGetDB( DB_MASTER );
47
48 $this->checkDbIsSupported();
49
50 $this->oldTablePrefix = $wgDBprefix;
51
52 if( !self::$dbSetup ) {
53 $this->initDB();
54 self::$dbSetup = true;
55 }
56
57 $this->addCoreDBData();
58 $this->addDBData();
59
60 parent::run( $result );
61
62 $this->resetDB();
63 } else {
64 parent::run( $result );
65 }
66 }
67
68 function dbPrefix() {
69 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
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 if ( $wgDBprefix === $this->dbPrefix() ) {
112 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
113 }
114
115 $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
116 $dbClone->useTemporaryTables( $this->useTemporaryTables );
117 $dbClone->cloneTableStructure();
118
119 if ( $this->db->getType() == 'oracle' ) {
120 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
121
122 # Insert 0 user to prevent FK violations
123 # Anonymous user
124 $this->db->insert( 'user', array(
125 'user_id' => 0,
126 'user_name' => 'Anonymous' ) );
127 }
128 }
129
130 /**
131 * Empty all tables so they can be repopulated for tests
132 */
133 private function resetDB() {
134 if( $this->db ) {
135 foreach( $this->listTables() as $tbl ) {
136 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
137 $this->db->delete( $tbl, '*', __METHOD__ );
138 }
139 }
140 }
141
142 protected function destroyDB() {
143 if ( $this->useTemporaryTables || is_null( $this->db ) ) {
144 # Don't need to do anything
145 return;
146 }
147
148 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
149
150 foreach ( $tables as $table ) {
151 try {
152 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
153 $this->db->query( $sql, __METHOD__ );
154 } catch( MWException $mwe ) {}
155 }
156
157 if ( $this->db->getType() == 'oracle' )
158 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
159
160 CloneDatabase::changePrefix( $this->oldTablePrefix );
161 }
162
163
164 function __call( $func, $args ) {
165 static $compatibility = array(
166 'assertInternalType' => 'assertType',
167 'assertNotInternalType' => 'assertNotType',
168 'assertInstanceOf' => 'assertType',
169 'assertEmpty' => 'assertEmpty2',
170 );
171
172 if ( method_exists( $this->suite, $func ) ) {
173 return call_user_func_array( array( $this->suite, $func ), $args);
174 } elseif ( isset( $compatibility[$func] ) ) {
175 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
176 } else {
177 throw new MWException( "Called non-existant $func method on "
178 . get_class( $this ) );
179 }
180 }
181
182 private function assertEmpty2( $value, $msg ) {
183 return $this->assertTrue( $value == '', $msg );
184 }
185
186 static private function unprefixTable( $tableName ) {
187 global $wgDBprefix;
188 return substr( $tableName, strlen( $wgDBprefix ) );
189 }
190
191 static private function isNotUnittest( $table ) {
192 return strpos( $table, 'unittest_' ) !== 0;
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
201 // Don't duplicate test tables from the previous fataled run
202 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
203
204 if ( $this->db->getType() == 'sqlite' ) {
205 $tables = array_flip( $tables );
206 // these are subtables of searchindex and don't need to be duped/dropped separately
207 unset( $tables['searchindex_content'] );
208 unset( $tables['searchindex_segdir'] );
209 unset( $tables['searchindex_segments'] );
210 $tables = array_flip( $tables );
211 }
212 return $tables;
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 public static function disableInterwikis( $prefix, &$data ) {
236 return false;
237 }
238 }
239