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;
73 $oldName = $wgSharedDB;
74 $oldTables = $wgSharedTables;
75 $oldPrefix = $wgSharedPrefix;
77 $wgSharedDB = $database;
78 $wgSharedTables = [ $table ];
79 $wgSharedPrefix = $prefix;
81 $ret = $this->db
->tableName( $table, $format );
83 $wgSharedDB = $oldName;
84 $wgSharedTables = $oldTables;
85 $wgSharedPrefix = $oldPrefix;
90 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
91 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
93 } elseif ( $this->db
->getType() === 'mysql' ) {
95 } elseif ( $this->db
->getType() === 'oracle' ) {
101 if ( $database !== null ) {
102 if ( $this->db
->getType() === 'oracle' ) {
103 $database = $quote . $database . '.';
105 $database = $quote . $database . $quote . '.';
109 if ( $prefix === null ) {
110 $prefix = $this->dbPrefix();
113 if ( $this->db
->getType() === 'oracle' ) {
114 return strtoupper( $database . $quote . $prefix . $table );
116 return $database . $quote . $prefix . $table . $quote;
120 public function testTableNameLocal() {
122 $this->prefixAndQuote( 'tablename' ),
123 $this->db
->tableName( 'tablename' )
127 public function testTableNameRawLocal() {
129 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
130 $this->db
->tableName( 'tablename', 'raw' )
134 public function testTableNameShared() {
136 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
137 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
141 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
142 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
146 public function testTableNameRawShared() {
148 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
149 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
153 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
154 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
158 public function testTableNameForeign() {
160 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
161 $this->db
->tableName( 'databasename.tablename' )
165 public function testTableNameRawForeign() {
167 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
168 $this->db
->tableName( 'databasename.tablename', 'raw' )
172 public function testFillPreparedEmpty() {
173 $sql = $this->db
->fillPrepared(
174 'SELECT * FROM interwiki', [] );
176 "SELECT * FROM interwiki",
180 public function testFillPreparedQuestion() {
181 $sql = $this->db
->fillPrepared(
182 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
183 [ 4, "Snicker's_paradox" ] );
185 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
186 if ( $this->db
->getType() === 'mysql' ) {
187 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
189 $this->assertEquals( $check, $sql );
192 public function testFillPreparedBang() {
193 $sql = $this->db
->fillPrepared(
194 'SELECT user_id FROM ! WHERE user_name=?',
195 [ '"user"', "Slash's Dot" ] );
197 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
198 if ( $this->db
->getType() === 'mysql' ) {
199 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
201 $this->assertEquals( $check, $sql );
204 public function testFillPreparedRaw() {
205 $sql = $this->db
->fillPrepared(
206 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
207 [ '"user"', "Slash's Dot" ] );
209 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
213 public function testStoredFunctions() {
214 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), [ 'mysql', 'postgres' ] ) ) {
215 $this->markTestSkipped( 'MySQL or Postgres required' );
218 $this->dropFunctions();
219 $this->functionTest
= true;
221 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
223 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
224 $this->assertEquals( 42, $res->fetchObject()->test
);
227 private function dropFunctions() {
228 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
229 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
233 public function testUnknownTableCorruptsResults() {
234 $res = $this->db
->select( 'page', '*', [ 'page_id' => 1 ] );
235 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
236 $this->assertInternalType( 'int', $res->numRows() );
239 public function testTransactionIdle() {
242 $db->setFlag( DBO_TRX
);
245 $db->onTransactionIdle( function() use ( $db, &$flagSet, &$called ) {
247 $flagSet = $db->getFlag( DBO_TRX
);
249 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
250 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
251 $this->assertTrue( $called, 'Callback reached' );
253 $db->clearFlag( DBO_TRX
);
255 $db->onTransactionIdle( function() use ( $db, &$flagSet ) {
256 $flagSet = $db->getFlag( DBO_TRX
);
258 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
259 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
261 $db->clearFlag( DBO_TRX
);
262 $db->onTransactionIdle( function() use ( $db ) {
263 $db->setFlag( DBO_TRX
);
265 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
268 public function testTransactionResolution() {
271 $db->clearFlag( DBO_TRX
);
272 $db->begin( __METHOD__
);
274 $db->onTransactionResolution( function() use ( $db, &$called ) {
276 $db->setFlag( DBO_TRX
);
278 $db->commit( __METHOD__
);
279 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
280 $this->assertTrue( $called, 'Callback reached' );
282 $db->clearFlag( DBO_TRX
);
283 $db->begin( __METHOD__
);
285 $db->onTransactionResolution( function() use ( $db, &$called ) {
287 $db->setFlag( DBO_TRX
);
289 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
290 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
291 $this->assertTrue( $called, 'Callback reached' );
295 * @covers DatabaseBase::setTransactionListener()
297 public function testTransactionListener() {
300 $db->setTransactionListener( 'ping', function() use ( $db, &$called ) {
305 $db->begin( __METHOD__
);
306 $db->commit( __METHOD__
);
307 $this->assertTrue( $called, 'Callback reached' );
310 $db->begin( __METHOD__
);
311 $db->commit( __METHOD__
);
312 $this->assertTrue( $called, 'Callback still reached' );
315 $db->begin( __METHOD__
);
316 $db->rollback( __METHOD__
);
317 $this->assertTrue( $called, 'Callback reached' );
319 $db->setTransactionListener( 'ping', null );
321 $db->begin( __METHOD__
);
322 $db->commit( __METHOD__
);
323 $this->assertFalse( $called, 'Callback not reached' );
327 * @covers DatabaseBase::clearSnapshot()
329 public function testClearSnapshot() {
332 $db->clearSnapshot( __METHOD__
); // ok
333 $db->clearSnapshot( __METHOD__
); // ok
335 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
336 $db->query( 'SELECT 1', __METHOD__
);
337 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
338 $db->clearSnapshot( __METHOD__
); // ok
339 $db->restoreFlags( $db::RESTORE_PRIOR
);
341 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
344 public function testGetScopedLock() {
347 $db->setFlag( DBO_TRX
);
349 $this->badLockingMethodImplicit( $db );
350 } catch ( RunTimeException
$e ) {
351 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
353 $db->clearFlag( DBO_TRX
);
354 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
355 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
358 $this->badLockingMethodExplicit( $db );
359 } catch ( RunTimeException
$e ) {
360 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
362 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
363 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
366 private function badLockingMethodImplicit( IDatabase
$db ) {
367 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
368 $db->query( "SELECT 1" ); // trigger DBO_TRX
369 throw new RunTimeException( "Uh oh!" );
372 private function badLockingMethodExplicit( IDatabase
$db ) {
373 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
374 $db->begin( __METHOD__
);
375 throw new RunTimeException( "Uh oh!" );
379 * @covers DatabaseBase::getFlag(
380 * @covers DatabaseBase::setFlag()
381 * @covers DatabaseBase::restoreFlags()
383 public function testFlagSetting() {
385 $origTrx = $db->getFlag( DBO_TRX
);
386 $origSsl = $db->getFlag( DBO_SSL
);
389 $db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
391 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
393 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
396 $db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
398 $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
400 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
403 $db2->restoreFlags( $db::RESTORE_INITIAL
);
404 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX
) );
405 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL
) );
408 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
409 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
412 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
413 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );