(Bug 41352) Provide tests for edit conflicts.
[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 function testReplaceVars() {
58 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
59
60 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
61 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
62 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
63 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
64 );
65
66 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
67 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
68 );
69
70 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
71 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
72 );
73
74 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
75 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
76 'Table name changed'
77 );
78
79 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
80 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
81 );
82 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
83 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
84 );
85
86 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
87 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
88 );
89
90 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
91 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
92 );
93 }
94
95 public function testTableName() {
96 // @todo Moar!
97 $db = new DatabaseSqliteStandalone( ':memory:' );
98 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
99 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
100 $db->tablePrefix( 'foo' );
101 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
102 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
103 }
104
105 public function testDuplicateTableStructure() {
106 $db = new DatabaseSqliteStandalone( ':memory:' );
107 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
108
109 $db->duplicateTableStructure( 'foo', 'bar' );
110 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
111 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
112 'Normal table duplication'
113 );
114
115 $db->duplicateTableStructure( 'foo', 'baz', true );
116 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
117 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
118 'Creation of temporary duplicate'
119 );
120 $this->assertEquals( 0,
121 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
122 'Create a temporary duplicate only'
123 );
124 }
125
126 public function testDuplicateTableStructureVirtual() {
127 $db = new DatabaseSqliteStandalone( ':memory:' );
128 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
129 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
130 }
131 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
132
133 $db->duplicateTableStructure( 'foo', 'bar' );
134 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
135 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
136 'Duplication of virtual tables'
137 );
138
139 $db->duplicateTableStructure( 'foo', 'baz', true );
140 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
141 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
142 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
143 );
144 }
145
146 public function testDeleteJoin() {
147 $db = new DatabaseSqliteStandalone( ':memory:' );
148 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
149 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
150 $db->insert( 'a', array(
151 array( 'a_1' => 1 ),
152 array( 'a_1' => 2 ),
153 array( 'a_1' => 3 ),
154 ),
155 __METHOD__
156 );
157 $db->insert( 'b', array(
158 array( 'b_1' => 2, 'b_2' => 'a' ),
159 array( 'b_1' => 3, 'b_2' => 'b' ),
160 ),
161 __METHOD__
162 );
163 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
164 $res = $db->query( "SELECT * FROM a", __METHOD__ );
165 $this->assertResultIs( array(
166 array( 'a_1' => 1 ),
167 array( 'a_1' => 3 ),
168 ),
169 $res
170 );
171 }
172
173 public function testEntireSchema() {
174 global $IP;
175
176 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
177 if ( $result !== true ) {
178 $this->fail( $result );
179 }
180 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
181 }
182
183 /**
184 * Runs upgrades of older databases and compares results with current schema
185 * @todo: currently only checks list of tables
186 */
187 public function testUpgrades() {
188 global $IP, $wgVersion;
189
190 // Versions tested
191 $versions = array(
192 //'1.13', disabled for now, was totally screwed up
193 // SQLite wasn't included in 1.14
194 '1.15',
195 '1.16',
196 '1.17',
197 '1.18',
198 );
199
200 // Mismatches for these columns we can safely ignore
201 $ignoredColumns = array(
202 'user_newtalk.user_last_timestamp', // r84185
203 );
204
205 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
206 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
207 $currentTables = $this->getTables( $currentDB );
208 sort( $currentTables );
209
210 foreach ( $versions as $version ) {
211 $versions = "upgrading from $version to $wgVersion";
212 $db = $this->prepareDB( $version );
213 $tables = $this->getTables( $db );
214 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
215 foreach ( $tables as $table ) {
216 $currentCols = $this->getColumns( $currentDB, $table );
217 $cols = $this->getColumns( $db, $table );
218 $this->assertEquals(
219 array_keys( $currentCols ),
220 array_keys( $cols ),
221 "Mismatching columns for table \"$table\" $versions"
222 );
223 foreach ( $currentCols as $name => $column ) {
224 $fullName = "$table.$name";
225 $this->assertEquals(
226 (bool)$column->pk,
227 (bool)$cols[$name]->pk,
228 "PRIMARY KEY status does not match for column $fullName $versions"
229 );
230 if ( !in_array( $fullName, $ignoredColumns ) ) {
231 $this->assertEquals(
232 (bool)$column->notnull,
233 (bool)$cols[$name]->notnull,
234 "NOT NULL status does not match for column $fullName $versions"
235 );
236 $this->assertEquals(
237 $column->dflt_value,
238 $cols[$name]->dflt_value,
239 "Default values does not match for column $fullName $versions"
240 );
241 }
242 }
243 $currentIndexes = $this->getIndexes( $currentDB, $table );
244 $indexes = $this->getIndexes( $db, $table );
245 $this->assertEquals(
246 array_keys( $currentIndexes ),
247 array_keys( $indexes ),
248 "mismatching indexes for table \"$table\" $versions"
249 );
250 }
251 $db->close();
252 }
253 }
254
255 public function testInsertIdType() {
256 $db = new DatabaseSqliteStandalone( ':memory:' );
257 $this->assertInstanceOf( 'ResultWrapper',
258 $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ), "Database creationg" );
259 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ ),
260 "Insertion worked" );
261 $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
262 $this->assertTrue( $db->close(), "closing database" );
263 }
264
265 private function prepareDB( $version ) {
266 static $maint = null;
267 if ( $maint === null ) {
268 $maint = new FakeMaintenance();
269 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
270 }
271
272 global $IP;
273 $db = new DatabaseSqliteStandalone( ':memory:' );
274 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
275 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
276 $updater->doUpdates( array( 'core' ) );
277 return $db;
278 }
279
280 private function getTables( $db ) {
281 $list = array_flip( $db->listTables() );
282 $excluded = array(
283 'math', // moved out of core in 1.18
284 'trackbacks', // removed from core in 1.19
285 'searchindex',
286 'searchindex_content',
287 'searchindex_segments',
288 'searchindex_segdir',
289 // FTS4 ready!!1
290 'searchindex_docsize',
291 'searchindex_stat',
292 );
293 foreach ( $excluded as $t ) {
294 unset( $list[$t] );
295 }
296 $list = array_flip( $list );
297 sort( $list );
298 return $list;
299 }
300
301 private function getColumns( $db, $table ) {
302 $cols = array();
303 $res = $db->query( "PRAGMA table_info($table)" );
304 $this->assertNotNull( $res );
305 foreach ( $res as $col ) {
306 $cols[$col->name] = $col;
307 }
308 ksort( $cols );
309 return $cols;
310 }
311
312 private function getIndexes( $db, $table ) {
313 $indexes = array();
314 $res = $db->query( "PRAGMA index_list($table)" );
315 $this->assertNotNull( $res );
316 foreach ( $res as $index ) {
317 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
318 $this->assertNotNull( $res2 );
319 $index->columns = array();
320 foreach ( $res2 as $col ) {
321 $index->columns[] = $col;
322 }
323 $indexes[$index->name] = $index;
324 }
325 ksort( $indexes );
326 return $indexes;
327 }
328 }