Fix issues identified by SpaceBeforeSingleLineComment sniff
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class DatabaseSqliteMock extends DatabaseSqlite {
4 private $lastQuery;
5
6 public static function newInstance( array $p = array() ) {
7 $p['dbFilePath'] = ':memory:';
8 $p['schema'] = false;
9
10 return DatabaseBase::factory( 'SqliteMock', $p );
11 }
12
13 function query( $sql, $fname = '', $tempIgnore = false ) {
14 $this->lastQuery = $sql;
15
16 return true;
17 }
18
19 /**
20 * Override parent visibility to public
21 */
22 public function replaceVars( $s ) {
23 return parent::replaceVars( $s );
24 }
25 }
26
27 /**
28 * @group sqlite
29 * @group Database
30 * @group medium
31 */
32 class DatabaseSqliteTest extends MediaWikiTestCase {
33 /** @var DatabaseSqliteMock */
34 protected $db;
35
36 protected function setUp() {
37 parent::setUp();
38
39 if ( !Sqlite::isPresent() ) {
40 $this->markTestSkipped( 'No SQLite support detected' );
41 }
42 $this->db = DatabaseSqliteMock::newInstance();
43 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
44 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
45 }
46 }
47
48 private function replaceVars( $sql ) {
49 // normalize spacing to hide implementation details
50 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
51 }
52
53 private function assertResultIs( $expected, $res ) {
54 $this->assertNotNull( $res );
55 $i = 0;
56 foreach ( $res as $row ) {
57 foreach ( $expected[$i] as $key => $value ) {
58 $this->assertTrue( isset( $row->$key ) );
59 $this->assertEquals( $value, $row->$key );
60 }
61 $i++;
62 }
63 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
64 }
65
66 public static function provideAddQuotes() {
67 return array(
68 array( // #0: empty
69 '', "''"
70 ),
71 array( // #1: simple
72 'foo bar', "'foo bar'"
73 ),
74 array( // #2: including quote
75 'foo\'bar', "'foo''bar'"
76 ),
77 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
78 array(
79 "x\0y",
80 "x'780079'",
81 ),
82 array( // #4: blob object (must be represented as hex)
83 new Blob( "hello" ),
84 "x'68656c6c6f'",
85 ),
86 );
87 }
88
89 /**
90 * @dataProvider provideAddQuotes()
91 * @covers DatabaseSqlite::addQuotes
92 */
93 public function testAddQuotes( $value, $expected ) {
94 // check quoting
95 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
96 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
97
98 // ok, quoting works as expected, now try a round trip.
99 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
100
101 $this->assertTrue( $re !== false, 'query failed' );
102
103 if ( $row = $re->fetchRow() ) {
104 if ( $value instanceof Blob ) {
105 $value = $value->fetch();
106 }
107
108 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
109 } else {
110 $this->fail( 'query returned no result' );
111 }
112 }
113
114 /**
115 * @covers DatabaseSqlite::replaceVars
116 */
117 public function testReplaceVars() {
118 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
119
120 $this->assertEquals(
121 "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
122 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
123 $this->replaceVars(
124 "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
125 . "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
126 . "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
127 )
128 );
129
130 $this->assertEquals(
131 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
132 $this->replaceVars(
133 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
134 )
135 );
136
137 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
138 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
139 );
140
141 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
142 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
143 'Table name changed'
144 );
145
146 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
147 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
148 );
149 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
150 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
151 );
152
153 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
154 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
155 );
156
157 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
158 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
159 );
160
161 $this->assertEquals( "DROP INDEX foo",
162 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
163 );
164
165 $this->assertEquals( "DROP INDEX foo -- dropping index",
166 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
167 );
168 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
169 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
170 );
171 }
172
173 /**
174 * @covers DatabaseSqlite::tableName
175 */
176 public function testTableName() {
177 // @todo Moar!
178 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
179 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
180 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
181 $db->tablePrefix( 'foo' );
182 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
183 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
184 }
185
186 /**
187 * @covers DatabaseSqlite::duplicateTableStructure
188 */
189 public function testDuplicateTableStructure() {
190 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
191 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
192 $db->query( 'CREATE INDEX index1 ON foo(foo)' );
193 $db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
194
195 $db->duplicateTableStructure( 'foo', 'bar' );
196 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
197 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
198 'Normal table duplication'
199 );
200 $indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
201 $index = $indexList->next();
202 $this->assertEquals( 'bar_index1', $index->name );
203 $this->assertEquals( '0', $index->unique );
204 $index = $indexList->next();
205 $this->assertEquals( 'bar_index2', $index->name );
206 $this->assertEquals( '1', $index->unique );
207
208 $db->duplicateTableStructure( 'foo', 'baz', true );
209 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
210 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
211 'Creation of temporary duplicate'
212 );
213 $indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
214 $index = $indexList->next();
215 $this->assertEquals( 'baz_index1', $index->name );
216 $this->assertEquals( '0', $index->unique );
217 $index = $indexList->next();
218 $this->assertEquals( 'baz_index2', $index->name );
219 $this->assertEquals( '1', $index->unique );
220 $this->assertEquals( 0,
221 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
222 'Create a temporary duplicate only'
223 );
224 }
225
226 /**
227 * @covers DatabaseSqlite::duplicateTableStructure
228 */
229 public function testDuplicateTableStructureVirtual() {
230 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
231 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
232 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
233 }
234 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
235
236 $db->duplicateTableStructure( 'foo', 'bar' );
237 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
238 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
239 'Duplication of virtual tables'
240 );
241
242 $db->duplicateTableStructure( 'foo', 'baz', true );
243 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
244 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
245 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
246 );
247 }
248
249 /**
250 * @covers DatabaseSqlite::deleteJoin
251 */
252 public function testDeleteJoin() {
253 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
254 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
255 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
256 $db->insert( 'a', array(
257 array( 'a_1' => 1 ),
258 array( 'a_1' => 2 ),
259 array( 'a_1' => 3 ),
260 ),
261 __METHOD__
262 );
263 $db->insert( 'b', array(
264 array( 'b_1' => 2, 'b_2' => 'a' ),
265 array( 'b_1' => 3, 'b_2' => 'b' ),
266 ),
267 __METHOD__
268 );
269 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
270 $res = $db->query( "SELECT * FROM a", __METHOD__ );
271 $this->assertResultIs( array(
272 array( 'a_1' => 1 ),
273 array( 'a_1' => 3 ),
274 ),
275 $res
276 );
277 }
278
279 public function testEntireSchema() {
280 global $IP;
281
282 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
283 if ( $result !== true ) {
284 $this->fail( $result );
285 }
286 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
287 }
288
289 /**
290 * Runs upgrades of older databases and compares results with current schema
291 * @todo Currently only checks list of tables
292 */
293 public function testUpgrades() {
294 global $IP, $wgVersion, $wgProfiler;
295
296 // Versions tested
297 $versions = array(
298 // '1.13', disabled for now, was totally screwed up
299 // SQLite wasn't included in 1.14
300 '1.15',
301 '1.16',
302 '1.17',
303 '1.18',
304 );
305
306 // Mismatches for these columns we can safely ignore
307 $ignoredColumns = array(
308 'user_newtalk.user_last_timestamp', // r84185
309 );
310
311 $currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
312 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
313
314 $profileToDb = false;
315 if ( isset( $wgProfiler['output'] ) ) {
316 $out = $wgProfiler['output'];
317 if ( $out === 'db' ) {
318 $profileToDb = true;
319 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
320 $profileToDb = true;
321 }
322 }
323
324 if ( $profileToDb ) {
325 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
326 }
327 $currentTables = $this->getTables( $currentDB );
328 sort( $currentTables );
329
330 foreach ( $versions as $version ) {
331 $versions = "upgrading from $version to $wgVersion";
332 $db = $this->prepareDB( $version );
333 $tables = $this->getTables( $db );
334 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
335 foreach ( $tables as $table ) {
336 $currentCols = $this->getColumns( $currentDB, $table );
337 $cols = $this->getColumns( $db, $table );
338 $this->assertEquals(
339 array_keys( $currentCols ),
340 array_keys( $cols ),
341 "Mismatching columns for table \"$table\" $versions"
342 );
343 foreach ( $currentCols as $name => $column ) {
344 $fullName = "$table.$name";
345 $this->assertEquals(
346 (bool)$column->pk,
347 (bool)$cols[$name]->pk,
348 "PRIMARY KEY status does not match for column $fullName $versions"
349 );
350 if ( !in_array( $fullName, $ignoredColumns ) ) {
351 $this->assertEquals(
352 (bool)$column->notnull,
353 (bool)$cols[$name]->notnull,
354 "NOT NULL status does not match for column $fullName $versions"
355 );
356 $this->assertEquals(
357 $column->dflt_value,
358 $cols[$name]->dflt_value,
359 "Default values does not match for column $fullName $versions"
360 );
361 }
362 }
363 $currentIndexes = $this->getIndexes( $currentDB, $table );
364 $indexes = $this->getIndexes( $db, $table );
365 $this->assertEquals(
366 array_keys( $currentIndexes ),
367 array_keys( $indexes ),
368 "mismatching indexes for table \"$table\" $versions"
369 );
370 }
371 $db->close();
372 }
373 }
374
375 /**
376 * @covers DatabaseSqlite::insertId
377 */
378 public function testInsertIdType() {
379 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
380
381 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
382 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
383
384 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
385 $this->assertTrue( $insertion, "Insertion worked" );
386
387 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
388 $this->assertTrue( $db->close(), "closing database" );
389 }
390
391 private function prepareDB( $version ) {
392 static $maint = null;
393 if ( $maint === null ) {
394 $maint = new FakeMaintenance();
395 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
396 }
397
398 global $IP;
399 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
400 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
401 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
402 $updater->doUpdates( array( 'core' ) );
403
404 return $db;
405 }
406
407 private function getTables( $db ) {
408 $list = array_flip( $db->listTables() );
409 $excluded = array(
410 'external_user', // removed from core in 1.22
411 'math', // moved out of core in 1.18
412 'trackbacks', // removed from core in 1.19
413 'searchindex',
414 'searchindex_content',
415 'searchindex_segments',
416 'searchindex_segdir',
417 // FTS4 ready!!1
418 'searchindex_docsize',
419 'searchindex_stat',
420 );
421 foreach ( $excluded as $t ) {
422 unset( $list[$t] );
423 }
424 $list = array_flip( $list );
425 sort( $list );
426
427 return $list;
428 }
429
430 private function getColumns( $db, $table ) {
431 $cols = array();
432 $res = $db->query( "PRAGMA table_info($table)" );
433 $this->assertNotNull( $res );
434 foreach ( $res as $col ) {
435 $cols[$col->name] = $col;
436 }
437 ksort( $cols );
438
439 return $cols;
440 }
441
442 private function getIndexes( $db, $table ) {
443 $indexes = array();
444 $res = $db->query( "PRAGMA index_list($table)" );
445 $this->assertNotNull( $res );
446 foreach ( $res as $index ) {
447 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
448 $this->assertNotNull( $res2 );
449 $index->columns = array();
450 foreach ( $res2 as $col ) {
451 $index->columns[] = $col;
452 }
453 $indexes[$index->name] = $index;
454 }
455 ksort( $indexes );
456
457 return $indexes;
458 }
459
460 public function testCaseInsensitiveLike() {
461 // TODO: Test this for all databases
462 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
463 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
464 $row = $res->fetchRow();
465 $this->assertFalse( (bool)$row['a'] );
466 }
467
468 /**
469 * @covers DatabaseSqlite::numFields
470 */
471 public function testNumFields() {
472 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
473
474 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
475 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
476 $res = $db->select( 'a', '*' );
477 $this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
478 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
479 $this->assertTrue( $insertion, "Insertion failed" );
480 $res = $db->select( 'a', '*' );
481 $this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
482
483 $this->assertTrue( $db->close(), "closing database" );
484 }
485 }