Avoid 'message' in log context in AuthManager
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 /**
9 * @var DatabaseBase
10 */
11 protected $db;
12
13 private $functionTest = false;
14
15 protected function setUp() {
16 parent::setUp();
17 $this->db = wfGetDB( DB_MASTER );
18 }
19
20 protected function tearDown() {
21 parent::tearDown();
22 if ( $this->functionTest ) {
23 $this->dropFunctions();
24 $this->functionTest = false;
25 }
26 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
27 }
28
29 /**
30 * @covers DatabaseBase::dropTable
31 */
32 public function testAddQuotesNull() {
33 $check = "NULL";
34 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
35 $check = "''";
36 }
37 $this->assertEquals( $check, $this->db->addQuotes( null ) );
38 }
39
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 ) );
46 }
47
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 ) );
53 }
54
55 public function testAddQuotesString() {
56 $this->assertEquals(
57 "'string'",
58 $this->db->addQuotes( 'string' ) );
59 }
60
61 public function testAddQuotesStringQuote() {
62 $check = "'string''s cause trouble'";
63 if ( $this->db->getType() === 'mysql' ) {
64 $check = "'string\'s cause trouble'";
65 }
66 $this->assertEquals(
67 $check,
68 $this->db->addQuotes( "string's cause trouble" ) );
69 }
70
71 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
72 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgSharedSchema;
73
74 $this->db->setTableAliases( [
75 $table => [
76 'dbname' => $database,
77 'schema' => null,
78 'prefix' => $prefix
79 ]
80 ] );
81
82 $ret = $this->db->tableName( $table, $format );
83
84 $this->db->setTableAliases( array_fill_keys(
85 $wgSharedDB ? $wgSharedTables : [],
86 [
87 'dbname' => $wgSharedDB,
88 'schema' => $wgSharedSchema,
89 'prefix' => $wgSharedPrefix
90 ]
91 ) );
92
93 return $ret;
94 }
95
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 = '"';
105 }
106
107 if ( $database !== null ) {
108 if ( $this->db->getType() === 'oracle' ) {
109 $database = $quote . $database . '.';
110 } else {
111 $database = $quote . $database . $quote . '.';
112 }
113 }
114
115 if ( $prefix === null ) {
116 $prefix = $this->dbPrefix();
117 }
118
119 if ( $this->db->getType() === 'oracle' ) {
120 return strtoupper( $database . $quote . $prefix . $table );
121 } else {
122 return $database . $quote . $prefix . $table . $quote;
123 }
124 }
125
126 public function testTableNameLocal() {
127 $this->assertEquals(
128 $this->prefixAndQuote( 'tablename' ),
129 $this->db->tableName( 'tablename' )
130 );
131 }
132
133 public function testTableNameRawLocal() {
134 $this->assertEquals(
135 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
136 $this->db->tableName( 'tablename', 'raw' )
137 );
138 }
139
140 public function testTableNameShared() {
141 $this->assertEquals(
142 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
143 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
144 );
145
146 $this->assertEquals(
147 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
148 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
149 );
150 }
151
152 public function testTableNameRawShared() {
153 $this->assertEquals(
154 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
155 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
156 );
157
158 $this->assertEquals(
159 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
160 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
161 );
162 }
163
164 public function testTableNameForeign() {
165 $this->assertEquals(
166 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
167 $this->db->tableName( 'databasename.tablename' )
168 );
169 }
170
171 public function testTableNameRawForeign() {
172 $this->assertEquals(
173 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
174 $this->db->tableName( 'databasename.tablename', 'raw' )
175 );
176 }
177
178 public function testStoredFunctions() {
179 if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
180 $this->markTestSkipped( 'MySQL or Postgres required' );
181 }
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" )
187 );
188 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
189 $this->assertEquals( 42, $res->fetchObject()->test );
190 }
191
192 private function dropFunctions() {
193 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
194 . ( $this->db->getType() == 'postgres' ? '()' : '' )
195 );
196 }
197
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() );
202 }
203
204 public function testTransactionIdle() {
205 $db = $this->db;
206
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 );
214 },
215 __METHOD__
216 );
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' );
220
221 $db->clearFlag( DBO_TRX );
222 $flagSet = null;
223 $db->onTransactionIdle(
224 function () use ( $db, &$flagSet ) {
225 $flagSet = $db->getFlag( DBO_TRX );
226 },
227 __METHOD__
228 );
229 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
230 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
231
232 $db->clearFlag( DBO_TRX );
233 $db->onTransactionIdle(
234 function () use ( $db ) {
235 $db->setFlag( DBO_TRX );
236 },
237 __METHOD__
238 );
239 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
240 }
241
242 public function testTransactionResolution() {
243 $db = $this->db;
244
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' );
255
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' );
266 }
267
268 /**
269 * @covers DatabaseBase::setTransactionListener()
270 */
271 public function testTransactionListener() {
272 $db = $this->db;
273
274 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
275 $called = true;
276 } );
277
278 $called = false;
279 $db->begin( __METHOD__ );
280 $db->commit( __METHOD__ );
281 $this->assertTrue( $called, 'Callback reached' );
282
283 $called = false;
284 $db->begin( __METHOD__ );
285 $db->commit( __METHOD__ );
286 $this->assertTrue( $called, 'Callback still reached' );
287
288 $called = false;
289 $db->begin( __METHOD__ );
290 $db->rollback( __METHOD__ );
291 $this->assertTrue( $called, 'Callback reached' );
292
293 $db->setTransactionListener( 'ping', null );
294 $called = false;
295 $db->begin( __METHOD__ );
296 $db->commit( __METHOD__ );
297 $this->assertFalse( $called, 'Callback not reached' );
298 }
299
300 /**
301 * @covers DatabaseBase::flushSnapshot()
302 */
303 public function testFlushSnapshot() {
304 $db = $this->db;
305
306 $db->flushSnapshot( __METHOD__ ); // ok
307 $db->flushSnapshot( __METHOD__ ); // ok
308
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 );
314
315 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
316 }
317
318 public function testGetScopedLock() {
319 $db = $this->db;
320
321 $db->setFlag( DBO_TRX );
322 try {
323 $this->badLockingMethodImplicit( $db );
324 } catch ( RunTimeException $e ) {
325 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
326 }
327 $db->clearFlag( DBO_TRX );
328 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
329 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
330
331 try {
332 $this->badLockingMethodExplicit( $db );
333 } catch ( RunTimeException $e ) {
334 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
335 }
336 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
337 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
338 }
339
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!" );
344 }
345
346 private function badLockingMethodExplicit( IDatabase $db ) {
347 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
348 $db->begin( __METHOD__ );
349 throw new RunTimeException( "Uh oh!" );
350 }
351
352 /**
353 * @covers DatabaseBase::getFlag(
354 * @covers DatabaseBase::setFlag()
355 * @covers DatabaseBase::restoreFlags()
356 */
357 public function testFlagSetting() {
358 $db = $this->db;
359 $origTrx = $db->getFlag( DBO_TRX );
360 $origSsl = $db->getFlag( DBO_SSL );
361
362 if ( $origTrx ) {
363 $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR );
364 } else {
365 $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
366 }
367 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
368
369 if ( $origSsl ) {
370 $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR );
371 } else {
372 $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
373 }
374 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL ) );
375
376 $db2 = clone $db;
377 $db2->restoreFlags( $db::RESTORE_INITIAL );
378 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX ) );
379 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL ) );
380
381 $db->restoreFlags();
382 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
383 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
384
385 $db->restoreFlags();
386 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
387 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
388 }
389
390 /**
391 * @covers DatabaseBase::tablePrefix()
392 * @covers DatabaseBase::dbSchema()
393 */
394 public function testMutators() {
395 $old = $this->db->tablePrefix();
396 $this->assertType( 'string', $old, 'Prefix is string' );
397 $this->assertEquals( $old, $this->db->tablePrefix(), "Prefix unchanged" );
398 $this->assertEquals( $old, $this->db->tablePrefix( 'xxx' ) );
399 $this->assertEquals( 'xxx', $this->db->tablePrefix(), "Prefix set" );
400 $this->db->tablePrefix( $old );
401 $this->assertNotEquals( 'xxx', $this->db->tablePrefix() );
402
403 $old = $this->db->dbSchema();
404 $this->assertType( 'string', $old, 'Schema is string' );
405 $this->assertEquals( $old, $this->db->dbSchema(), "Schema unchanged" );
406 $this->assertEquals( $old, $this->db->dbSchema( 'xxx' ) );
407 $this->assertEquals( 'xxx', $this->db->dbSchema(), "Schema set" );
408 $this->db->dbSchema( $old );
409 $this->assertNotEquals( 'xxx', $this->db->dbSchema() );
410 }
411 }