Merge "(bug 41295) Galician date and time localization"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( ':memory:' );
8 }
9
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
13 }
14
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
17 }
18 }
19
20 /**
21 * @group sqlite
22 * @group Database
23 */
24 class DatabaseSqliteTest extends MediaWikiTestCase {
25 var $db;
26
27 protected function setUp() {
28 parent::setUp();
29
30 if ( !Sqlite::isPresent() ) {
31 $this->markTestSkipped( 'No SQLite support detected' );
32 }
33 $this->db = new MockDatabaseSqlite();
34 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
35 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
36 }
37 }
38
39 private function replaceVars( $sql ) {
40 // normalize spacing to hide implementation details
41 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
42 }
43
44 private function assertResultIs( $expected, $res ) {
45 $this->assertNotNull( $res );
46 $i = 0;
47 foreach( $res as $row ) {
48 foreach( $expected[$i] as $key => $value ) {
49 $this->assertTrue( isset( $row->$key ) );
50 $this->assertEquals( $value, $row->$key );
51 }
52 $i++;
53 }
54 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
55 }
56
57 public static function provideAddQuotes() {
58 return array(
59 array( // #0: empty
60 '', "''"
61 ),
62 array( // #1: simple
63 'foo bar', "'foo bar'"
64 ),
65 array( // #2: including quote
66 'foo\'bar', "'foo''bar'"
67 ),
68 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
69 "x\0y",
70 "x'780079'",
71 ),
72 array( // #4: blob object (must be represented as hex)
73 new Blob( "hello" ),
74 "x'68656c6c6f'",
75 ),
76 );
77 }
78
79 /**
80 * @dataProvider provideAddQuotes()
81 */
82 public function testAddQuotes( $value, $expected ) {
83 // check quoting
84 $db = new DatabaseSqliteStandalone( ':memory:' );
85 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
86
87 // ok, quoting works as expected, now try a round trip.
88 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
89
90 $this->assertTrue( $re !== false, 'query failed' );
91
92 if ( $row = $re->fetchRow() ) {
93 if ( $value instanceof Blob ) {
94 $value = $value->fetch();
95 }
96
97 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
98 } else {
99 $this->fail( 'query returned no result' );
100 }
101 }
102
103 public function testReplaceVars() {
104 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
105
106 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
107 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
108 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
109 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
110 );
111
112 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
113 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
114 );
115
116 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
117 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
118 );
119
120 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
121 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
122 'Table name changed'
123 );
124
125 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
126 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
127 );
128 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
129 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
130 );
131
132 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
133 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
134 );
135
136 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
137 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
138 );
139 }
140
141 public function testTableName() {
142 // @todo Moar!
143 $db = new DatabaseSqliteStandalone( ':memory:' );
144 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
145 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
146 $db->tablePrefix( 'foo' );
147 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
148 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
149 }
150
151 public function testDuplicateTableStructure() {
152 $db = new DatabaseSqliteStandalone( ':memory:' );
153 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
154
155 $db->duplicateTableStructure( 'foo', 'bar' );
156 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
157 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
158 'Normal table duplication'
159 );
160
161 $db->duplicateTableStructure( 'foo', 'baz', true );
162 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
163 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
164 'Creation of temporary duplicate'
165 );
166 $this->assertEquals( 0,
167 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
168 'Create a temporary duplicate only'
169 );
170 }
171
172 public function testDuplicateTableStructureVirtual() {
173 $db = new DatabaseSqliteStandalone( ':memory:' );
174 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
175 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
176 }
177 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
178
179 $db->duplicateTableStructure( 'foo', 'bar' );
180 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
181 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
182 'Duplication of virtual tables'
183 );
184
185 $db->duplicateTableStructure( 'foo', 'baz', true );
186 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
187 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
188 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
189 );
190 }
191
192 public function testDeleteJoin() {
193 $db = new DatabaseSqliteStandalone( ':memory:' );
194 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
195 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
196 $db->insert( 'a', array(
197 array( 'a_1' => 1 ),
198 array( 'a_1' => 2 ),
199 array( 'a_1' => 3 ),
200 ),
201 __METHOD__
202 );
203 $db->insert( 'b', array(
204 array( 'b_1' => 2, 'b_2' => 'a' ),
205 array( 'b_1' => 3, 'b_2' => 'b' ),
206 ),
207 __METHOD__
208 );
209 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
210 $res = $db->query( "SELECT * FROM a", __METHOD__ );
211 $this->assertResultIs( array(
212 array( 'a_1' => 1 ),
213 array( 'a_1' => 3 ),
214 ),
215 $res
216 );
217 }
218
219 public function testEntireSchema() {
220 global $IP;
221
222 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
223 if ( $result !== true ) {
224 $this->fail( $result );
225 }
226 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
227 }
228
229 /**
230 * Runs upgrades of older databases and compares results with current schema
231 * @todo: currently only checks list of tables
232 */
233 public function testUpgrades() {
234 global $IP, $wgVersion;
235
236 // Versions tested
237 $versions = array(
238 //'1.13', disabled for now, was totally screwed up
239 // SQLite wasn't included in 1.14
240 '1.15',
241 '1.16',
242 '1.17',
243 '1.18',
244 );
245
246 // Mismatches for these columns we can safely ignore
247 $ignoredColumns = array(
248 'user_newtalk.user_last_timestamp', // r84185
249 );
250
251 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
252 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
253 $currentTables = $this->getTables( $currentDB );
254 sort( $currentTables );
255
256 foreach ( $versions as $version ) {
257 $versions = "upgrading from $version to $wgVersion";
258 $db = $this->prepareDB( $version );
259 $tables = $this->getTables( $db );
260 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
261 foreach ( $tables as $table ) {
262 $currentCols = $this->getColumns( $currentDB, $table );
263 $cols = $this->getColumns( $db, $table );
264 $this->assertEquals(
265 array_keys( $currentCols ),
266 array_keys( $cols ),
267 "Mismatching columns for table \"$table\" $versions"
268 );
269 foreach ( $currentCols as $name => $column ) {
270 $fullName = "$table.$name";
271 $this->assertEquals(
272 (bool)$column->pk,
273 (bool)$cols[$name]->pk,
274 "PRIMARY KEY status does not match for column $fullName $versions"
275 );
276 if ( !in_array( $fullName, $ignoredColumns ) ) {
277 $this->assertEquals(
278 (bool)$column->notnull,
279 (bool)$cols[$name]->notnull,
280 "NOT NULL status does not match for column $fullName $versions"
281 );
282 $this->assertEquals(
283 $column->dflt_value,
284 $cols[$name]->dflt_value,
285 "Default values does not match for column $fullName $versions"
286 );
287 }
288 }
289 $currentIndexes = $this->getIndexes( $currentDB, $table );
290 $indexes = $this->getIndexes( $db, $table );
291 $this->assertEquals(
292 array_keys( $currentIndexes ),
293 array_keys( $indexes ),
294 "mismatching indexes for table \"$table\" $versions"
295 );
296 }
297 $db->close();
298 }
299 }
300
301 public function testInsertIdType() {
302 $db = new DatabaseSqliteStandalone( ':memory:' );
303 $this->assertInstanceOf( 'ResultWrapper',
304 $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ), "Database creationg" );
305 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ ),
306 "Insertion worked" );
307 $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
308 $this->assertTrue( $db->close(), "closing database" );
309 }
310
311 private function prepareDB( $version ) {
312 static $maint = null;
313 if ( $maint === null ) {
314 $maint = new FakeMaintenance();
315 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
316 }
317
318 global $IP;
319 $db = new DatabaseSqliteStandalone( ':memory:' );
320 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
321 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
322 $updater->doUpdates( array( 'core' ) );
323 return $db;
324 }
325
326 private function getTables( $db ) {
327 $list = array_flip( $db->listTables() );
328 $excluded = array(
329 'math', // moved out of core in 1.18
330 'trackbacks', // removed from core in 1.19
331 'searchindex',
332 'searchindex_content',
333 'searchindex_segments',
334 'searchindex_segdir',
335 // FTS4 ready!!1
336 'searchindex_docsize',
337 'searchindex_stat',
338 );
339 foreach ( $excluded as $t ) {
340 unset( $list[$t] );
341 }
342 $list = array_flip( $list );
343 sort( $list );
344 return $list;
345 }
346
347 private function getColumns( $db, $table ) {
348 $cols = array();
349 $res = $db->query( "PRAGMA table_info($table)" );
350 $this->assertNotNull( $res );
351 foreach ( $res as $col ) {
352 $cols[$col->name] = $col;
353 }
354 ksort( $cols );
355 return $cols;
356 }
357
358 private function getIndexes( $db, $table ) {
359 $indexes = array();
360 $res = $db->query( "PRAGMA index_list($table)" );
361 $this->assertNotNull( $res );
362 foreach ( $res as $index ) {
363 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
364 $this->assertNotNull( $res2 );
365 $index->columns = array();
366 foreach ( $res2 as $col ) {
367 $index->columns[] = $col;
368 }
369 $indexes[$index->name] = $index;
370 }
371 ksort( $indexes );
372 return $indexes;
373 }
374 }