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 Database::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 testStoredFunctions() {
179 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), [ 'mysql', 'postgres' ] ) ) {
180 $this->markTestSkipped( 'MySQL or Postgres required' );
183 $this->dropFunctions();
184 $this->functionTest
= true;
186 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
188 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
189 $this->assertEquals( 42, $res->fetchObject()->test
);
192 private function dropFunctions() {
193 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
194 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
198 public function testUnknownTableCorruptsResults() {
199 $res = $this->db
->select( 'page', '*', [ 'page_id' => 1 ] );
200 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
201 $this->assertInternalType( 'int', $res->numRows() );
204 public function testTransactionIdle() {
207 $db->setFlag( DBO_TRX
);
210 $db->onTransactionIdle(
211 function () use ( $db, &$flagSet, &$called ) {
213 $flagSet = $db->getFlag( DBO_TRX
);
217 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
218 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
219 $this->assertTrue( $called, 'Callback reached' );
221 $db->clearFlag( DBO_TRX
);
223 $db->onTransactionIdle(
224 function () use ( $db, &$flagSet ) {
225 $flagSet = $db->getFlag( DBO_TRX
);
229 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
230 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
232 $db->clearFlag( DBO_TRX
);
233 $db->onTransactionIdle(
234 function () use ( $db ) {
235 $db->setFlag( DBO_TRX
);
239 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
242 public function testTransactionResolution() {
245 $db->clearFlag( DBO_TRX
);
246 $db->begin( __METHOD__
);
248 $db->onTransactionResolution( function () use ( $db, &$called ) {
250 $db->setFlag( DBO_TRX
);
252 $db->commit( __METHOD__
);
253 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
254 $this->assertTrue( $called, 'Callback reached' );
256 $db->clearFlag( DBO_TRX
);
257 $db->begin( __METHOD__
);
259 $db->onTransactionResolution( function () use ( $db, &$called ) {
261 $db->setFlag( DBO_TRX
);
263 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
264 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
265 $this->assertTrue( $called, 'Callback reached' );
269 * @covers Database::setTransactionListener()
271 public function testTransactionListener() {
274 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
279 $db->begin( __METHOD__
);
280 $db->commit( __METHOD__
);
281 $this->assertTrue( $called, 'Callback reached' );
284 $db->begin( __METHOD__
);
285 $db->commit( __METHOD__
);
286 $this->assertTrue( $called, 'Callback still reached' );
289 $db->begin( __METHOD__
);
290 $db->rollback( __METHOD__
);
291 $this->assertTrue( $called, 'Callback reached' );
293 $db->setTransactionListener( 'ping', null );
295 $db->begin( __METHOD__
);
296 $db->commit( __METHOD__
);
297 $this->assertFalse( $called, 'Callback not reached' );
301 * @covers Database::flushSnapshot()
303 public function testFlushSnapshot() {
306 $db->flushSnapshot( __METHOD__
); // ok
307 $db->flushSnapshot( __METHOD__
); // ok
309 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
310 $db->query( 'SELECT 1', __METHOD__
);
311 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
312 $db->flushSnapshot( __METHOD__
); // ok
313 $db->restoreFlags( $db::RESTORE_PRIOR
);
315 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
318 public function testGetScopedLock() {
321 $db->setFlag( DBO_TRX
);
323 $this->badLockingMethodImplicit( $db );
324 } catch ( RunTimeException
$e ) {
325 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
327 $db->clearFlag( DBO_TRX
);
328 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
329 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
332 $this->badLockingMethodExplicit( $db );
333 } catch ( RunTimeException
$e ) {
334 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
336 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
337 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
340 private function badLockingMethodImplicit( IDatabase
$db ) {
341 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
342 $db->query( "SELECT 1" ); // trigger DBO_TRX
343 throw new RunTimeException( "Uh oh!" );
346 private function badLockingMethodExplicit( IDatabase
$db ) {
347 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
348 $db->begin( __METHOD__
);
349 throw new RunTimeException( "Uh oh!" );
353 * @covers Database::getFlag(
354 * @covers Database::setFlag()
355 * @covers Database::restoreFlags()
357 public function testFlagSetting() {
359 $origTrx = $db->getFlag( DBO_TRX
);
360 $origSsl = $db->getFlag( DBO_SSL
);
363 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
364 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
365 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
368 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
369 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
370 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
372 $db->restoreFlags( $db::RESTORE_INITIAL
);
373 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
374 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
377 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
378 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
380 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
381 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
384 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
385 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
388 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
389 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
393 * @covers Database::tablePrefix()
394 * @covers Database::dbSchema()
396 public function testMutators() {
397 $old = $this->db
->tablePrefix();
398 $this->assertType( 'string', $old, 'Prefix is string' );
399 $this->assertEquals( $old, $this->db
->tablePrefix(), "Prefix unchanged" );
400 $this->assertEquals( $old, $this->db
->tablePrefix( 'xxx' ) );
401 $this->assertEquals( 'xxx', $this->db
->tablePrefix(), "Prefix set" );
402 $this->db
->tablePrefix( $old );
403 $this->assertNotEquals( 'xxx', $this->db
->tablePrefix() );
405 $old = $this->db
->dbSchema();
406 $this->assertType( 'string', $old, 'Schema is string' );
407 $this->assertEquals( $old, $this->db
->dbSchema(), "Schema unchanged" );
408 $this->assertEquals( $old, $this->db
->dbSchema( 'xxx' ) );
409 $this->assertEquals( 'xxx', $this->db
->dbSchema(), "Schema set" );
410 $this->db
->dbSchema( $old );
411 $this->assertNotEquals( 'xxx', $this->db
->dbSchema() );