Move table prefixes to constants, add doc to addDBData()
[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 );
23
24 function __construct( $name = null, array $data = array(), $dataName = '' ) {
25 if ($name !== null) {
26 $this->setName($name);
27 }
28
29 $this->data = $data;
30 $this->dataName = $dataName;
31
32 $this->backupGlobals = false;
33 $this->backupStaticAttributes = false;
34 }
35
36 function run( PHPUnit_Framework_TestResult $result = NULL ) {
37
38 if( $this->needsDB() ) {
39
40 global $wgDBprefix;
41
42 $this->db = wfGetDB( DB_MASTER );
43
44 $this->checkDbIsSupported();
45
46 $this->oldTablePrefix = $wgDBprefix;
47
48 $this->destroyDB();
49
50 $this->initDB();
51 $this->addCoreDBData();
52 $this->addDBData();
53
54 parent::run( $result );
55
56 $this->destroyDB();
57 }
58 else {
59 parent::run( $result );
60
61 }
62
63 }
64
65 function __destruct() {
66 if( $this->needsDB() ) {
67 $this->destroyDB();
68 }
69 }
70
71 function needsDB() {
72 $rc = new ReflectionClass( $this );
73 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
74 }
75
76 /**
77 * Stub. If a test needs to add additional data to the database, it should
78 * implement this method and do so
79 */
80 function addDBData() {}
81
82 private function addCoreDBData() {
83
84 //Make sysop user
85 $user = User::newFromName( 'UTSysop' );
86
87 if ( $user->idForName() == 0 ) {
88 $user->addToDatabase();
89 $user->setPassword( 'UTSysopPassword' );
90
91 $user->addGroup( 'sysop' );
92 $user->addGroup( 'bureaucrat' );
93 $user->saveSettings();
94 }
95
96
97 //Make 1 page with 1 revision
98 $article = new Article( Title::newFromText( 'UTPage' ) );
99 $article->doEdit( 'UTContent',
100 'UTPageSummary',
101 EDIT_NEW,
102 false,
103 User::newFromName( 'UTSysop' ) );
104 }
105
106 private function initDB() {
107 global $wgDBprefix;
108
109 $dbType = $this->db->getType();
110
111 if ( $wgDBprefix === self::DB_PREFIX || ( $dbType == 'oracle' && $wgDBprefix === self::ORA_DB_PREFIX ) ) {
112 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
113 }
114
115 $tables = $this->listTables();
116
117 $prefix = $dbType != 'oracle' ? self::DB_PREFIX : self::ORA_DB_PREFIX;
118
119 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
120 $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
121 $this->dbClone->cloneTableStructure();
122
123 if ( $dbType == 'oracle' )
124 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
125
126 if ( $dbType == 'oracle' ) {
127 # Insert 0 user to prevent FK violations
128
129 # Anonymous user
130 $this->db->insert( 'user', array(
131 'user_id' => 0,
132 'user_name' => 'Anonymous' ) );
133 }
134
135 }
136
137 protected function destroyDB() {
138
139 if ( $this->useTemporaryTables ) {
140 # Don't need to do anything
141 //return;
142 //Temporary tables seem to be broken ATM, delete anyway
143 }
144
145 if( is_null( $this->db ) ) {
146 return;
147 }
148
149 if( $this->db->getType() == 'oracle' ) {
150 $tables = $this->db->listTables( self::ORA_DB_PREFIX, __METHOD__ );
151 }
152 else {
153 $tables = $this->db->listTables( self::DB_PREFIX, __METHOD__ );
154 }
155
156 foreach ( $tables as $table ) {
157 try {
158 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "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 );
176
177 if ( method_exists( $this->suite, $func ) ) {
178 return call_user_func_array( array( $this->suite, $func ), $args);
179 } elseif ( isset( $compatibility[$func] ) ) {
180 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
181 } else {
182 throw new MWException( "Called non-existant $func method on "
183 . get_class( $this ) );
184 }
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 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
196 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
197 return $tables;
198
199 }
200
201 protected function checkDbIsSupported() {
202 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
203 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
204 }
205 }
206
207 public function getCliArg( $offset ) {
208
209 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
210 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
211 }
212
213 }
214
215 public function setCliArg( $offset, $value ) {
216
217 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
218
219 }
220 }
221