Fix the DELETE FROM `unittest_objectcache` WHERE keyname = 'my_wiki-unittest_:user...
[lhc/web/wiklou.git] / tests / phpunit / bootstrap.php
1 <?php
2 /**
3 * Bootstrapping for MediaWiki PHPUnit tests
4 * This file is included by phpunit and is NOT in the global scope.
5 *
6 * @file
7 */
8
9 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
10 echo <<<EOF
11 You are running these tests directly from phpunit. You may not have all globals correctly set.
12 Running phpunit.php instead is recommended.
13 EOF;
14 require_once ( dirname( __FILE__ ) . "/phpunit.php" );
15 }
16
17 // Output a notice when running with older versions of PHPUnit
18 if ( !version_compare( PHPUnit_Runner_Version::id(), "3.4.1", ">" ) ) {
19 echo <<<EOF
20 ********************************************************************************
21
22 These tests run best with version PHPUnit 3.4.2 or better. Earlier versions may
23 show failures because earlier versions of PHPUnit do not properly implement
24 dependencies.
25
26 ********************************************************************************
27
28 EOF;
29 }
30
31 global $wgLocalisationCacheConf, $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
32 global $wgMessageCache, $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgMemc;
33 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
34 $wgMainCacheType = CACHE_NONE;
35 $wgMessageCacheType = CACHE_NONE;
36 $wgParserCacheType = CACHE_NONE;
37 $wgUseDatabaseMessages = false; # Set for future resets
38 $wgMemc = new FakeMemCachedClient;
39
40 # The message cache was already created in Setup.php
41 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
42 array( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry ) );
43
44 /* Classes */
45
46 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
47 public $suite;
48 public $regex = '';
49 public $runDisabled = false;
50
51 protected static $databaseSetupDone = false;
52 protected $db;
53 protected $dbClone;
54 protected $oldTablePrefix;
55 protected $useTemporaryTables = true;
56
57 function __construct( $name = null, array $data = array(), $dataName = '' ) {
58 if ($name !== null) {
59 $this->setName($name);
60 }
61
62 $this->data = $data;
63 $this->dataName = $dataName;
64 }
65
66 function run( PHPUnit_Framework_TestResult $result = NULL ) {
67 if( $this->needsDB() && !is_object( $this->dbClone ) ) {
68 $this->initDB();
69 $this->addCoreDBData();
70 $this->addDBData();
71 }
72 parent::run( $result );
73 }
74
75 function __destruct() {
76 if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
77 $this->destroyDB();
78 }
79 }
80
81 function needsDB() {
82 $rc = new ReflectionClass( $this );
83 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
84 }
85
86 function addDBData() {}
87
88 private function addCoreDBData() {
89
90 //Make sysop user
91 $user = User::newFromName( 'UTSysop' );
92
93 if ( $user->idForName() == 0 ) {
94 $user->addToDatabase();
95 $user->setPassword( 'UTSysopPassword' );
96
97 $user->addGroup( 'sysop' );
98 $user->addGroup( 'bureaucrat' );
99 $user->saveSettings();
100 }
101
102
103 //Make 1 page with 1 revision
104 $article = new Article( Title::newFromText( 'UTPage' ) );
105 $article->doEdit( 'UTContent',
106 'UTPageSummary',
107 EDIT_NEW,
108 false,
109 User::newFromName( 'UTSysop' ) );
110 }
111
112 private function initDB() {
113 global $wgDBprefix;
114
115 if ( self::$databaseSetupDone ) {
116 return;
117 }
118
119 $this->db = wfGetDB( DB_MASTER );
120 $dbType = $this->db->getType();
121
122 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
123 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
124 }
125
126 self::$databaseSetupDone = true;
127 $this->oldTablePrefix = $wgDBprefix;
128
129 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
130 # It seems to have been fixed since (r55079?).
131 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
132
133 # CREATE TEMPORARY TABLE breaks if there is more than one server
134 if ( wfGetLB()->getServerCount() != 1 ) {
135 $this->useTemporaryTables = false;
136 }
137
138 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
139
140 $tables = $this->listTables();
141
142 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
143
144 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
145 $this->dbClone->useTemporaryTables( $temporary );
146 $this->dbClone->cloneTableStructure();
147
148 if ( $dbType == 'oracle' )
149 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
150
151 if ( $dbType == 'oracle' ) {
152 # Insert 0 user to prevent FK violations
153
154 # Anonymous user
155 $this->db->insert( 'user', array(
156 'user_id' => 0,
157 'user_name' => 'Anonymous' ) );
158 }
159
160 }
161
162 protected function destroyDB() {
163 if ( !self::$databaseSetupDone ) {
164 return;
165 }
166
167 $this->dbClone->destroy();
168 self::$databaseSetupDone = false;
169
170 if ( $this->useTemporaryTables ) {
171 # Don't need to do anything
172 //return;
173 //Temporary tables seem to be broken ATM, delete anyway
174 }
175
176 if( $this->db->getType() == 'oracle' ) {
177 $tables = $this->db->listTables( 'ut_', __METHOD__ );
178 }
179 else {
180 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
181 }
182
183 foreach ( $tables as $table ) {
184 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
185 $this->db->query( $sql );
186 }
187
188 if ( $this->db->getType() == 'oracle' )
189 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
190
191
192 }
193
194 function __call( $func, $args ) {
195 if ( method_exists( $this->suite, $func ) ) {
196 return call_user_func_array( array( $this->suite, $func ), $args);
197 } else {
198 throw new MWException( "Called non-existant $func method on "
199 . get_class( $this ) );
200 }
201 }
202
203 protected function listTables() {
204 global $wgDBprefix;
205
206 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
207 $tables = array_map( create_function( '$table', 'global $wgDBprefix; return substr( $table, strlen( $wgDBprefix ) );' ), $tables );
208 return $tables;
209
210 }
211 }
212