MediaWiki  REL1_21
DatabaseSqliteTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
00004         var $lastQuery;
00005 
00006         function __construct() {
00007                 parent::__construct( ':memory:' );
00008         }
00009 
00010         function query( $sql, $fname = '', $tempIgnore = false ) {
00011                 $this->lastQuery = $sql;
00012                 return true;
00013         }
00014 
00018         public function replaceVars( $s ) {
00019                 return parent::replaceVars( $s );
00020         }
00021 }
00022 
00028 class DatabaseSqliteTest extends MediaWikiTestCase {
00029         var $db;
00030 
00031         protected function setUp() {
00032                 parent::setUp();
00033 
00034                 if ( !Sqlite::isPresent() ) {
00035                         $this->markTestSkipped( 'No SQLite support detected' );
00036                 }
00037                 $this->db = new MockDatabaseSqlite();
00038                 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
00039                         $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
00040                 }
00041         }
00042 
00043         private function replaceVars( $sql ) {
00044                 // normalize spacing to hide implementation details
00045                 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
00046         }
00047 
00048         private function assertResultIs( $expected, $res ) {
00049                 $this->assertNotNull( $res );
00050                 $i = 0;
00051                 foreach ( $res as $row ) {
00052                         foreach ( $expected[$i] as $key => $value ) {
00053                                 $this->assertTrue( isset( $row->$key ) );
00054                                 $this->assertEquals( $value, $row->$key );
00055                         }
00056                         $i++;
00057                 }
00058                 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
00059         }
00060 
00061         public static function provideAddQuotes() {
00062                 return array(
00063                         array( // #0: empty
00064                                 '', "''"
00065                         ),
00066                         array( // #1: simple
00067                                 'foo bar', "'foo bar'"
00068                         ),
00069                         array( // #2: including quote
00070                                 'foo\'bar', "'foo''bar'"
00071                         ),
00072                         array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
00073                                 "x\0y",
00074                                 "x'780079'",
00075                         ),
00076                         array( // #4: blob object (must be represented as hex)
00077                                 new Blob( "hello" ),
00078                                 "x'68656c6c6f'",
00079                         ),
00080                 );
00081         }
00082 
00086         public function testAddQuotes( $value, $expected ) {
00087                 // check quoting
00088                 $db = new DatabaseSqliteStandalone( ':memory:' );
00089                 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
00090 
00091                 // ok, quoting works as expected, now try a round trip.
00092                 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
00093 
00094                 $this->assertTrue( $re !== false, 'query failed' );
00095 
00096                 if ( $row = $re->fetchRow() ) {
00097                         if ( $value instanceof Blob ) {
00098                                 $value = $value->fetch();
00099                         }
00100 
00101                         $this->assertEquals( $value, $row[0], 'string mangled by the database' );
00102                 } else {
00103                         $this->fail( 'query returned no result' );
00104                 }
00105         }
00106 
00107         public function testReplaceVars() {
00108                 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
00109 
00110                 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
00111                                 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
00112                         $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
00113                         foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
00114                 );
00115 
00116                 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
00117                         $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
00118                 );
00119 
00120                 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
00121                         $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
00122                 );
00123 
00124                 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
00125                         $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
00126                         'Table name changed'
00127                 );
00128 
00129                 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
00130                         $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
00131                 );
00132                 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
00133                         $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
00134                 );
00135 
00136                 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
00137                         $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
00138                 );
00139 
00140                 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
00141                         $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
00142                 );
00143         }
00144 
00145         public function testTableName() {
00146                 // @todo Moar!
00147                 $db = new DatabaseSqliteStandalone( ':memory:' );
00148                 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
00149                 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
00150                 $db->tablePrefix( 'foo' );
00151                 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
00152                 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
00153         }
00154 
00155         public function testDuplicateTableStructure() {
00156                 $db = new DatabaseSqliteStandalone( ':memory:' );
00157                 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
00158 
00159                 $db->duplicateTableStructure( 'foo', 'bar' );
00160                 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
00161                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
00162                         'Normal table duplication'
00163                 );
00164 
00165                 $db->duplicateTableStructure( 'foo', 'baz', true );
00166                 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
00167                         $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
00168                         'Creation of temporary duplicate'
00169                 );
00170                 $this->assertEquals( 0,
00171                         $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
00172                         'Create a temporary duplicate only'
00173                 );
00174         }
00175 
00176         public function testDuplicateTableStructureVirtual() {
00177                 $db = new DatabaseSqliteStandalone( ':memory:' );
00178                 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
00179                         $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
00180                 }
00181                 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
00182 
00183                 $db->duplicateTableStructure( 'foo', 'bar' );
00184                 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
00185                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
00186                         'Duplication of virtual tables'
00187                 );
00188 
00189                 $db->duplicateTableStructure( 'foo', 'baz', true );
00190                 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
00191                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
00192                         "Can't create temporary virtual tables, should fall back to non-temporary duplication"
00193                 );
00194         }
00195 
00196         public function testDeleteJoin() {
00197                 $db = new DatabaseSqliteStandalone( ':memory:' );
00198                 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
00199                 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
00200                 $db->insert( 'a', array(
00201                                 array( 'a_1' => 1 ),
00202                                 array( 'a_1' => 2 ),
00203                                 array( 'a_1' => 3 ),
00204                         ),
00205                         __METHOD__
00206                 );
00207                 $db->insert( 'b', array(
00208                                 array( 'b_1' => 2, 'b_2' => 'a' ),
00209                                 array( 'b_1' => 3, 'b_2' => 'b' ),
00210                         ),
00211                         __METHOD__
00212                 );
00213                 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
00214                 $res = $db->query( "SELECT * FROM a", __METHOD__ );
00215                 $this->assertResultIs( array(
00216                                 array( 'a_1' => 1 ),
00217                                 array( 'a_1' => 3 ),
00218                         ),
00219                         $res
00220                 );
00221         }
00222 
00223         public function testEntireSchema() {
00224                 global $IP;
00225 
00226                 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
00227                 if ( $result !== true ) {
00228                         $this->fail( $result );
00229                 }
00230                 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
00231         }
00232 
00237         public function testUpgrades() {
00238                 global $IP, $wgVersion, $wgProfileToDatabase;
00239 
00240                 // Versions tested
00241                 $versions = array(
00242                         //'1.13', disabled for now, was totally screwed up
00243                         // SQLite wasn't included in 1.14
00244                         '1.15',
00245                         '1.16',
00246                         '1.17',
00247                         '1.18',
00248                 );
00249 
00250                 // Mismatches for these columns we can safely ignore
00251                 $ignoredColumns = array(
00252                         'user_newtalk.user_last_timestamp', // r84185
00253                 );
00254 
00255                 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
00256                 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
00257                 if ( $wgProfileToDatabase ) {
00258                         $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
00259                 }
00260                 $currentTables = $this->getTables( $currentDB );
00261                 sort( $currentTables );
00262 
00263                 foreach ( $versions as $version ) {
00264                         $versions = "upgrading from $version to $wgVersion";
00265                         $db = $this->prepareDB( $version );
00266                         $tables = $this->getTables( $db );
00267                         $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
00268                         foreach ( $tables as $table ) {
00269                                 $currentCols = $this->getColumns( $currentDB, $table );
00270                                 $cols = $this->getColumns( $db, $table );
00271                                 $this->assertEquals(
00272                                         array_keys( $currentCols ),
00273                                         array_keys( $cols ),
00274                                         "Mismatching columns for table \"$table\" $versions"
00275                                 );
00276                                 foreach ( $currentCols as $name => $column ) {
00277                                         $fullName = "$table.$name";
00278                                         $this->assertEquals(
00279                                                 (bool)$column->pk,
00280                                                 (bool)$cols[$name]->pk,
00281                                                 "PRIMARY KEY status does not match for column $fullName $versions"
00282                                         );
00283                                         if ( !in_array( $fullName, $ignoredColumns ) ) {
00284                                                 $this->assertEquals(
00285                                                         (bool)$column->notnull,
00286                                                         (bool)$cols[$name]->notnull,
00287                                                         "NOT NULL status does not match for column $fullName $versions"
00288                                                 );
00289                                                 $this->assertEquals(
00290                                                         $column->dflt_value,
00291                                                         $cols[$name]->dflt_value,
00292                                                         "Default values does not match for column $fullName $versions"
00293                                                 );
00294                                         }
00295                                 }
00296                                 $currentIndexes = $this->getIndexes( $currentDB, $table );
00297                                 $indexes = $this->getIndexes( $db, $table );
00298                                 $this->assertEquals(
00299                                         array_keys( $currentIndexes ),
00300                                         array_keys( $indexes ),
00301                                         "mismatching indexes for table \"$table\" $versions"
00302                                 );
00303                         }
00304                         $db->close();
00305                 }
00306         }
00307 
00308         public function testInsertIdType() {
00309                 $db = new DatabaseSqliteStandalone( ':memory:' );
00310                 $this->assertInstanceOf( 'ResultWrapper',
00311                         $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ), "Database creationg" );
00312                 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ ),
00313                         "Insertion worked" );
00314                 $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
00315                 $this->assertTrue( $db->close(), "closing database" );
00316         }
00317 
00318         private function prepareDB( $version ) {
00319                 static $maint = null;
00320                 if ( $maint === null ) {
00321                         $maint = new FakeMaintenance();
00322                         $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
00323                 }
00324 
00325                 global $IP;
00326                 $db = new DatabaseSqliteStandalone( ':memory:' );
00327                 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
00328                 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
00329                 $updater->doUpdates( array( 'core' ) );
00330                 return $db;
00331         }
00332 
00333         private function getTables( $db ) {
00334                 $list = array_flip( $db->listTables() );
00335                 $excluded = array(
00336                         'math', // moved out of core in 1.18
00337                         'trackbacks', // removed from core in 1.19
00338                         'searchindex',
00339                         'searchindex_content',
00340                         'searchindex_segments',
00341                         'searchindex_segdir',
00342                         // FTS4 ready!!1
00343                         'searchindex_docsize',
00344                         'searchindex_stat',
00345                 );
00346                 foreach ( $excluded as $t ) {
00347                         unset( $list[$t] );
00348                 }
00349                 $list = array_flip( $list );
00350                 sort( $list );
00351                 return $list;
00352         }
00353 
00354         private function getColumns( $db, $table ) {
00355                 $cols = array();
00356                 $res = $db->query( "PRAGMA table_info($table)" );
00357                 $this->assertNotNull( $res );
00358                 foreach ( $res as $col ) {
00359                         $cols[$col->name] = $col;
00360                 }
00361                 ksort( $cols );
00362                 return $cols;
00363         }
00364 
00365         private function getIndexes( $db, $table ) {
00366                 $indexes = array();
00367                 $res = $db->query( "PRAGMA index_list($table)" );
00368                 $this->assertNotNull( $res );
00369                 foreach ( $res as $index ) {
00370                         $res2 = $db->query( "PRAGMA index_info({$index->name})" );
00371                         $this->assertNotNull( $res2 );
00372                         $index->columns = array();
00373                         foreach ( $res2 as $col ) {
00374                                 $index->columns[] = $col;
00375                         }
00376                         $indexes[$index->name] = $index;
00377                 }
00378                 ksort( $indexes );
00379                 return $indexes;
00380         }
00381 
00382         function testCaseInsensitiveLike() {
00383                 // TODO: Test this for all databases
00384                 $db = new DatabaseSqliteStandalone( ':memory:' );
00385                 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
00386                 $row = $res->fetchRow();
00387                 $this->assertFalse( (bool)$row['a'] );
00388         }
00389 }