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