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