Set default to 1 recent contributor instead of -1
[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 'site_stats.ss_active_users', // T56888
313 ];
314
315 $currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
316 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
317
318 $profileToDb = false;
319 if ( isset( $wgProfiler['output'] ) ) {
320 $out = $wgProfiler['output'];
321 if ( $out === 'db' ) {
322 $profileToDb = true;
323 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
324 $profileToDb = true;
325 }
326 }
327
328 if ( $profileToDb ) {
329 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
330 }
331 $currentTables = $this->getTables( $currentDB );
332 sort( $currentTables );
333
334 foreach ( $versions as $version ) {
335 $versions = "upgrading from $version to $wgVersion";
336 $db = $this->prepareTestDB( $version );
337 $tables = $this->getTables( $db );
338 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
339 foreach ( $tables as $table ) {
340 $currentCols = $this->getColumns( $currentDB, $table );
341 $cols = $this->getColumns( $db, $table );
342 $this->assertEquals(
343 array_keys( $currentCols ),
344 array_keys( $cols ),
345 "Mismatching columns for table \"$table\" $versions"
346 );
347 foreach ( $currentCols as $name => $column ) {
348 $fullName = "$table.$name";
349 $this->assertEquals(
350 (bool)$column->pk,
351 (bool)$cols[$name]->pk,
352 "PRIMARY KEY status does not match for column $fullName $versions"
353 );
354 if ( !in_array( $fullName, $ignoredColumns ) ) {
355 $this->assertEquals(
356 (bool)$column->notnull,
357 (bool)$cols[$name]->notnull,
358 "NOT NULL status does not match for column $fullName $versions"
359 );
360 $this->assertEquals(
361 $column->dflt_value,
362 $cols[$name]->dflt_value,
363 "Default values does not match for column $fullName $versions"
364 );
365 }
366 }
367 $currentIndexes = $this->getIndexes( $currentDB, $table );
368 $indexes = $this->getIndexes( $db, $table );
369 $this->assertEquals(
370 array_keys( $currentIndexes ),
371 array_keys( $indexes ),
372 "mismatching indexes for table \"$table\" $versions"
373 );
374 }
375 $db->close();
376 }
377 }
378
379 /**
380 * @covers DatabaseSqlite::insertId
381 */
382 public function testInsertIdType() {
383 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
384
385 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
386 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
387
388 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__ );
389 $this->assertTrue( $insertion, "Insertion worked" );
390
391 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
392 $this->assertTrue( $db->close(), "closing database" );
393 }
394
395 private function prepareTestDB( $version ) {
396 static $maint = null;
397 if ( $maint === null ) {
398 $maint = new FakeMaintenance();
399 $maint->loadParamsAndArgs( null, [ 'quiet' => 1 ] );
400 }
401
402 global $IP;
403 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
404 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
405 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
406 $updater->doUpdates( [ 'core' ] );
407
408 return $db;
409 }
410
411 private function getTables( $db ) {
412 $list = array_flip( $db->listTables() );
413 $excluded = [
414 'external_user', // removed from core in 1.22
415 'math', // moved out of core in 1.18
416 'trackbacks', // removed from core in 1.19
417 'searchindex',
418 'searchindex_content',
419 'searchindex_segments',
420 'searchindex_segdir',
421 // FTS4 ready!!1
422 'searchindex_docsize',
423 'searchindex_stat',
424 ];
425 foreach ( $excluded as $t ) {
426 unset( $list[$t] );
427 }
428 $list = array_flip( $list );
429 sort( $list );
430
431 return $list;
432 }
433
434 private function getColumns( $db, $table ) {
435 $cols = [];
436 $res = $db->query( "PRAGMA table_info($table)" );
437 $this->assertNotNull( $res );
438 foreach ( $res as $col ) {
439 $cols[$col->name] = $col;
440 }
441 ksort( $cols );
442
443 return $cols;
444 }
445
446 private function getIndexes( $db, $table ) {
447 $indexes = [];
448 $res = $db->query( "PRAGMA index_list($table)" );
449 $this->assertNotNull( $res );
450 foreach ( $res as $index ) {
451 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
452 $this->assertNotNull( $res2 );
453 $index->columns = [];
454 foreach ( $res2 as $col ) {
455 $index->columns[] = $col;
456 }
457 $indexes[$index->name] = $index;
458 }
459 ksort( $indexes );
460
461 return $indexes;
462 }
463
464 public function testCaseInsensitiveLike() {
465 // TODO: Test this for all databases
466 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
467 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
468 $row = $res->fetchRow();
469 $this->assertFalse( (bool)$row['a'] );
470 }
471
472 /**
473 * @covers DatabaseSqlite::numFields
474 */
475 public function testNumFields() {
476 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
477
478 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
479 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
480 $res = $db->select( 'a', '*' );
481 $this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
482 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__ );
483 $this->assertTrue( $insertion, "Insertion failed" );
484 $res = $db->select( 'a', '*' );
485 $this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
486
487 $this->assertTrue( $db->close(), "closing database" );
488 }
489
490 public function testToString() {
491 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
492
493 $toString = (string)$db;
494
495 $this->assertContains( 'SQLite ', $toString );
496 }
497 }