f182208d098c6a49b6da36351c788b92c0a608e5
[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 */
23 class DatabaseSqliteTest extends MediaWikiTestCase {
24 var $db;
25
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
29 }
30 $this->db = new MockDatabaseSqlite();
31 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
32 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
33 }
34 }
35
36 private function replaceVars( $sql ) {
37 // normalize spacing to hide implementation details
38 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
39 }
40
41 public function testReplaceVars() {
42 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
43
44 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
45 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
46 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
47 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
48 );
49
50 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
51 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
52 );
53
54 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
55 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
56 );
57
58 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
59 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
60 'Table name changed'
61 );
62
63 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
64 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
65 );
66 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
67 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
68 );
69
70 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
71 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
72 );
73
74 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
75 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
76 );
77 }
78
79 public function testTableName() {
80 // @todo Moar!
81 $db = new DatabaseSqliteStandalone( ':memory:' );
82 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
83 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
84 $db->tablePrefix( 'foo' );
85 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
86 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
87 }
88
89 public function testDuplicateTableStructure() {
90 $db = new DatabaseSqliteStandalone( ':memory:' );
91 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
92
93 $db->duplicateTableStructure( 'foo', 'bar' );
94 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
95 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
96 'Normal table duplication'
97 );
98
99 $db->duplicateTableStructure( 'foo', 'baz', true );
100 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
101 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
102 'Creation of temporary duplicate'
103 );
104 $this->assertEquals( 0,
105 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
106 'Create a temporary duplicate only'
107 );
108 }
109
110 public function testDuplicateTableStructureVirtual() {
111 $db = new DatabaseSqliteStandalone( ':memory:' );
112 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
113 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
114 }
115 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
116
117 $db->duplicateTableStructure( 'foo', 'bar' );
118 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
119 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
120 'Duplication of virtual tables'
121 );
122
123 $db->duplicateTableStructure( 'foo', 'baz', true );
124 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
125 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
126 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
127 );
128 }
129
130 public function testEntireSchema() {
131 global $IP;
132
133 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
134 if ( $result !== true ) {
135 $this->fail( $result );
136 }
137 }
138
139 /**
140 * Runs upgrades of older databases and compares results with current schema
141 * @todo: currently only checks list of tables
142 */
143 public function testUpgrades() {
144 global $IP;
145
146 $versions = array( '1.13', '1.15', '1.16', '1.17' ); // SQLite wasn't included in 1.14
147 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
148 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
149 $currentTables = $this->getTables( $currentDB );
150 sort( $currentTables );
151
152 foreach ( $versions as $version ) {
153 $db = $this->prepareDB( $version );
154 $tables = $this->getTables( $db );
155 $this->assertEquals( $currentTables, $tables );
156 $db->close();
157 }
158 }
159
160 private function prepareDB( $version ) {
161 static $maint = null;
162 if ( $maint === null ) {
163 $maint = new FakeMaintenance();
164 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
165 }
166
167 $db = new DatabaseSqliteStandalone( ':memory:' );
168 $db->sourceFile( dirname( __FILE__ ) . "/sqlite/tables-$version.sql" );
169 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
170 $updater->doUpdates( array( 'core' ) );
171 return $db;
172 }
173
174 protected function getTables( $db ) {
175 $list = array_flip( $db->listTables() );
176 $excluded = array(
177 'math', // moved out of core in 1.18
178 'searchindex',
179 'searchindex_content',
180 'searchindex_segments',
181 'searchindex_segdir',
182 // FTS4 ready!!1
183 'searchindex_docsize',
184 'searchindex_stat',
185 );
186 foreach ( $excluded as $t ) {
187 unset( $list[$t] );
188 }
189 $list = array_flip( $list );
190 sort( $list );
191 return $list;
192 }
193 }