changed path after relocation
[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 protected $supportedDBs = array(
14 'mysql',
15 'sqlite'
16 );
17
18 function __construct( $name = null, array $data = array(), $dataName = '' ) {
19 if ($name !== null) {
20 $this->setName($name);
21 }
22
23 $this->data = $data;
24 $this->dataName = $dataName;
25
26 $this->backupGlobals = false;
27 $this->backupStaticAttributes = false;
28 }
29
30 function run( PHPUnit_Framework_TestResult $result = NULL ) {
31
32 if( $this->needsDB() ) {
33
34 global $wgDBprefix;
35
36 $this->db = wfGetDB( DB_MASTER );
37
38 $this->checkDbIsSupported();
39
40 $this->oldTablePrefix = $wgDBprefix;
41
42 $this->destroyDB();
43
44 $this->initDB();
45 $this->addCoreDBData();
46 $this->addDBData();
47
48 parent::run( $result );
49
50 $this->destroyDB();
51 }
52 else {
53 parent::run( $result );
54
55 }
56
57 }
58
59 function __destruct() {
60 if( $this->needsDB() ) {
61 $this->destroyDB();
62 }
63 }
64
65 function needsDB() {
66 $rc = new ReflectionClass( $this );
67 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
68 }
69
70 function addDBData() {}
71
72 private function addCoreDBData() {
73
74 //Make sysop user
75 $user = User::newFromName( 'UTSysop' );
76
77 if ( $user->idForName() == 0 ) {
78 $user->addToDatabase();
79 $user->setPassword( 'UTSysopPassword' );
80
81 $user->addGroup( 'sysop' );
82 $user->addGroup( 'bureaucrat' );
83 $user->saveSettings();
84 }
85
86
87 //Make 1 page with 1 revision
88 $article = new Article( Title::newFromText( 'UTPage' ) );
89 $article->doEdit( 'UTContent',
90 'UTPageSummary',
91 EDIT_NEW,
92 false,
93 User::newFromName( 'UTSysop' ) );
94 }
95
96 private function initDB() {
97 global $wgDBprefix;
98
99 $dbType = $this->db->getType();
100
101 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
102 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
103 }
104
105 $tables = $this->listTables();
106
107 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
108
109 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
110 $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
111 $this->dbClone->cloneTableStructure();
112
113 if ( $dbType == 'oracle' )
114 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
115
116 if ( $dbType == 'oracle' ) {
117 # Insert 0 user to prevent FK violations
118
119 # Anonymous user
120 $this->db->insert( 'user', array(
121 'user_id' => 0,
122 'user_name' => 'Anonymous' ) );
123 }
124
125 }
126
127 protected function destroyDB() {
128 global $wgDBprefix;
129
130 if ( $this->useTemporaryTables ) {
131 # Don't need to do anything
132 //return;
133 //Temporary tables seem to be broken ATM, delete anyway
134 }
135
136 if( is_null( $this->db ) ) {
137 return;
138 }
139
140 if( $this->db->getType() == 'oracle' ) {
141 $tables = $this->db->listTables( 'ut_', __METHOD__ );
142 }
143 else {
144 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
145 }
146
147 foreach ( $tables as $table ) {
148 try {
149 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
150 $this->db->query( $sql, __METHOD__ );
151 } catch( Exception $e ) {
152 }
153 }
154
155 if ( $this->db->getType() == 'oracle' )
156 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
157
158 CloneDatabase::changePrefix( $this->oldTablePrefix );
159 }
160
161 function __call( $func, $args ) {
162 static $compatibility = array(
163 'assertInternalType' => 'assertType',
164 'assertNotInternalType' => 'assertNotType',
165 'assertInstanceOf' => 'assertType',
166 );
167
168 if ( method_exists( $this->suite, $func ) ) {
169 return call_user_func_array( array( $this->suite, $func ), $args);
170 } elseif ( isset( $compatibility[$func] ) ) {
171 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
172 } else {
173 throw new MWException( "Called non-existant $func method on "
174 . get_class( $this ) );
175 }
176 }
177
178 static private function unprefixTable( $tableName ) {
179 global $wgDBprefix;
180 return substr( $tableName, strlen( $wgDBprefix ) );
181 }
182
183 protected function listTables() {
184 global $wgDBprefix;
185
186 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
187 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
188 return $tables;
189
190 }
191
192 protected function checkDbIsSupported() {
193 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
194 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
195 }
196 }
197
198 public function getCliArg( $offset ) {
199
200 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
201 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
202 }
203
204 }
205
206 public function setCliArg( $offset, $value ) {
207
208 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
209
210 }
211 }
212