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