7 class DatabaseTest
extends MediaWikiTestCase
{
13 private $functionTest = false;
15 protected function setUp() {
17 $this->db
= wfGetDB( DB_MASTER
);
20 protected function tearDown() {
22 if ( $this->functionTest
) {
23 $this->dropFunctions();
24 $this->functionTest
= false;
26 $this->db
->restoreFlags( IDatabase
::RESTORE_INITIAL
);
30 * @covers DatabaseBase::dropTable
32 public function testAddQuotesNull() {
34 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
37 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
40 public function testAddQuotesInt() {
41 # returning just "1234" should be ok too, though...
45 $this->db
->addQuotes( 1234 ) );
48 public function testAddQuotesFloat() {
49 # returning just "1234.5678" would be ok too, though
52 $this->db
->addQuotes( 1234.5678 ) );
55 public function testAddQuotesString() {
58 $this->db
->addQuotes( 'string' ) );
61 public function testAddQuotesStringQuote() {
62 $check = "'string''s cause trouble'";
63 if ( $this->db
->getType() === 'mysql' ) {
64 $check = "'string\'s cause trouble'";
68 $this->db
->addQuotes( "string's cause trouble" ) );
71 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
72 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgSharedSchema;
74 $this->db
->setTableAliases( [
76 'dbname' => $database,
82 $ret = $this->db
->tableName( $table, $format );
84 $this->db
->setTableAliases( array_fill_keys(
85 $wgSharedDB ?
$wgSharedTables : [],
87 'dbname' => $wgSharedDB,
88 'schema' => $wgSharedSchema,
89 'prefix' => $wgSharedPrefix
96 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
97 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
99 } elseif ( $this->db
->getType() === 'mysql' ) {
101 } elseif ( $this->db
->getType() === 'oracle' ) {
107 if ( $database !== null ) {
108 if ( $this->db
->getType() === 'oracle' ) {
109 $database = $quote . $database . '.';
111 $database = $quote . $database . $quote . '.';
115 if ( $prefix === null ) {
116 $prefix = $this->dbPrefix();
119 if ( $this->db
->getType() === 'oracle' ) {
120 return strtoupper( $database . $quote . $prefix . $table );
122 return $database . $quote . $prefix . $table . $quote;
126 public function testTableNameLocal() {
128 $this->prefixAndQuote( 'tablename' ),
129 $this->db
->tableName( 'tablename' )
133 public function testTableNameRawLocal() {
135 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
136 $this->db
->tableName( 'tablename', 'raw' )
140 public function testTableNameShared() {
142 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
143 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
147 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
148 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
152 public function testTableNameRawShared() {
154 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
155 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
159 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
160 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
164 public function testTableNameForeign() {
166 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
167 $this->db
->tableName( 'databasename.tablename' )
171 public function testTableNameRawForeign() {
173 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
174 $this->db
->tableName( 'databasename.tablename', 'raw' )
178 public function testFillPreparedEmpty() {
179 $sql = $this->db
->fillPrepared(
180 'SELECT * FROM interwiki', [] );
182 "SELECT * FROM interwiki",
186 public function testFillPreparedQuestion() {
187 $sql = $this->db
->fillPrepared(
188 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
189 [ 4, "Snicker's_paradox" ] );
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'";
195 $this->assertEquals( $check, $sql );
198 public function testFillPreparedBang() {
199 $sql = $this->db
->fillPrepared(
200 'SELECT user_id FROM ! WHERE user_name=?',
201 [ '"user"', "Slash's Dot" ] );
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'";
207 $this->assertEquals( $check, $sql );
210 public function testFillPreparedRaw() {
211 $sql = $this->db
->fillPrepared(
212 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
213 [ '"user"', "Slash's Dot" ] );
215 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
219 public function testStoredFunctions() {
220 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), [ 'mysql', 'postgres' ] ) ) {
221 $this->markTestSkipped( 'MySQL or Postgres required' );
224 $this->dropFunctions();
225 $this->functionTest
= true;
227 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
229 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
230 $this->assertEquals( 42, $res->fetchObject()->test
);
233 private function dropFunctions() {
234 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
235 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
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() );
245 public function testTransactionIdle() {
248 $db->setFlag( DBO_TRX
);
251 $db->onTransactionIdle(
252 function () use ( $db, &$flagSet, &$called ) {
254 $flagSet = $db->getFlag( DBO_TRX
);
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' );
262 $db->clearFlag( DBO_TRX
);
264 $db->onTransactionIdle(
265 function () use ( $db, &$flagSet ) {
266 $flagSet = $db->getFlag( DBO_TRX
);
270 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
271 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
273 $db->clearFlag( DBO_TRX
);
274 $db->onTransactionIdle(
275 function () use ( $db ) {
276 $db->setFlag( DBO_TRX
);
280 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
283 public function testTransactionResolution() {
286 $db->clearFlag( DBO_TRX
);
287 $db->begin( __METHOD__
);
289 $db->onTransactionResolution( function () use ( $db, &$called ) {
291 $db->setFlag( DBO_TRX
);
293 $db->commit( __METHOD__
);
294 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
295 $this->assertTrue( $called, 'Callback reached' );
297 $db->clearFlag( DBO_TRX
);
298 $db->begin( __METHOD__
);
300 $db->onTransactionResolution( function () use ( $db, &$called ) {
302 $db->setFlag( DBO_TRX
);
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' );
310 * @covers DatabaseBase::setTransactionListener()
312 public function testTransactionListener() {
315 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
320 $db->begin( __METHOD__
);
321 $db->commit( __METHOD__
);
322 $this->assertTrue( $called, 'Callback reached' );
325 $db->begin( __METHOD__
);
326 $db->commit( __METHOD__
);
327 $this->assertTrue( $called, 'Callback still reached' );
330 $db->begin( __METHOD__
);
331 $db->rollback( __METHOD__
);
332 $this->assertTrue( $called, 'Callback reached' );
334 $db->setTransactionListener( 'ping', null );
336 $db->begin( __METHOD__
);
337 $db->commit( __METHOD__
);
338 $this->assertFalse( $called, 'Callback not reached' );
342 * @covers DatabaseBase::flushSnapshot()
344 public function testFlushSnapshot() {
347 $db->flushSnapshot( __METHOD__
); // ok
348 $db->flushSnapshot( __METHOD__
); // ok
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
);
356 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
359 public function testGetScopedLock() {
362 $db->setFlag( DBO_TRX
);
364 $this->badLockingMethodImplicit( $db );
365 } catch ( RunTimeException
$e ) {
366 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
368 $db->clearFlag( DBO_TRX
);
369 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
370 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
373 $this->badLockingMethodExplicit( $db );
374 } catch ( RunTimeException
$e ) {
375 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
377 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
378 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
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!" );
387 private function badLockingMethodExplicit( IDatabase
$db ) {
388 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
389 $db->begin( __METHOD__
);
390 throw new RunTimeException( "Uh oh!" );
394 * @covers DatabaseBase::getFlag(
395 * @covers DatabaseBase::setFlag()
396 * @covers DatabaseBase::restoreFlags()
398 public function testFlagSetting() {
400 $origTrx = $db->getFlag( DBO_TRX
);
401 $origSsl = $db->getFlag( DBO_SSL
);
404 $db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
406 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
408 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
411 $db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
413 $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
415 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
418 $db2->restoreFlags( $db::RESTORE_INITIAL
);
419 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX
) );
420 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL
) );
423 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
424 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
427 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
428 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
432 * @covers DatabaseBase::tablePrefix()
433 * @covers DatabaseBase::dbSchema()
435 public function testMutators() {
436 $old = $this->db
->tablePrefix();
437 $this->assertType( 'string', $old, 'Prefix is string' );
438 $this->assertEquals( $old, $this->db
->tablePrefix(), "Prefix unchanged" );
439 $this->assertEquals( $old, $this->db
->tablePrefix( 'xxx' ) );
440 $this->assertEquals( 'xxx', $this->db
->tablePrefix(), "Prefix set" );
441 $this->db
->tablePrefix( $old );
442 $this->assertNotEquals( 'xxx', $this->db
->tablePrefix() );
444 $old = $this->db
->dbSchema();
445 $this->assertType( 'string', $old, 'Schema is string' );
446 $this->assertEquals( $old, $this->db
->dbSchema(), "Schema unchanged" );
447 $this->assertEquals( $old, $this->db
->dbSchema( 'xxx' ) );
448 $this->assertEquals( 'xxx', $this->db
->dbSchema(), "Schema set" );
449 $this->db
->dbSchema( $old );
450 $this->assertNotEquals( 'xxx', $this->db
->dbSchema() );