~ubuntu-branches/ubuntu/trusty/mediawiki/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/phpunit/includes/db/DatabaseSqliteTest.php

  • Committer: Package Import Robot
  • Author(s): Thorsten Glaser
  • Date: 2014-03-28 09:56:29 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20140328095629-1526y9tchdd507id
Tags: 1:1.19.14+dfsg-1
* New upstream security fix release (Closes: #742857):
  - (bug 62497) SECURITY: Add CSRF token on Special:ChangePassword
  - (bug 62467) Set a title for the context during import on the cli
* Use upstream-provided signing key bundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        private function prepareDB( $version ) {
 
254
                static $maint = null;
 
255
                if ( $maint === null ) {
 
256
                        $maint = new FakeMaintenance();
 
257
                        $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
 
258
                }
 
259
 
 
260
                global $IP;
 
261
                $db = new DatabaseSqliteStandalone( ':memory:' );
 
262
                $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
 
263
                $updater = DatabaseUpdater::newForDB( $db, false, $maint );
 
264
                $updater->doUpdates( array( 'core' ) );
 
265
                return $db;
 
266
        }
 
267
 
 
268
        private function getTables( $db ) {
 
269
                $list = array_flip( $db->listTables() );
 
270
                $excluded = array(
 
271
                        'math', // moved out of core in 1.18
 
272
                        'trackbacks', // removed from core in 1.19
 
273
                        'searchindex',
 
274
                        'searchindex_content',
 
275
                        'searchindex_segments',
 
276
                        'searchindex_segdir',
 
277
                        // FTS4 ready!!1
 
278
                        'searchindex_docsize',
 
279
                        'searchindex_stat',
 
280
                );
 
281
                foreach ( $excluded as $t ) {
 
282
                        unset( $list[$t] );
 
283
                }
 
284
                $list = array_flip( $list );
 
285
                sort( $list );
 
286
                return $list;
 
287
        }
 
288
 
 
289
        private function getColumns( $db, $table ) {
 
290
                $cols = array();
 
291
                $res = $db->query( "PRAGMA table_info($table)" );
 
292
                $this->assertNotNull( $res );
 
293
                foreach ( $res as $col ) {
 
294
                        $cols[$col->name] = $col;
 
295
                }
 
296
                ksort( $cols );
 
297
                return $cols;
 
298
        }
 
299
 
 
300
        private function getIndexes( $db, $table ) {
 
301
                $indexes = array();
 
302
                $res = $db->query( "PRAGMA index_list($table)" );
 
303
                $this->assertNotNull( $res );
 
304
                foreach ( $res as $index ) {
 
305
                        $res2 = $db->query( "PRAGMA index_info({$index->name})" );
 
306
                        $this->assertNotNull( $res2 );
 
307
                        $index->columns = array();
 
308
                        foreach ( $res2 as $col ) {
 
309
                                $index->columns[] = $col;
 
310
                        }
 
311
                        $indexes[$index->name] = $index;
 
312
                }
 
313
                ksort( $indexes );
 
314
                return $indexes;
 
315
        }
 
316
}