Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTest.php
blob606a20989cc56967fdba85591740dfbd588bcc5d
1 <?php
3 /**
4 * @group Database
5 * @group Database
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 /**
9 * @var Database
11 protected $db;
13 private $functionTest = false;
15 protected function setUp() {
16 parent::setUp();
17 $this->db = wfGetDB( DB_MASTER );
20 protected function tearDown() {
21 parent::tearDown();
22 if ( $this->functionTest ) {
23 $this->dropFunctions();
24 $this->functionTest = false;
26 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
29 /**
30 * @covers Database::dropTable
32 public function testAddQuotesNull() {
33 $check = "NULL";
34 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
35 $check = "''";
37 $this->assertEquals( $check, $this->db->addQuotes( null ) );
40 public function testAddQuotesInt() {
41 # returning just "1234" should be ok too, though...
42 # maybe
43 $this->assertEquals(
44 "'1234'",
45 $this->db->addQuotes( 1234 ) );
48 public function testAddQuotesFloat() {
49 # returning just "1234.5678" would be ok too, though
50 $this->assertEquals(
51 "'1234.5678'",
52 $this->db->addQuotes( 1234.5678 ) );
55 public function testAddQuotesString() {
56 $this->assertEquals(
57 "'string'",
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'";
66 $this->assertEquals(
67 $check,
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( [
75 $table => [
76 'dbname' => $database,
77 'schema' => null,
78 'prefix' => $prefix
80 ] );
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
91 ) );
93 return $ret;
96 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
97 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
98 $quote = '';
99 } elseif ( $this->db->getType() === 'mysql' ) {
100 $quote = '`';
101 } elseif ( $this->db->getType() === 'oracle' ) {
102 $quote = '/*Q*/';
103 } else {
104 $quote = '"';
107 if ( $database !== null ) {
108 if ( $this->db->getType() === 'oracle' ) {
109 $database = $quote . $database . '.';
110 } else {
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 );
121 } else {
122 return $database . $quote . $prefix . $table . $quote;
126 public function testTableNameLocal() {
127 $this->assertEquals(
128 $this->prefixAndQuote( 'tablename' ),
129 $this->db->tableName( 'tablename' )
133 public function testTableNameRawLocal() {
134 $this->assertEquals(
135 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
136 $this->db->tableName( 'tablename', 'raw' )
140 public function testTableNameShared() {
141 $this->assertEquals(
142 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
143 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
146 $this->assertEquals(
147 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
148 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
152 public function testTableNameRawShared() {
153 $this->assertEquals(
154 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
155 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
158 $this->assertEquals(
159 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
160 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
164 public function testTableNameForeign() {
165 $this->assertEquals(
166 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
167 $this->db->tableName( 'databasename.tablename' )
171 public function testTableNameRawForeign() {
172 $this->assertEquals(
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' );
182 global $IP;
183 $this->dropFunctions();
184 $this->functionTest = true;
185 $this->assertTrue(
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() {
205 $db = $this->db;
207 $db->setFlag( DBO_TRX );
208 $called = false;
209 $flagSet = null;
210 $db->onTransactionIdle(
211 function () use ( $db, &$flagSet, &$called ) {
212 $called = true;
213 $flagSet = $db->getFlag( DBO_TRX );
215 __METHOD__
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 );
222 $flagSet = null;
223 $db->onTransactionIdle(
224 function () use ( $db, &$flagSet ) {
225 $flagSet = $db->getFlag( DBO_TRX );
227 __METHOD__
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 );
237 __METHOD__
239 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
242 public function testTransactionResolution() {
243 $db = $this->db;
245 $db->clearFlag( DBO_TRX );
246 $db->begin( __METHOD__ );
247 $called = false;
248 $db->onTransactionResolution( function () use ( $db, &$called ) {
249 $called = true;
250 $db->setFlag( DBO_TRX );
251 } );
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__ );
258 $called = false;
259 $db->onTransactionResolution( function () use ( $db, &$called ) {
260 $called = true;
261 $db->setFlag( DBO_TRX );
262 } );
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() {
272 $db = $this->db;
274 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
275 $called = true;
276 } );
278 $called = false;
279 $db->begin( __METHOD__ );
280 $db->commit( __METHOD__ );
281 $this->assertTrue( $called, 'Callback reached' );
283 $called = false;
284 $db->begin( __METHOD__ );
285 $db->commit( __METHOD__ );
286 $this->assertTrue( $called, 'Callback still reached' );
288 $called = false;
289 $db->begin( __METHOD__ );
290 $db->rollback( __METHOD__ );
291 $this->assertTrue( $called, 'Callback reached' );
293 $db->setTransactionListener( 'ping', null );
294 $called = false;
295 $db->begin( __METHOD__ );
296 $db->commit( __METHOD__ );
297 $this->assertFalse( $called, 'Callback not reached' );
301 * @covers Database::flushSnapshot()
303 public function testFlushSnapshot() {
304 $db = $this->db;
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() {
319 $db = $this->db;
321 $db->setFlag( DBO_TRX );
322 try {
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__ ) );
331 try {
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() {
358 $db = $this->db;
359 $origTrx = $db->getFlag( DBO_TRX );
360 $origSsl = $db->getFlag( DBO_SSL );
362 $origTrx
363 ? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
364 : $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
365 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
367 $origSsl
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 ) );
376 $origTrx
377 ? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
378 : $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
379 $origSsl
380 ? $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR )
381 : $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
383 $db->restoreFlags();
384 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
385 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
387 $db->restoreFlags();
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() );