Moved SQLite test .sql files to the common test data directory
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( ':memory:' );
8 }
9
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
13 }
14
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
17 }
18 }
19
20 /**
21 * @group sqlite
22 */
23 class DatabaseSqliteTest extends MediaWikiTestCase {
24 var $db;
25
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
29 }
30 $this->db = new MockDatabaseSqlite();
31 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
32 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
33 }
34 }
35
36 private function replaceVars( $sql ) {
37 // normalize spacing to hide implementation details
38 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
39 }
40
41 private function assertResultIs( $expected, $res ) {
42 $this->assertNotNull( $res );
43 $i = 0;
44 foreach( $res as $row ) {
45 foreach( $expected[$i] as $key => $value ) {
46 $this->assertTrue( isset( $row->$key ) );
47 $this->assertEquals( $value, $row->$key );
48 }
49 $i++;
50 }
51 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
52 }
53
54 public function testReplaceVars() {
55 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
56
57 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
58 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
59 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
60 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
61 );
62
63 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
64 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
65 );
66
67 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
68 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
69 );
70
71 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
72 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
73 'Table name changed'
74 );
75
76 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
77 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
78 );
79 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
80 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
81 );
82
83 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
84 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
85 );
86
87 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
88 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
89 );
90 }
91
92 public function testTableName() {
93 // @todo Moar!
94 $db = new DatabaseSqliteStandalone( ':memory:' );
95 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
96 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
97 $db->tablePrefix( 'foo' );
98 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
99 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
100 }
101
102 public function testDuplicateTableStructure() {
103 $db = new DatabaseSqliteStandalone( ':memory:' );
104 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
105
106 $db->duplicateTableStructure( 'foo', 'bar' );
107 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
108 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
109 'Normal table duplication'
110 );
111
112 $db->duplicateTableStructure( 'foo', 'baz', true );
113 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
114 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
115 'Creation of temporary duplicate'
116 );
117 $this->assertEquals( 0,
118 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
119 'Create a temporary duplicate only'
120 );
121 }
122
123 public function testDuplicateTableStructureVirtual() {
124 $db = new DatabaseSqliteStandalone( ':memory:' );
125 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
126 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
127 }
128 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
129
130 $db->duplicateTableStructure( 'foo', 'bar' );
131 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
132 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
133 'Duplication of virtual tables'
134 );
135
136 $db->duplicateTableStructure( 'foo', 'baz', true );
137 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
138 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
139 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
140 );
141 }
142
143 public function testDeleteJoin() {
144 $db = new DatabaseSqliteStandalone( ':memory:' );
145 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
146 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
147 $db->insert( 'a', array(
148 array( 'a_1' => 1 ),
149 array( 'a_1' => 2 ),
150 array( 'a_1' => 3 ),
151 ),
152 __METHOD__
153 );
154 $db->insert( 'b', array(
155 array( 'b_1' => 2, 'b_2' => 'a' ),
156 array( 'b_1' => 3, 'b_2' => 'b' ),
157 ),
158 __METHOD__
159 );
160 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
161 $res = $db->query( "SELECT * FROM a", __METHOD__ );
162 $this->assertResultIs( array(
163 array( 'a_1' => 1 ),
164 array( 'a_1' => 3 ),
165 ),
166 $res
167 );
168 }
169
170 public function testEntireSchema() {
171 global $IP;
172
173 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
174 if ( $result !== true ) {
175 $this->fail( $result );
176 }
177 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
178 }
179
180 /**
181 * Runs upgrades of older databases and compares results with current schema
182 * @todo: currently only checks list of tables
183 */
184 public function testUpgrades() {
185 global $IP, $wgVersion;
186
187 // Versions tested
188 $versions = array(
189 //'1.13', disabled for now, was totally screwed up
190 // SQLite wasn't included in 1.14
191 '1.15',
192 '1.16',
193 '1.17',
194 '1.18',
195 );
196
197 // Mismatches for these columns we can safely ignore
198 $ignoredColumns = array(
199 'user_newtalk.user_last_timestamp', // r84185
200 );
201
202 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
203 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
204 $currentTables = $this->getTables( $currentDB );
205 sort( $currentTables );
206
207 foreach ( $versions as $version ) {
208 $versions = "upgrading from $version to $wgVersion";
209 $db = $this->prepareDB( $version );
210 $tables = $this->getTables( $db );
211 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
212 foreach ( $tables as $table ) {
213 $currentCols = $this->getColumns( $currentDB, $table );
214 $cols = $this->getColumns( $db, $table );
215 $this->assertEquals(
216 array_keys( $currentCols ),
217 array_keys( $cols ),
218 "Mismatching columns for table \"$table\" $versions"
219 );
220 foreach ( $currentCols as $name => $column ) {
221 $fullName = "$table.$name";
222 $this->assertEquals(
223 (bool)$column->pk,
224 (bool)$cols[$name]->pk,
225 "PRIMARY KEY status does not match for column $fullName $versions"
226 );
227 if ( !in_array( $fullName, $ignoredColumns ) ) {
228 $this->assertEquals(
229 (bool)$column->notnull,
230 (bool)$cols[$name]->notnull,
231 "NOT NULL status does not match for column $fullName $versions"
232 );
233 $this->assertEquals(
234 $column->dflt_value,
235 $cols[$name]->dflt_value,
236 "Default values does not match for column $fullName $versions"
237 );
238 }
239 }
240 $currentIndexes = $this->getIndexes( $currentDB, $table );
241 $indexes = $this->getIndexes( $db, $table );
242 $this->assertEquals(
243 array_keys( $currentIndexes ),
244 array_keys( $indexes ),
245 "mismatching indexes for table \"$table\" $versions"
246 );
247 }
248 $db->close();
249 }
250 }
251
252 private function prepareDB( $version ) {
253 static $maint = null;
254 if ( $maint === null ) {
255 $maint = new FakeMaintenance();
256 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
257 }
258
259 global $IP;
260 $db = new DatabaseSqliteStandalone( ':memory:' );
261 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
262 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
263 $updater->doUpdates( array( 'core' ) );
264 return $db;
265 }
266
267 private function getTables( $db ) {
268 $list = array_flip( $db->listTables() );
269 $excluded = array(
270 'math', // moved out of core in 1.18
271 'trackbacks', // removed from core in 1.19
272 'searchindex',
273 'searchindex_content',
274 'searchindex_segments',
275 'searchindex_segdir',
276 // FTS4 ready!!1
277 'searchindex_docsize',
278 'searchindex_stat',
279 );
280 foreach ( $excluded as $t ) {
281 unset( $list[$t] );
282 }
283 $list = array_flip( $list );
284 sort( $list );
285 return $list;
286 }
287
288 private function getColumns( $db, $table ) {
289 $cols = array();
290 $res = $db->query( "PRAGMA table_info($table)" );
291 $this->assertNotNull( $res );
292 foreach ( $res as $col ) {
293 $cols[$col->name] = $col;
294 }
295 ksort( $cols );
296 return $cols;
297 }
298
299 private function getIndexes( $db, $table ) {
300 $indexes = array();
301 $res = $db->query( "PRAGMA index_list($table)" );
302 $this->assertNotNull( $res );
303 foreach ( $res as $index ) {
304 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
305 $this->assertNotNull( $res2 );
306 $index->columns = array();
307 foreach ( $res2 as $col ) {
308 $index->columns[] = $col;
309 }
310 $indexes[$index->name] = $index;
311 }
312 ksort( $indexes );
313 return $indexes;
314 }
315 }