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