Make wfUrlEncode(null) reset the static. Two skipped tests work now.
[mediawiki.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
blob914ab27cc0cb6b5d415852a7aaccec2e5af2f9ea
1 <?php
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
6 function __construct( ) {
7 parent::__construct( ':memory:' );
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
20 /**
21 * @group sqlite
23 class DatabaseSqliteTest extends MediaWikiTestCase {
24 var $db;
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
30 $this->db = new MockDatabaseSqlite();
31 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
32 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
36 private function replaceVars( $sql ) {
37 // normalize spacing to hide implementation details
38 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
41 private function assertResultIs( $expected, $res ) {
42 $this->assertNotNull( $res );
43 $i = 0;
44 foreach( $res as $row ) {
45 foreach( $expected[$i] as $key => $value ) {
46 $this->assertTrue( isset( $row->$key ) );
47 $this->assertEquals( $value, $row->$key );
49 $i++;
51 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
54 public function testReplaceVars() {
55 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
57 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
58 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
59 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
60 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
63 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
64 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
67 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
68 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
71 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
72 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
73 'Table name changed'
76 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
77 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
79 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
80 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
83 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
84 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
87 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
88 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
92 public function testTableName() {
93 // @todo Moar!
94 $db = new DatabaseSqliteStandalone( ':memory:' );
95 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
96 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
97 $db->tablePrefix( 'foo' );
98 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
99 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
102 public function testDuplicateTableStructure() {
103 $db = new DatabaseSqliteStandalone( ':memory:' );
104 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
106 $db->duplicateTableStructure( 'foo', 'bar' );
107 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
108 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
109 'Normal table duplication'
112 $db->duplicateTableStructure( 'foo', 'baz', true );
113 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
114 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
115 'Creation of temporary duplicate'
117 $this->assertEquals( 0,
118 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
119 'Create a temporary duplicate only'
123 public function testDuplicateTableStructureVirtual() {
124 $db = new DatabaseSqliteStandalone( ':memory:' );
125 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
126 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
128 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
130 $db->duplicateTableStructure( 'foo', 'bar' );
131 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
132 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
133 'Duplication of virtual tables'
136 $db->duplicateTableStructure( 'foo', 'baz', true );
137 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
138 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
139 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
143 public function testDeleteJoin() {
144 $db = new DatabaseSqliteStandalone( ':memory:' );
145 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
146 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
147 $db->insert( 'a', array(
148 array( 'a_1' => 1 ),
149 array( 'a_1' => 2 ),
150 array( 'a_1' => 3 ),
152 __METHOD__
154 $db->insert( 'b', array(
155 array( 'b_1' => 2, 'b_2' => 'a' ),
156 array( 'b_1' => 3, 'b_2' => 'b' ),
158 __METHOD__
160 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
161 $res = $db->query( "SELECT * FROM a", __METHOD__ );
162 $this->assertResultIs( array(
163 array( 'a_1' => 1 ),
164 array( 'a_1' => 3 ),
166 $res
170 public function testEntireSchema() {
171 global $IP;
173 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
174 if ( $result !== true ) {
175 $this->fail( $result );
177 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
181 * Runs upgrades of older databases and compares results with current schema
182 * @todo: currently only checks list of tables
184 public function testUpgrades() {
185 global $IP, $wgVersion;
187 // Versions tested
188 $versions = array(
189 //'1.13', disabled for now, was totally screwed up
190 // SQLite wasn't included in 1.14
191 '1.15',
192 '1.16',
193 '1.17',
196 // Mismatches for these columns we can safely ignore
197 $ignoredColumns = array(
198 'user_newtalk.user_last_timestamp', // r84185
201 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
202 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
203 $currentTables = $this->getTables( $currentDB );
204 sort( $currentTables );
206 foreach ( $versions as $version ) {
207 $versions = "upgrading from $version to $wgVersion";
208 $db = $this->prepareDB( $version );
209 $tables = $this->getTables( $db );
210 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
211 foreach ( $tables as $table ) {
212 $currentCols = $this->getColumns( $currentDB, $table );
213 $cols = $this->getColumns( $db, $table );
214 $this->assertEquals(
215 array_keys( $currentCols ),
216 array_keys( $cols ),
217 "Mismatching columns for table \"$table\" $versions"
219 foreach ( $currentCols as $name => $column ) {
220 $fullName = "$table.$name";
221 $this->assertEquals(
222 (bool)$column->pk,
223 (bool)$cols[$name]->pk,
224 "PRIMARY KEY status does not match for column $fullName $versions"
226 if ( !in_array( $fullName, $ignoredColumns ) ) {
227 $this->assertEquals(
228 (bool)$column->notnull,
229 (bool)$cols[$name]->notnull,
230 "NOT NULL status does not match for column $fullName $versions"
232 $this->assertEquals(
233 $column->dflt_value,
234 $cols[$name]->dflt_value,
235 "Default values does not match for column $fullName $versions"
239 $currentIndexes = $this->getIndexes( $currentDB, $table );
240 $indexes = $this->getIndexes( $db, $table );
241 $this->assertEquals(
242 array_keys( $currentIndexes ),
243 array_keys( $indexes ),
244 "mismatching indexes for table \"$table\" $versions"
247 $db->close();
251 private function prepareDB( $version ) {
252 static $maint = null;
253 if ( $maint === null ) {
254 $maint = new FakeMaintenance();
255 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
258 $db = new DatabaseSqliteStandalone( ':memory:' );
259 $db->sourceFile( dirname( __FILE__ ) . "/sqlite/tables-$version.sql" );
260 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
261 $updater->doUpdates( array( 'core' ) );
262 return $db;
265 private function getTables( $db ) {
266 $list = array_flip( $db->listTables() );
267 $excluded = array(
268 'math', // moved out of core in 1.18
269 'searchindex',
270 'searchindex_content',
271 'searchindex_segments',
272 'searchindex_segdir',
273 // FTS4 ready!!1
274 'searchindex_docsize',
275 'searchindex_stat',
277 foreach ( $excluded as $t ) {
278 unset( $list[$t] );
280 $list = array_flip( $list );
281 sort( $list );
282 return $list;
285 private function getColumns( $db, $table ) {
286 $cols = array();
287 $res = $db->query( "PRAGMA table_info($table)" );
288 $this->assertNotNull( $res );
289 foreach ( $res as $col ) {
290 $cols[$col->name] = $col;
292 ksort( $cols );
293 return $cols;
296 private function getIndexes( $db, $table ) {
297 $indexes = array();
298 $res = $db->query( "PRAGMA index_list($table)" );
299 $this->assertNotNull( $res );
300 foreach ( $res as $index ) {
301 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
302 $this->assertNotNull( $res2 );
303 $index->columns = array();
304 foreach ( $res2 as $col ) {
305 $index->columns[] = $col;
307 $indexes[$index->name] = $index;
309 ksort( $indexes );
310 return $indexes;