Follow up r79109.
[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;
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
39 # The message cache was already created in Setup.php
40 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
41 array( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry ) );
42
43 /* Classes */
44
45 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
46 public $suite;
47 public $regex = '';
48 public $runDisabled = false;
49
50 protected static $databaseSetupDone = false;
51 protected $db;
52 protected $dbClone;
53 protected $oldTablePrefix;
54 protected $useTemporaryTables = true;
55
56 function __construct( $name = null, array $data = array(), $dataName = '' ) {
57 if ($name !== null) {
58 $this->setName($name);
59 }
60
61 $this->data = $data;
62 $this->dataName = $dataName;
63 }
64
65 function run( PHPUnit_Framework_TestResult $result = NULL ) {
66 if( $this->needsDB() && !is_object( $this->dbClone ) ) {
67 $this->initDB();
68 $this->addCoreDBData();
69 $this->addDBData();
70 }
71 parent::run( $result );
72 }
73
74 function __destruct() {
75 if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
76 $this->destroyDB();
77 }
78 }
79
80 function needsDB() {
81 $rc = new ReflectionClass( $this );
82 return strpos( '@group Database', $rc->getDocComment() ) !== false;
83 }
84
85 function addDBData() {}
86
87 private function addCoreDBData() {
88
89 //Make sysop user
90 $user = User::newFromName( 'UTSysop' );
91
92 if ( $user->idForName() == 0 ) {
93 $user->addToDatabase();
94 $user->setPassword( 'UTSysopPassword' );
95
96 $user->addGroup( 'sysop' );
97 $user->addGroup( 'bureaucrat' );
98 $user->saveSettings();
99 }
100
101
102 //Make 1 page with 1 revision
103 $article = new Article( Title::newFromText( 'UTPage' ) );
104 $article->doEdit( 'UTContent',
105 'UTPageSummary',
106 EDIT_NEW,
107 false,
108 User::newFromName( 'UTSysop' ) );
109 }
110
111 private function initDB() {
112 global $wgDBprefix;
113
114 if ( self::$databaseSetupDone ) {
115 return;
116 }
117
118 $this->db = wfGetDB( DB_MASTER );
119 $dbType = $this->db->getType();
120
121 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
122 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
123 }
124
125 self::$databaseSetupDone = true;
126 $this->oldTablePrefix = $wgDBprefix;
127
128 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
129 # It seems to have been fixed since (r55079?).
130 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
131
132 # CREATE TEMPORARY TABLE breaks if there is more than one server
133 if ( wfGetLB()->getServerCount() != 1 ) {
134 $this->useTemporaryTables = false;
135 }
136
137 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
138
139 $tables = $this->listTables();
140
141 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
142
143 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
144 $this->dbClone->useTemporaryTables( $temporary );
145 $this->dbClone->cloneTableStructure();
146
147 if ( $dbType == 'oracle' )
148 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
149
150 if ( $dbType == 'oracle' ) {
151 # Insert 0 user to prevent FK violations
152
153 # Anonymous user
154 $this->db->insert( 'user', array(
155 'user_id' => 0,
156 'user_name' => 'Anonymous' ) );
157 }
158
159 }
160
161 protected function destroyDB() {
162 if ( !self::$databaseSetupDone ) {
163 return;
164 }
165
166 $this->dbClone->destroy();
167 self::$databaseSetupDone = false;
168
169 if ( $this->useTemporaryTables ) {
170 # Don't need to do anything
171 //return;
172 //Temporary tables seem to be broken ATM, delete anyway
173 }
174
175 if( $this->db->getType() == 'oracle' ) {
176 $tables = $this->db->listTables( 'ut_', __METHOD__ );
177 }
178 else {
179 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
180 }
181
182 foreach ( $tables as $table ) {
183 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
184 $this->db->query( $sql );
185 }
186
187 if ( $this->db->getType() == 'oracle' )
188 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
189
190
191 }
192
193 function __call( $func, $args ) {
194 if ( method_exists( $this->suite, $func ) ) {
195 return call_user_func_array( array( $this->suite, $func ), $args);
196 } else {
197 throw new MWException( "Called non-existant $func method on "
198 . get_class( $this ) );
199 }
200 }
201
202 protected function listTables() {
203 global $wgDBprefix;
204
205 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
206 $tables = array_map( create_function( '$table', 'global $wgDBprefix; return substr( $table, strlen( $wgDBprefix ) );' ), $tables );
207 return $tables;
208
209 }
210 }
211