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