Ensure users are able to edit the page after changing the content model
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 /**
9 * @var DatabaseBase
10 */
11 protected $db;
12
13 private $functionTest = false;
14
15 protected function setUp() {
16 parent::setUp();
17 $this->db = wfGetDB( DB_MASTER );
18 }
19
20 protected function tearDown() {
21 parent::tearDown();
22 if ( $this->functionTest ) {
23 $this->dropFunctions();
24 $this->functionTest = false;
25 }
26 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
27 }
28 /**
29 * @covers DatabaseBase::dropTable
30 */
31 public function testAddQuotesNull() {
32 $check = "NULL";
33 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
34 $check = "''";
35 }
36 $this->assertEquals( $check, $this->db->addQuotes( null ) );
37 }
38
39 public function testAddQuotesInt() {
40 # returning just "1234" should be ok too, though...
41 # maybe
42 $this->assertEquals(
43 "'1234'",
44 $this->db->addQuotes( 1234 ) );
45 }
46
47 public function testAddQuotesFloat() {
48 # returning just "1234.5678" would be ok too, though
49 $this->assertEquals(
50 "'1234.5678'",
51 $this->db->addQuotes( 1234.5678 ) );
52 }
53
54 public function testAddQuotesString() {
55 $this->assertEquals(
56 "'string'",
57 $this->db->addQuotes( 'string' ) );
58 }
59
60 public function testAddQuotesStringQuote() {
61 $check = "'string''s cause trouble'";
62 if ( $this->db->getType() === 'mysql' ) {
63 $check = "'string\'s cause trouble'";
64 }
65 $this->assertEquals(
66 $check,
67 $this->db->addQuotes( "string's cause trouble" ) );
68 }
69
70 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
71 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
72
73 $oldName = $wgSharedDB;
74 $oldTables = $wgSharedTables;
75 $oldPrefix = $wgSharedPrefix;
76
77 $wgSharedDB = $database;
78 $wgSharedTables = [ $table ];
79 $wgSharedPrefix = $prefix;
80
81 $ret = $this->db->tableName( $table, $format );
82
83 $wgSharedDB = $oldName;
84 $wgSharedTables = $oldTables;
85 $wgSharedPrefix = $oldPrefix;
86
87 return $ret;
88 }
89
90 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
91 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
92 $quote = '';
93 } elseif ( $this->db->getType() === 'mysql' ) {
94 $quote = '`';
95 } elseif ( $this->db->getType() === 'oracle' ) {
96 $quote = '/*Q*/';
97 } else {
98 $quote = '"';
99 }
100
101 if ( $database !== null ) {
102 if ( $this->db->getType() === 'oracle' ) {
103 $database = $quote . $database . '.';
104 } else {
105 $database = $quote . $database . $quote . '.';
106 }
107 }
108
109 if ( $prefix === null ) {
110 $prefix = $this->dbPrefix();
111 }
112
113 if ( $this->db->getType() === 'oracle' ) {
114 return strtoupper( $database . $quote . $prefix . $table );
115 } else {
116 return $database . $quote . $prefix . $table . $quote;
117 }
118 }
119
120 public function testTableNameLocal() {
121 $this->assertEquals(
122 $this->prefixAndQuote( 'tablename' ),
123 $this->db->tableName( 'tablename' )
124 );
125 }
126
127 public function testTableNameRawLocal() {
128 $this->assertEquals(
129 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
130 $this->db->tableName( 'tablename', 'raw' )
131 );
132 }
133
134 public function testTableNameShared() {
135 $this->assertEquals(
136 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
137 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
138 );
139
140 $this->assertEquals(
141 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
142 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
143 );
144 }
145
146 public function testTableNameRawShared() {
147 $this->assertEquals(
148 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
149 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
150 );
151
152 $this->assertEquals(
153 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
154 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
155 );
156 }
157
158 public function testTableNameForeign() {
159 $this->assertEquals(
160 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
161 $this->db->tableName( 'databasename.tablename' )
162 );
163 }
164
165 public function testTableNameRawForeign() {
166 $this->assertEquals(
167 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
168 $this->db->tableName( 'databasename.tablename', 'raw' )
169 );
170 }
171
172 public function testFillPreparedEmpty() {
173 $sql = $this->db->fillPrepared(
174 'SELECT * FROM interwiki', [] );
175 $this->assertEquals(
176 "SELECT * FROM interwiki",
177 $sql );
178 }
179
180 public function testFillPreparedQuestion() {
181 $sql = $this->db->fillPrepared(
182 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
183 [ 4, "Snicker's_paradox" ] );
184
185 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
186 if ( $this->db->getType() === 'mysql' ) {
187 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
188 }
189 $this->assertEquals( $check, $sql );
190 }
191
192 public function testFillPreparedBang() {
193 $sql = $this->db->fillPrepared(
194 'SELECT user_id FROM ! WHERE user_name=?',
195 [ '"user"', "Slash's Dot" ] );
196
197 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
198 if ( $this->db->getType() === 'mysql' ) {
199 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
200 }
201 $this->assertEquals( $check, $sql );
202 }
203
204 public function testFillPreparedRaw() {
205 $sql = $this->db->fillPrepared(
206 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
207 [ '"user"', "Slash's Dot" ] );
208 $this->assertEquals(
209 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
210 $sql );
211 }
212
213 public function testStoredFunctions() {
214 if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
215 $this->markTestSkipped( 'MySQL or Postgres required' );
216 }
217 global $IP;
218 $this->dropFunctions();
219 $this->functionTest = true;
220 $this->assertTrue(
221 $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
222 );
223 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
224 $this->assertEquals( 42, $res->fetchObject()->test );
225 }
226
227 private function dropFunctions() {
228 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
229 . ( $this->db->getType() == 'postgres' ? '()' : '' )
230 );
231 }
232
233 public function testUnknownTableCorruptsResults() {
234 $res = $this->db->select( 'page', '*', [ 'page_id' => 1 ] );
235 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
236 $this->assertInternalType( 'int', $res->numRows() );
237 }
238
239 public function testTransactionIdle() {
240 $db = $this->db;
241
242 $db->setFlag( DBO_TRX );
243 $called = false;
244 $flagSet = null;
245 $db->onTransactionIdle( function() use ( $db, &$flagSet, &$called ) {
246 $called = true;
247 $flagSet = $db->getFlag( DBO_TRX );
248 } );
249 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
250 $this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
251 $this->assertTrue( $called, 'Callback reached' );
252
253 $db->clearFlag( DBO_TRX );
254 $flagSet = null;
255 $db->onTransactionIdle( function() use ( $db, &$flagSet ) {
256 $flagSet = $db->getFlag( DBO_TRX );
257 } );
258 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
259 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
260
261 $db->clearFlag( DBO_TRX );
262 $db->onTransactionIdle( function() use ( $db ) {
263 $db->setFlag( DBO_TRX );
264 } );
265 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
266 }
267
268 public function testTransactionResolution() {
269 $db = $this->db;
270
271 $db->clearFlag( DBO_TRX );
272 $db->begin( __METHOD__ );
273 $called = false;
274 $db->onTransactionResolution( function() use ( $db, &$called ) {
275 $called = true;
276 $db->setFlag( DBO_TRX );
277 } );
278 $db->commit( __METHOD__ );
279 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
280 $this->assertTrue( $called, 'Callback reached' );
281
282 $db->clearFlag( DBO_TRX );
283 $db->begin( __METHOD__ );
284 $called = false;
285 $db->onTransactionResolution( function() use ( $db, &$called ) {
286 $called = true;
287 $db->setFlag( DBO_TRX );
288 } );
289 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
290 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
291 $this->assertTrue( $called, 'Callback reached' );
292 }
293
294 /**
295 * @covers DatabaseBase::setTransactionListener()
296 */
297 public function testTransactionListener() {
298 $db = $this->db;
299
300 $db->setTransactionListener( 'ping', function() use ( $db, &$called ) {
301 $called = true;
302 } );
303
304 $called = false;
305 $db->begin( __METHOD__ );
306 $db->commit( __METHOD__ );
307 $this->assertTrue( $called, 'Callback reached' );
308
309 $called = false;
310 $db->begin( __METHOD__ );
311 $db->commit( __METHOD__ );
312 $this->assertTrue( $called, 'Callback still reached' );
313
314 $called = false;
315 $db->begin( __METHOD__ );
316 $db->rollback( __METHOD__ );
317 $this->assertTrue( $called, 'Callback reached' );
318
319 $db->setTransactionListener( 'ping', null );
320 $called = false;
321 $db->begin( __METHOD__ );
322 $db->commit( __METHOD__ );
323 $this->assertFalse( $called, 'Callback not reached' );
324 }
325
326 /**
327 * @covers DatabaseBase::flushSnapshot()
328 */
329 public function testFlushSnapshot() {
330 $db = $this->db;
331
332 $db->flushSnapshot( __METHOD__ ); // ok
333 $db->flushSnapshot( __METHOD__ ); // ok
334
335 $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
336 $db->query( 'SELECT 1', __METHOD__ );
337 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
338 $db->flushSnapshot( __METHOD__ ); // ok
339 $db->restoreFlags( $db::RESTORE_PRIOR );
340
341 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
342 }
343
344 public function testGetScopedLock() {
345 $db = $this->db;
346
347 $db->setFlag( DBO_TRX );
348 try {
349 $this->badLockingMethodImplicit( $db );
350 } catch ( RunTimeException $e ) {
351 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
352 }
353 $db->clearFlag( DBO_TRX );
354 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
355 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
356
357 try {
358 $this->badLockingMethodExplicit( $db );
359 } catch ( RunTimeException $e ) {
360 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
361 }
362 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
363 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
364 }
365
366 private function badLockingMethodImplicit( IDatabase $db ) {
367 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
368 $db->query( "SELECT 1" ); // trigger DBO_TRX
369 throw new RunTimeException( "Uh oh!" );
370 }
371
372 private function badLockingMethodExplicit( IDatabase $db ) {
373 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
374 $db->begin( __METHOD__ );
375 throw new RunTimeException( "Uh oh!" );
376 }
377
378 /**
379 * @covers DatabaseBase::getFlag(
380 * @covers DatabaseBase::setFlag()
381 * @covers DatabaseBase::restoreFlags()
382 */
383 public function testFlagSetting() {
384 $db = $this->db;
385 $origTrx = $db->getFlag( DBO_TRX );
386 $origSsl = $db->getFlag( DBO_SSL );
387
388 if ( $origTrx ) {
389 $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR );
390 } else {
391 $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
392 }
393 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
394
395 if ( $origSsl ) {
396 $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR );
397 } else {
398 $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
399 }
400 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL ) );
401
402 $db2 = clone $db;
403 $db2->restoreFlags( $db::RESTORE_INITIAL );
404 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX ) );
405 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL ) );
406
407 $db->restoreFlags();
408 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
409 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
410
411 $db->restoreFlags();
412 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
413 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
414 }
415 }