wiki.techinc.nl/tests/phpunit/integration/includes/db/DatabaseSqliteTest.php

555 lines
16 KiB
PHP
Raw Normal View History

<?php
use Psr\Log\NullLogger;
use Wikimedia\Rdbms\Blob;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DatabaseSqlite;
use Wikimedia\Rdbms\ResultWrapper;
use Wikimedia\Rdbms\TransactionProfiler;
use Wikimedia\TestingAccessWrapper;
/**
* @group sqlite
* @group Database
* @group medium
*/
Define unit and integration test suites Following discussion in Ibb8175981092d7f41864e641cc3c118af70a5c76, this patch proposes to further reduce the scope of what unit tests may access, by removing the loading of DefaultSettings and GlobalFunctions.php. This also has the implied effect of disabling the storage backend, as well as the global service locator. MediaWikiTestCase is renamed to MediaWikiIntegrationTestCase so it's scope and purpose is more clear. Whether we still need to keep `@group Database` annotation around is debatable, as it's unclear to me what the performance costs are of implying database access for all tests which extend IntegrationTestCase. As far as I can tell, `@group Database` is primarily used in CI to run faster tests before slower ones, and with the new UnitTestCase the annotation seems redundant. To run all testsuites, use `composer phpunit`. Other composer scripts: - `composer phpunit:unit` to run unit tests - `composer phpunit:integration` to run integration tests - `composer phpunit:coverage` to generate code coverage reports from unit tests (requires XDebug). Note that you can pass arguments to composer scripts with `--`, e.g. `composer phpunit:integration --exclude-group Dump`. Other changes: - Rename bootstrap.php to bootstrap.maintenance.php so it's clear it's part of the legacy PHPUnit-as-maintenance-class setup - Create new bootstrap.php which loads the minimal configuration necessary for the tests, and do additional setup in the run() method of the unit/integration test case classes - Move the unit-tests.xml file to phpunit.xml.dist in preparation for this being the default test configuration For a follow-up patch: - Find unit/integration tests for extensions/skins - Migrate other test suites from suite.xml - Support running all tests via vendor/bin/phpunit Bug: T84948 Bug: T89432 Bug: T87781 Change-Id: Ie717b0ecf4fcfd089d46248f14853c80b7ef4a76
2019-06-26 02:33:14 +00:00
class DatabaseSqliteTest extends \MediaWikiIntegrationTestCase {
/** @var DatabaseSqlite */
protected $db;
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
if ( !Sqlite::isPresent() ) {
$this->markTestSkipped( 'No SQLite support detected' );
}
$this->db = $this->getMockBuilder( DatabaseSqlite::class )
->setConstructorArgs( [ [
'dbFilePath' => ':memory:',
'dbname' => 'Foo',
'schema' => false,
'host' => false,
'user' => false,
'password' => false,
'tablePrefix' => '',
'cliMode' => true,
'agent' => 'unit-tests',
'flags' => DBO_DEFAULT,
'variables' => [],
'profiler' => null,
'trxProfiler' => new TransactionProfiler(),
'connLogger' => new NullLogger(),
'queryLogger' => new NullLogger(),
'errorLogger' => null,
'deprecationLogger' => null,
] ] )->setMethods( [ 'query' ] )
->getMock();
$this->db->initConnection();
$this->db->method( 'query' )->willReturn( true );
if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
$this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
}
}
/**
* @param $sql
* @return string|string[]|null
*/
private function replaceVars( $sql ) {
$wrapper = TestingAccessWrapper::newFromObject( $this->db );
// normalize spacing to hide implementation details
return preg_replace( '/\s+/', ' ', $wrapper->replaceVars( $sql ) );
}
private function assertResultIs( $expected, $res ) {
$this->assertNotNull( $res );
$i = 0;
foreach ( $res as $row ) {
foreach ( $expected[$i] as $key => $value ) {
$this->assertTrue( isset( $row->$key ) );
$this->assertEquals( $value, $row->$key );
}
$i++;
}
$this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
}
public static function provideAddQuotes() {
return [
[ // #0: empty
'', "''"
],
[ // #1: simple
'foo bar', "'foo bar'"
],
[ // #2: including quote
'foo\'bar', "'foo''bar'"
],
// #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
[
"x\0y",
"x'780079'",
],
[ // #4: blob object (must be represented as hex)
new Blob( "hello" ),
"x'68656c6c6f'",
],
[ // #5: null
null,
"''",
],
];
}
/**
* @dataProvider provideAddQuotes()
* @covers DatabaseSqlite::addQuotes
*/
public function testAddQuotes( $value, $expected ) {
// check quoting
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
// ok, quoting works as expected, now try a round trip.
$re = $db->query( 'select ' . $db->addQuotes( $value ) );
$this->assertTrue( $re !== false, 'query failed' );
$row = $re->fetchRow();
if ( $row ) {
if ( $value instanceof Blob ) {
$value = $value->fetch();
}
$this->assertEquals( $value, $row[0], 'string mangled by the database' );
} else {
$this->fail( 'query returned no result' );
}
}
/**
* @covers DatabaseSqlite::replaceVars
*/
public function testReplaceVars() {
$this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
$this->assertEquals(
"CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
. "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
$this->replaceVars(
"CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
. "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
. "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
)
);
$this->assertEquals(
"CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
$this->replaceVars(
"CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
)
);
$this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
$this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
);
$this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
$this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
'Table name changed'
);
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
$this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
);
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
$this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
);
$this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
$this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
);
$this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
$this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
);
$this->assertEquals( "DROP INDEX foo",
$this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
);
$this->assertEquals( "DROP INDEX foo -- dropping index",
$this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
);
$this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
$this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
);
}
/**
* @covers DatabaseSqlite::tableName
*/
public function testTableName() {
// @todo Moar!
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$this->assertEquals( 'foo', $db->tableName( 'foo' ) );
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
$db->tablePrefix( 'foo_' );
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
$this->assertEquals( 'foo_bar', $db->tableName( 'bar' ) );
}
/**
* @covers DatabaseSqlite::duplicateTableStructure
*/
public function testDuplicateTableStructure() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->query( 'CREATE TABLE foo(foo, barfoo)' );
$db->query( 'CREATE INDEX index1 ON foo(foo)' );
$db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
$db->duplicateTableStructure( 'foo', 'bar' );
2011-04-12 20:48:19 +00:00
$this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
$db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
'Normal table duplication'
);
$indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
$index = $indexList->next();
$this->assertEquals( 'bar_index1', $index->name );
$this->assertEquals( '0', $index->unique );
$index = $indexList->next();
$this->assertEquals( 'bar_index2', $index->name );
$this->assertEquals( '1', $index->unique );
$db->duplicateTableStructure( 'foo', 'baz', true );
2011-04-12 20:48:19 +00:00
$this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
$db->selectField( 'sqlite_temp_master', 'sql', [ 'name' => 'baz' ] ),
'Creation of temporary duplicate'
);
$indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
$index = $indexList->next();
$this->assertEquals( 'baz_index1', $index->name );
$this->assertEquals( '0', $index->unique );
$index = $indexList->next();
$this->assertEquals( 'baz_index2', $index->name );
$this->assertEquals( '1', $index->unique );
$this->assertEquals( 0,
$db->selectField( 'sqlite_master', 'COUNT(*)', [ 'name' => 'baz' ] ),
'Create a temporary duplicate only'
);
}
/**
* @covers DatabaseSqlite::duplicateTableStructure
*/
public function testDuplicateTableStructureVirtual() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
if ( $db->getFulltextSearchModule() != 'FTS3' ) {
$this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
}
2011-04-12 20:48:19 +00:00
$db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
$db->duplicateTableStructure( 'foo', 'bar' );
2011-04-12 20:48:19 +00:00
$this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
$db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
'Duplication of virtual tables'
);
$db->duplicateTableStructure( 'foo', 'baz', true );
2011-04-12 20:48:19 +00:00
$this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
$db->selectField( 'sqlite_master', 'sql', [ 'name' => 'baz' ] ),
"Can't create temporary virtual tables, should fall back to non-temporary duplication"
);
}
/**
* @covers DatabaseSqlite::deleteJoin
*/
public function testDeleteJoin() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
$db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
$db->insert( 'a', [
[ 'a_1' => 1 ],
[ 'a_1' => 2 ],
[ 'a_1' => 3 ],
],
__METHOD__
);
$db->insert( 'b', [
[ 'b_1' => 2, 'b_2' => 'a' ],
[ 'b_1' => 3, 'b_2' => 'b' ],
],
__METHOD__
);
$db->deleteJoin( 'a', 'b', 'a_1', 'b_1', [ 'b_2' => 'a' ], __METHOD__ );
$res = $db->query( "SELECT * FROM a", __METHOD__ );
$this->assertResultIs( [
[ 'a_1' => 1 ],
[ 'a_1' => 3 ],
],
$res
);
}
/**
* @coversNothing
*/
public function testEntireSchema() {
global $IP;
$result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
if ( $result !== true ) {
$this->fail( $result );
}
$this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
}
/**
* Runs upgrades of older databases and compares results with current schema
* @todo Currently only checks list of tables
* @coversNothing
*/
public function testUpgrades() {
global $IP, $wgVersion, $wgProfiler;
2011-05-09 18:49:56 +00:00
// Versions tested
$versions = [
// '1.13', disabled for now, was totally screwed up
2011-05-09 18:49:56 +00:00
// SQLite wasn't included in 1.14
'1.15',
'1.16',
'1.17',
2011-12-11 16:56:37 +00:00
'1.18',
'1.19',
'1.20',
'1.21',
'1.22',
'1.23',
];
2011-05-09 18:49:56 +00:00
// Mismatches for these columns we can safely ignore
$ignoredColumns = [
2011-05-09 18:49:56 +00:00
'user_newtalk.user_last_timestamp', // r84185
];
$currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$currentDB->sourceFile( "$IP/maintenance/tables.sql" );
$profileToDb = false;
if ( isset( $wgProfiler['output'] ) ) {
$out = $wgProfiler['output'];
if ( $out === 'db' ) {
$profileToDb = true;
} elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
$profileToDb = true;
}
}
if ( $profileToDb ) {
$currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
}
$currentTables = $this->getTables( $currentDB );
sort( $currentTables );
foreach ( $versions as $version ) {
2011-05-09 18:49:56 +00:00
$versions = "upgrading from $version to $wgVersion";
$db = $this->prepareTestDB( $version );
$tables = $this->getTables( $db );
2011-05-09 18:49:56 +00:00
$this->assertEquals( $currentTables, $tables, "Different tables $versions" );
foreach ( $tables as $table ) {
$currentCols = $this->getColumns( $currentDB, $table );
$cols = $this->getColumns( $db, $table );
$this->assertEquals(
array_keys( $currentCols ),
array_keys( $cols ),
"Mismatching columns for table \"$table\" $versions"
2011-05-09 18:49:56 +00:00
);
foreach ( $currentCols as $name => $column ) {
$fullName = "$table.$name";
$this->assertEquals(
(bool)$column->pk,
(bool)$cols[$name]->pk,
"PRIMARY KEY status does not match for column $fullName $versions"
);
if ( !in_array( $fullName, $ignoredColumns ) ) {
$this->assertEquals(
(bool)$column->notnull,
(bool)$cols[$name]->notnull,
"NOT NULL status does not match for column $fullName $versions"
);
$this->assertEquals(
$column->dflt_value,
$cols[$name]->dflt_value,
"Default values does not match for column $fullName $versions"
);
}
}
$currentIndexes = $this->getIndexes( $currentDB, $table );
$indexes = $this->getIndexes( $db, $table );
$this->assertEquals(
array_keys( $currentIndexes ),
array_keys( $indexes ),
"mismatching indexes for table \"$table\" $versions"
);
2011-05-09 18:49:56 +00:00
}
$db->close();
}
}
/**
* @covers DatabaseSqlite::insertId
*/
public function testInsertIdType() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
$this->assertInstanceOf( ResultWrapper::class, $databaseCreation, "Database creation" );
$insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__ );
$this->assertTrue( $insertion, "Insertion worked" );
$this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
$this->assertTrue( $db->close(), "closing database" );
}
/**
* @covers DatabaseSqlite::insert
*/
public function testInsertAffectedRows() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->query( 'CREATE TABLE testInsertAffectedRows ( foo )', __METHOD__ );
$insertion = $db->insert(
'testInsertAffectedRows',
[
[ 'foo' => 10 ],
[ 'foo' => 12 ],
[ 'foo' => 1555 ],
],
__METHOD__
);
$this->assertTrue( $insertion, "Insertion worked" );
$this->assertSame( 3, $db->affectedRows() );
$this->assertTrue( $db->close(), "closing database" );
}
private function prepareTestDB( $version ) {
static $maint = null;
if ( $maint === null ) {
$maint = new FakeMaintenance();
$maint->loadParamsAndArgs( null, [ 'quiet' => 1 ] );
}
global $IP;
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
$updater = DatabaseUpdater::newForDB( $db, false, $maint );
$updater->doUpdates( [ 'core' ] );
return $db;
}
2011-05-09 18:49:56 +00:00
private function getTables( $db ) {
$list = array_flip( $db->listTables() );
$excluded = [
'external_user', // removed from core in 1.22
'math', // moved out of core in 1.18
2011-12-11 16:56:37 +00:00
'trackbacks', // removed from core in 1.19
'searchindex',
'searchindex_content',
'searchindex_segments',
'searchindex_segdir',
// FTS4 ready!!1
'searchindex_docsize',
'searchindex_stat',
];
foreach ( $excluded as $t ) {
unset( $list[$t] );
}
$list = array_flip( $list );
sort( $list );
return $list;
}
2011-05-09 18:49:56 +00:00
private function getColumns( $db, $table ) {
$cols = [];
2011-05-09 18:49:56 +00:00
$res = $db->query( "PRAGMA table_info($table)" );
$this->assertNotNull( $res );
foreach ( $res as $col ) {
$cols[$col->name] = $col;
}
ksort( $cols );
2011-05-09 18:49:56 +00:00
return $cols;
}
private function getIndexes( $db, $table ) {
$indexes = [];
$res = $db->query( "PRAGMA index_list($table)" );
$this->assertNotNull( $res );
foreach ( $res as $index ) {
$res2 = $db->query( "PRAGMA index_info({$index->name})" );
$this->assertNotNull( $res2 );
$index->columns = [];
foreach ( $res2 as $col ) {
$index->columns[] = $col;
}
$indexes[$index->name] = $index;
}
ksort( $indexes );
return $indexes;
}
/**
* @coversNothing
*/
public function testCaseInsensitiveLike() {
// TODO: Test this for all databases
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$res = $db->query( 'SELECT "a" LIKE "A" AS a' );
$row = $res->fetchRow();
$this->assertFalse( (bool)$row['a'] );
}
/**
* @covers DatabaseSqlite::numFields
*/
public function testNumFields() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
$this->assertInstanceOf( ResultWrapper::class, $databaseCreation, "Failed to create table a" );
$res = $db->select( 'a', '*' );
$this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
$insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__ );
$this->assertTrue( $insertion, "Insertion failed" );
$res = $db->select( 'a', '*' );
$this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
$this->assertTrue( $db->close(), "closing database" );
}
/**
* @covers \Wikimedia\Rdbms\DatabaseSqlite::__toString
*/
public function testToString() {
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$toString = (string)$db;
$this->assertContains( 'sqlite object', $toString );
}
/**
* @covers \Wikimedia\Rdbms\DatabaseSqlite::getAttributes()
*/
public function testsAttributes() {
$attributes = Database::attributesFromType( 'sqlite' );
$this->assertTrue( $attributes[Database::ATTR_DB_LEVEL_LOCKING] );
}
2011-04-12 20:48:19 +00:00
}