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