865335387454a9bd523a4b64464b10a64e14672f
[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 static $databaseSetupDone = false;
9 protected $db;
10 protected $dbClone;
11 protected $oldTablePrefix;
12 protected $useTemporaryTables = true;
13
14 function __construct( $name = null, array $data = array(), $dataName = '' ) {
15 if ($name !== null) {
16 $this->setName($name);
17 }
18
19 $this->data = $data;
20 $this->dataName = $dataName;
21 }
22
23 function run( PHPUnit_Framework_TestResult $result = NULL ) {
24
25 if( $this->needsDB() ) {
26
27 $this->destroyDBCheck();
28
29 $this->initDB();
30 $this->addCoreDBData();
31 $this->addDBData();
32 }
33
34 parent::run( $result );
35 }
36
37 function __destruct() {
38 $this->destroyDBCheck();
39 }
40
41 function destroyDBCheck() {
42 if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
43 $this->destroyDB();
44 }
45 }
46
47 function needsDB() {
48 $rc = new ReflectionClass( $this );
49 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
50 }
51
52 function addDBData() {}
53
54 private function addCoreDBData() {
55
56 //Make sysop user
57 $user = User::newFromName( 'UTSysop' );
58
59 if ( $user->idForName() == 0 ) {
60 $user->addToDatabase();
61 $user->setPassword( 'UTSysopPassword' );
62
63 $user->addGroup( 'sysop' );
64 $user->addGroup( 'bureaucrat' );
65 $user->saveSettings();
66 }
67
68
69 //Make 1 page with 1 revision
70 $article = new Article( Title::newFromText( 'UTPage' ) );
71 $article->doEdit( 'UTContent',
72 'UTPageSummary',
73 EDIT_NEW,
74 false,
75 User::newFromName( 'UTSysop' ) );
76 }
77
78 private function initDB() {
79 global $wgDBprefix;
80
81 if ( self::$databaseSetupDone ) {
82 return;
83 }
84
85 $this->db = wfGetDB( DB_MASTER );
86 $dbType = $this->db->getType();
87
88 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
89 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
90 }
91
92 self::$databaseSetupDone = true;
93 $this->oldTablePrefix = $wgDBprefix;
94
95 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
96 # It seems to have been fixed since (r55079?).
97 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
98
99 # CREATE TEMPORARY TABLE breaks if there is more than one server
100 if ( wfGetLB()->getServerCount() != 1 ) {
101 $this->useTemporaryTables = false;
102 }
103
104 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
105
106 $tables = $this->listTables();
107
108 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
109
110 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
111 $this->dbClone->useTemporaryTables( $temporary );
112 $this->dbClone->cloneTableStructure();
113
114 if ( $dbType == 'oracle' )
115 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
116
117 if ( $dbType == 'oracle' ) {
118 # Insert 0 user to prevent FK violations
119
120 # Anonymous user
121 $this->db->insert( 'user', array(
122 'user_id' => 0,
123 'user_name' => 'Anonymous' ) );
124 }
125
126 }
127
128 protected function destroyDB() {
129 if ( !self::$databaseSetupDone ) {
130 return;
131 }
132
133 $this->dbClone->destroy();
134 self::$databaseSetupDone = false;
135
136 if ( $this->useTemporaryTables ) {
137 # Don't need to do anything
138 //return;
139 //Temporary tables seem to be broken ATM, delete anyway
140 }
141
142 if( $this->db->getType() == 'oracle' ) {
143 $tables = $this->db->listTables( 'ut_', __METHOD__ );
144 }
145 else {
146 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
147 }
148
149 foreach ( $tables as $table ) {
150 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
151 $this->db->query( $sql );
152 }
153
154 if ( $this->db->getType() == 'oracle' )
155 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
156
157
158 }
159
160 function __call( $func, $args ) {
161 static $compatibility = array(
162 'assertInternalType' => 'assertType',
163 'assertNotInternalType' => 'assertNotType',
164 'assertInstanceOf' => 'assertType',
165 );
166
167 if ( method_exists( $this->suite, $func ) ) {
168 return call_user_func_array( array( $this->suite, $func ), $args);
169 } elseif ( isset( $compatibility[$func] ) ) {
170 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
171 } else {
172 throw new MWException( "Called non-existant $func method on "
173 . get_class( $this ) );
174 }
175 }
176
177 static private function unprefixTable( $tableName ) {
178 global $wgDBprefix;
179 return substr( $tableName, strlen( $wgDBprefix ) );
180 }
181
182 protected function listTables() {
183 global $wgDBprefix;
184
185 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
186 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
187 return $tables;
188
189 }
190 }
191