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
);
29 * @covers DatabaseBase::dropTable
31 public function testAddQuotesNull() {
33 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
36 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
39 public function testAddQuotesInt() {
40 # returning just "1234" should be ok too, though...
44 $this->db
->addQuotes( 1234 ) );
47 public function testAddQuotesFloat() {
48 # returning just "1234.5678" would be ok too, though
51 $this->db
->addQuotes( 1234.5678 ) );
54 public function testAddQuotesString() {
57 $this->db
->addQuotes( 'string' ) );
60 public function testAddQuotesStringQuote() {
61 $check = "'string''s cause trouble'";
62 if ( $this->db
->getType() === 'mysql' ) {
63 $check = "'string\'s cause trouble'";
67 $this->db
->addQuotes( "string's cause trouble" ) );
70 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
71 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgSharedSchema;
73 $this->db
->setTableAliases( [
75 'dbname' => $database,
81 $ret = $this->db
->tableName( $table, $format );
83 $this->db
->setTableAliases( array_fill_keys(
84 $wgSharedDB ?
$wgSharedTables : [],
86 'dbname' => $wgSharedDB,
87 'schema' => $wgSharedSchema,
88 'prefix' => $wgSharedPrefix
95 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
96 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
98 } elseif ( $this->db
->getType() === 'mysql' ) {
100 } elseif ( $this->db
->getType() === 'oracle' ) {
106 if ( $database !== null ) {
107 if ( $this->db
->getType() === 'oracle' ) {
108 $database = $quote . $database . '.';
110 $database = $quote . $database . $quote . '.';
114 if ( $prefix === null ) {
115 $prefix = $this->dbPrefix();
118 if ( $this->db
->getType() === 'oracle' ) {
119 return strtoupper( $database . $quote . $prefix . $table );
121 return $database . $quote . $prefix . $table . $quote;
125 public function testTableNameLocal() {
127 $this->prefixAndQuote( 'tablename' ),
128 $this->db
->tableName( 'tablename' )
132 public function testTableNameRawLocal() {
134 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
135 $this->db
->tableName( 'tablename', 'raw' )
139 public function testTableNameShared() {
141 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
142 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
146 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
147 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
151 public function testTableNameRawShared() {
153 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
154 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
158 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
159 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
163 public function testTableNameForeign() {
165 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
166 $this->db
->tableName( 'databasename.tablename' )
170 public function testTableNameRawForeign() {
172 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
173 $this->db
->tableName( 'databasename.tablename', 'raw' )
177 public function testFillPreparedEmpty() {
178 $sql = $this->db
->fillPrepared(
179 'SELECT * FROM interwiki', [] );
181 "SELECT * FROM interwiki",
185 public function testFillPreparedQuestion() {
186 $sql = $this->db
->fillPrepared(
187 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
188 [ 4, "Snicker's_paradox" ] );
190 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
191 if ( $this->db
->getType() === 'mysql' ) {
192 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
194 $this->assertEquals( $check, $sql );
197 public function testFillPreparedBang() {
198 $sql = $this->db
->fillPrepared(
199 'SELECT user_id FROM ! WHERE user_name=?',
200 [ '"user"', "Slash's Dot" ] );
202 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
203 if ( $this->db
->getType() === 'mysql' ) {
204 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
206 $this->assertEquals( $check, $sql );
209 public function testFillPreparedRaw() {
210 $sql = $this->db
->fillPrepared(
211 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
212 [ '"user"', "Slash's Dot" ] );
214 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
218 public function testStoredFunctions() {
219 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), [ 'mysql', 'postgres' ] ) ) {
220 $this->markTestSkipped( 'MySQL or Postgres required' );
223 $this->dropFunctions();
224 $this->functionTest
= true;
226 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
228 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
229 $this->assertEquals( 42, $res->fetchObject()->test
);
232 private function dropFunctions() {
233 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
234 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
238 public function testUnknownTableCorruptsResults() {
239 $res = $this->db
->select( 'page', '*', [ 'page_id' => 1 ] );
240 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
241 $this->assertInternalType( 'int', $res->numRows() );
244 public function testTransactionIdle() {
247 $db->setFlag( DBO_TRX
);
250 $db->onTransactionIdle( function() use ( $db, &$flagSet, &$called ) {
252 $flagSet = $db->getFlag( DBO_TRX
);
254 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
255 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
256 $this->assertTrue( $called, 'Callback reached' );
258 $db->clearFlag( DBO_TRX
);
260 $db->onTransactionIdle( function() use ( $db, &$flagSet ) {
261 $flagSet = $db->getFlag( DBO_TRX
);
263 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
264 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
266 $db->clearFlag( DBO_TRX
);
267 $db->onTransactionIdle( function() use ( $db ) {
268 $db->setFlag( DBO_TRX
);
270 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
273 public function testTransactionResolution() {
276 $db->clearFlag( DBO_TRX
);
277 $db->begin( __METHOD__
);
279 $db->onTransactionResolution( function() use ( $db, &$called ) {
281 $db->setFlag( DBO_TRX
);
283 $db->commit( __METHOD__
);
284 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
285 $this->assertTrue( $called, 'Callback reached' );
287 $db->clearFlag( DBO_TRX
);
288 $db->begin( __METHOD__
);
290 $db->onTransactionResolution( function() use ( $db, &$called ) {
292 $db->setFlag( DBO_TRX
);
294 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
295 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
296 $this->assertTrue( $called, 'Callback reached' );
300 * @covers DatabaseBase::setTransactionListener()
302 public function testTransactionListener() {
305 $db->setTransactionListener( 'ping', function() use ( $db, &$called ) {
310 $db->begin( __METHOD__
);
311 $db->commit( __METHOD__
);
312 $this->assertTrue( $called, 'Callback reached' );
315 $db->begin( __METHOD__
);
316 $db->commit( __METHOD__
);
317 $this->assertTrue( $called, 'Callback still reached' );
320 $db->begin( __METHOD__
);
321 $db->rollback( __METHOD__
);
322 $this->assertTrue( $called, 'Callback reached' );
324 $db->setTransactionListener( 'ping', null );
326 $db->begin( __METHOD__
);
327 $db->commit( __METHOD__
);
328 $this->assertFalse( $called, 'Callback not reached' );
332 * @covers DatabaseBase::flushSnapshot()
334 public function testFlushSnapshot() {
337 $db->flushSnapshot( __METHOD__
); // ok
338 $db->flushSnapshot( __METHOD__
); // ok
340 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
341 $db->query( 'SELECT 1', __METHOD__
);
342 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
343 $db->flushSnapshot( __METHOD__
); // ok
344 $db->restoreFlags( $db::RESTORE_PRIOR
);
346 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
349 public function testGetScopedLock() {
352 $db->setFlag( DBO_TRX
);
354 $this->badLockingMethodImplicit( $db );
355 } catch ( RunTimeException
$e ) {
356 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
358 $db->clearFlag( DBO_TRX
);
359 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
360 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
363 $this->badLockingMethodExplicit( $db );
364 } catch ( RunTimeException
$e ) {
365 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
367 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
368 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
371 private function badLockingMethodImplicit( IDatabase
$db ) {
372 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
373 $db->query( "SELECT 1" ); // trigger DBO_TRX
374 throw new RunTimeException( "Uh oh!" );
377 private function badLockingMethodExplicit( IDatabase
$db ) {
378 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
379 $db->begin( __METHOD__
);
380 throw new RunTimeException( "Uh oh!" );
384 * @covers DatabaseBase::getFlag(
385 * @covers DatabaseBase::setFlag()
386 * @covers DatabaseBase::restoreFlags()
388 public function testFlagSetting() {
390 $origTrx = $db->getFlag( DBO_TRX
);
391 $origSsl = $db->getFlag( DBO_SSL
);
394 $db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
396 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
398 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
401 $db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
403 $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
405 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
408 $db2->restoreFlags( $db::RESTORE_INITIAL
);
409 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX
) );
410 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL
) );
413 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
414 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
417 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
418 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );