3 use Wikimedia\Rdbms\IDatabase
;
9 class DatabaseTest
extends MediaWikiTestCase
{
15 private $functionTest = false;
17 protected function setUp() {
19 $this->db
= wfGetDB( DB_MASTER
);
22 protected function tearDown() {
24 if ( $this->functionTest
) {
25 $this->dropFunctions();
26 $this->functionTest
= false;
28 $this->db
->restoreFlags( IDatabase
::RESTORE_INITIAL
);
32 * @covers Database::dropTable
34 public function testAddQuotesNull() {
36 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
39 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
42 public function testAddQuotesInt() {
43 # returning just "1234" should be ok too, though...
47 $this->db
->addQuotes( 1234 ) );
50 public function testAddQuotesFloat() {
51 # returning just "1234.5678" would be ok too, though
54 $this->db
->addQuotes( 1234.5678 ) );
57 public function testAddQuotesString() {
60 $this->db
->addQuotes( 'string' ) );
63 public function testAddQuotesStringQuote() {
64 $check = "'string''s cause trouble'";
65 if ( $this->db
->getType() === 'mysql' ) {
66 $check = "'string\'s cause trouble'";
70 $this->db
->addQuotes( "string's cause trouble" ) );
73 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
74 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgSharedSchema;
76 $this->db
->setTableAliases( [
78 'dbname' => $database,
84 $ret = $this->db
->tableName( $table, $format );
86 $this->db
->setTableAliases( array_fill_keys(
87 $wgSharedDB ?
$wgSharedTables : [],
89 'dbname' => $wgSharedDB,
90 'schema' => $wgSharedSchema,
91 'prefix' => $wgSharedPrefix
98 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
99 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
101 } elseif ( $this->db
->getType() === 'mysql' ) {
103 } elseif ( $this->db
->getType() === 'oracle' ) {
109 if ( $database !== null ) {
110 if ( $this->db
->getType() === 'oracle' ) {
111 $database = $quote . $database . '.';
113 $database = $quote . $database . $quote . '.';
117 if ( $prefix === null ) {
118 $prefix = $this->dbPrefix();
121 if ( $this->db
->getType() === 'oracle' ) {
122 return strtoupper( $database . $quote . $prefix . $table );
124 return $database . $quote . $prefix . $table . $quote;
128 public function testTableNameLocal() {
130 $this->prefixAndQuote( 'tablename' ),
131 $this->db
->tableName( 'tablename' )
135 public function testTableNameRawLocal() {
137 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
138 $this->db
->tableName( 'tablename', 'raw' )
142 public function testTableNameShared() {
144 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
145 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
149 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
150 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
154 public function testTableNameRawShared() {
156 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
157 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
161 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
162 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
166 public function testTableNameForeign() {
168 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
169 $this->db
->tableName( 'databasename.tablename' )
173 public function testTableNameRawForeign() {
175 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
176 $this->db
->tableName( 'databasename.tablename', 'raw' )
180 public function testStoredFunctions() {
181 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), [ 'mysql', 'postgres' ] ) ) {
182 $this->markTestSkipped( 'MySQL or Postgres required' );
185 $this->dropFunctions();
186 $this->functionTest
= true;
188 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
190 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
191 $this->assertEquals( 42, $res->fetchObject()->test
);
194 private function dropFunctions() {
195 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
196 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
200 public function testUnknownTableCorruptsResults() {
201 $res = $this->db
->select( 'page', '*', [ 'page_id' => 1 ] );
202 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
203 $this->assertInternalType( 'int', $res->numRows() );
206 public function testTransactionIdle() {
209 $db->setFlag( DBO_TRX
);
212 $db->onTransactionIdle(
213 function () use ( $db, &$flagSet, &$called ) {
215 $flagSet = $db->getFlag( DBO_TRX
);
219 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
220 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
221 $this->assertTrue( $called, 'Callback reached' );
223 $db->clearFlag( DBO_TRX
);
225 $db->onTransactionIdle(
226 function () use ( $db, &$flagSet ) {
227 $flagSet = $db->getFlag( DBO_TRX
);
231 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
232 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
234 $db->clearFlag( DBO_TRX
);
235 $db->onTransactionIdle(
236 function () use ( $db ) {
237 $db->setFlag( DBO_TRX
);
241 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
244 public function testTransactionResolution() {
247 $db->clearFlag( DBO_TRX
);
248 $db->begin( __METHOD__
);
250 $db->onTransactionResolution( function () use ( $db, &$called ) {
252 $db->setFlag( DBO_TRX
);
254 $db->commit( __METHOD__
);
255 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
256 $this->assertTrue( $called, 'Callback reached' );
258 $db->clearFlag( DBO_TRX
);
259 $db->begin( __METHOD__
);
261 $db->onTransactionResolution( function () use ( $db, &$called ) {
263 $db->setFlag( DBO_TRX
);
265 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
266 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
267 $this->assertTrue( $called, 'Callback reached' );
271 * @covers Database::setTransactionListener()
273 public function testTransactionListener() {
276 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
281 $db->begin( __METHOD__
);
282 $db->commit( __METHOD__
);
283 $this->assertTrue( $called, 'Callback reached' );
286 $db->begin( __METHOD__
);
287 $db->commit( __METHOD__
);
288 $this->assertTrue( $called, 'Callback still reached' );
291 $db->begin( __METHOD__
);
292 $db->rollback( __METHOD__
);
293 $this->assertTrue( $called, 'Callback reached' );
295 $db->setTransactionListener( 'ping', null );
297 $db->begin( __METHOD__
);
298 $db->commit( __METHOD__
);
299 $this->assertFalse( $called, 'Callback not reached' );
303 * @covers Database::flushSnapshot()
305 public function testFlushSnapshot() {
308 $db->flushSnapshot( __METHOD__
); // ok
309 $db->flushSnapshot( __METHOD__
); // ok
311 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
312 $db->query( 'SELECT 1', __METHOD__
);
313 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
314 $db->flushSnapshot( __METHOD__
); // ok
315 $db->restoreFlags( $db::RESTORE_PRIOR
);
317 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
320 public function testGetScopedLock() {
323 $db->setFlag( DBO_TRX
);
325 $this->badLockingMethodImplicit( $db );
326 } catch ( RunTimeException
$e ) {
327 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
329 $db->clearFlag( DBO_TRX
);
330 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
331 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
334 $this->badLockingMethodExplicit( $db );
335 } catch ( RunTimeException
$e ) {
336 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
338 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
339 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
342 private function badLockingMethodImplicit( IDatabase
$db ) {
343 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
344 $db->query( "SELECT 1" ); // trigger DBO_TRX
345 throw new RunTimeException( "Uh oh!" );
348 private function badLockingMethodExplicit( IDatabase
$db ) {
349 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
350 $db->begin( __METHOD__
);
351 throw new RunTimeException( "Uh oh!" );
355 * @covers Database::getFlag(
356 * @covers Database::setFlag()
357 * @covers Database::restoreFlags()
359 public function testFlagSetting() {
361 $origTrx = $db->getFlag( DBO_TRX
);
362 $origSsl = $db->getFlag( DBO_SSL
);
365 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
366 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
367 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
370 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
371 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
372 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
374 $db->restoreFlags( $db::RESTORE_INITIAL
);
375 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
376 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
379 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
380 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
382 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
383 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
386 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
387 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
390 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
391 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
395 * @covers Database::tablePrefix()
396 * @covers Database::dbSchema()
398 public function testMutators() {
399 $old = $this->db
->tablePrefix();
400 $this->assertType( 'string', $old, 'Prefix is string' );
401 $this->assertEquals( $old, $this->db
->tablePrefix(), "Prefix unchanged" );
402 $this->assertEquals( $old, $this->db
->tablePrefix( 'xxx' ) );
403 $this->assertEquals( 'xxx', $this->db
->tablePrefix(), "Prefix set" );
404 $this->db
->tablePrefix( $old );
405 $this->assertNotEquals( 'xxx', $this->db
->tablePrefix() );
407 $old = $this->db
->dbSchema();
408 $this->assertType( 'string', $old, 'Schema is string' );
409 $this->assertEquals( $old, $this->db
->dbSchema(), "Schema unchanged" );
410 $this->assertEquals( $old, $this->db
->dbSchema( 'xxx' ) );
411 $this->assertEquals( 'xxx', $this->db
->dbSchema(), "Schema set" );
412 $this->db
->dbSchema( $old );
413 $this->assertNotEquals( 'xxx', $this->db
->dbSchema() );