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