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