Merge "Deprecate jquery.arrowSteps module"
[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 * @covers DatabaseBase::dropTable
30 */
31 public function testAddQuotesNull() {
32 $check = "NULL";
33 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
34 $check = "''";
35 }
36 $this->assertEquals( $check, $this->db->addQuotes( null ) );
37 }
38
39 public function testAddQuotesInt() {
40 # returning just "1234" should be ok too, though...
41 # maybe
42 $this->assertEquals(
43 "'1234'",
44 $this->db->addQuotes( 1234 ) );
45 }
46
47 public function testAddQuotesFloat() {
48 # returning just "1234.5678" would be ok too, though
49 $this->assertEquals(
50 "'1234.5678'",
51 $this->db->addQuotes( 1234.5678 ) );
52 }
53
54 public function testAddQuotesString() {
55 $this->assertEquals(
56 "'string'",
57 $this->db->addQuotes( 'string' ) );
58 }
59
60 public function testAddQuotesStringQuote() {
61 $check = "'string''s cause trouble'";
62 if ( $this->db->getType() === 'mysql' ) {
63 $check = "'string\'s cause trouble'";
64 }
65 $this->assertEquals(
66 $check,
67 $this->db->addQuotes( "string's cause trouble" ) );
68 }
69
70 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
71 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgSharedSchema;
72
73 $this->db->setTableAliases( [
74 $table => [
75 'dbname' => $database,
76 'schema' => null,
77 'prefix' => $prefix
78 ]
79 ] );
80
81 $ret = $this->db->tableName( $table, $format );
82
83 $this->db->setTableAliases( array_fill_keys(
84 $wgSharedDB ? $wgSharedTables : [],
85 [
86 'dbname' => $wgSharedDB,
87 'schema' => $wgSharedSchema,
88 'prefix' => $wgSharedPrefix
89 ]
90 ) );
91
92 return $ret;
93 }
94
95 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
96 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
97 $quote = '';
98 } elseif ( $this->db->getType() === 'mysql' ) {
99 $quote = '`';
100 } elseif ( $this->db->getType() === 'oracle' ) {
101 $quote = '/*Q*/';
102 } else {
103 $quote = '"';
104 }
105
106 if ( $database !== null ) {
107 if ( $this->db->getType() === 'oracle' ) {
108 $database = $quote . $database . '.';
109 } else {
110 $database = $quote . $database . $quote . '.';
111 }
112 }
113
114 if ( $prefix === null ) {
115 $prefix = $this->dbPrefix();
116 }
117
118 if ( $this->db->getType() === 'oracle' ) {
119 return strtoupper( $database . $quote . $prefix . $table );
120 } else {
121 return $database . $quote . $prefix . $table . $quote;
122 }
123 }
124
125 public function testTableNameLocal() {
126 $this->assertEquals(
127 $this->prefixAndQuote( 'tablename' ),
128 $this->db->tableName( 'tablename' )
129 );
130 }
131
132 public function testTableNameRawLocal() {
133 $this->assertEquals(
134 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
135 $this->db->tableName( 'tablename', 'raw' )
136 );
137 }
138
139 public function testTableNameShared() {
140 $this->assertEquals(
141 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
142 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
143 );
144
145 $this->assertEquals(
146 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
147 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
148 );
149 }
150
151 public function testTableNameRawShared() {
152 $this->assertEquals(
153 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
154 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
155 );
156
157 $this->assertEquals(
158 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
159 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
160 );
161 }
162
163 public function testTableNameForeign() {
164 $this->assertEquals(
165 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
166 $this->db->tableName( 'databasename.tablename' )
167 );
168 }
169
170 public function testTableNameRawForeign() {
171 $this->assertEquals(
172 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
173 $this->db->tableName( 'databasename.tablename', 'raw' )
174 );
175 }
176
177 public function testFillPreparedEmpty() {
178 $sql = $this->db->fillPrepared(
179 'SELECT * FROM interwiki', [] );
180 $this->assertEquals(
181 "SELECT * FROM interwiki",
182 $sql );
183 }
184
185 public function testFillPreparedQuestion() {
186 $sql = $this->db->fillPrepared(
187 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
188 [ 4, "Snicker's_paradox" ] );
189
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'";
193 }
194 $this->assertEquals( $check, $sql );
195 }
196
197 public function testFillPreparedBang() {
198 $sql = $this->db->fillPrepared(
199 'SELECT user_id FROM ! WHERE user_name=?',
200 [ '"user"', "Slash's Dot" ] );
201
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'";
205 }
206 $this->assertEquals( $check, $sql );
207 }
208
209 public function testFillPreparedRaw() {
210 $sql = $this->db->fillPrepared(
211 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
212 [ '"user"', "Slash's Dot" ] );
213 $this->assertEquals(
214 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
215 $sql );
216 }
217
218 public function testStoredFunctions() {
219 if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
220 $this->markTestSkipped( 'MySQL or Postgres required' );
221 }
222 global $IP;
223 $this->dropFunctions();
224 $this->functionTest = true;
225 $this->assertTrue(
226 $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
227 );
228 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
229 $this->assertEquals( 42, $res->fetchObject()->test );
230 }
231
232 private function dropFunctions() {
233 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
234 . ( $this->db->getType() == 'postgres' ? '()' : '' )
235 );
236 }
237
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() );
242 }
243
244 public function testTransactionIdle() {
245 $db = $this->db;
246
247 $db->setFlag( DBO_TRX );
248 $called = false;
249 $flagSet = null;
250 $db->onTransactionIdle( function() use ( $db, &$flagSet, &$called ) {
251 $called = true;
252 $flagSet = $db->getFlag( DBO_TRX );
253 } );
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' );
257
258 $db->clearFlag( DBO_TRX );
259 $flagSet = null;
260 $db->onTransactionIdle( function() use ( $db, &$flagSet ) {
261 $flagSet = $db->getFlag( DBO_TRX );
262 } );
263 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
264 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
265
266 $db->clearFlag( DBO_TRX );
267 $db->onTransactionIdle( function() use ( $db ) {
268 $db->setFlag( DBO_TRX );
269 } );
270 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
271 }
272
273 public function testTransactionResolution() {
274 $db = $this->db;
275
276 $db->clearFlag( DBO_TRX );
277 $db->begin( __METHOD__ );
278 $called = false;
279 $db->onTransactionResolution( function() use ( $db, &$called ) {
280 $called = true;
281 $db->setFlag( DBO_TRX );
282 } );
283 $db->commit( __METHOD__ );
284 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
285 $this->assertTrue( $called, 'Callback reached' );
286
287 $db->clearFlag( DBO_TRX );
288 $db->begin( __METHOD__ );
289 $called = false;
290 $db->onTransactionResolution( function() use ( $db, &$called ) {
291 $called = true;
292 $db->setFlag( DBO_TRX );
293 } );
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' );
297 }
298
299 /**
300 * @covers DatabaseBase::setTransactionListener()
301 */
302 public function testTransactionListener() {
303 $db = $this->db;
304
305 $db->setTransactionListener( 'ping', function() use ( $db, &$called ) {
306 $called = true;
307 } );
308
309 $called = false;
310 $db->begin( __METHOD__ );
311 $db->commit( __METHOD__ );
312 $this->assertTrue( $called, 'Callback reached' );
313
314 $called = false;
315 $db->begin( __METHOD__ );
316 $db->commit( __METHOD__ );
317 $this->assertTrue( $called, 'Callback still reached' );
318
319 $called = false;
320 $db->begin( __METHOD__ );
321 $db->rollback( __METHOD__ );
322 $this->assertTrue( $called, 'Callback reached' );
323
324 $db->setTransactionListener( 'ping', null );
325 $called = false;
326 $db->begin( __METHOD__ );
327 $db->commit( __METHOD__ );
328 $this->assertFalse( $called, 'Callback not reached' );
329 }
330
331 /**
332 * @covers DatabaseBase::flushSnapshot()
333 */
334 public function testFlushSnapshot() {
335 $db = $this->db;
336
337 $db->flushSnapshot( __METHOD__ ); // ok
338 $db->flushSnapshot( __METHOD__ ); // ok
339
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 );
345
346 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
347 }
348
349 public function testGetScopedLock() {
350 $db = $this->db;
351
352 $db->setFlag( DBO_TRX );
353 try {
354 $this->badLockingMethodImplicit( $db );
355 } catch ( RunTimeException $e ) {
356 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
357 }
358 $db->clearFlag( DBO_TRX );
359 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
360 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
361
362 try {
363 $this->badLockingMethodExplicit( $db );
364 } catch ( RunTimeException $e ) {
365 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
366 }
367 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
368 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
369 }
370
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!" );
375 }
376
377 private function badLockingMethodExplicit( IDatabase $db ) {
378 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
379 $db->begin( __METHOD__ );
380 throw new RunTimeException( "Uh oh!" );
381 }
382
383 /**
384 * @covers DatabaseBase::getFlag(
385 * @covers DatabaseBase::setFlag()
386 * @covers DatabaseBase::restoreFlags()
387 */
388 public function testFlagSetting() {
389 $db = $this->db;
390 $origTrx = $db->getFlag( DBO_TRX );
391 $origSsl = $db->getFlag( DBO_SSL );
392
393 if ( $origTrx ) {
394 $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR );
395 } else {
396 $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
397 }
398 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
399
400 if ( $origSsl ) {
401 $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR );
402 } else {
403 $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
404 }
405 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL ) );
406
407 $db2 = clone $db;
408 $db2->restoreFlags( $db::RESTORE_INITIAL );
409 $this->assertEquals( $origTrx, $db2->getFlag( DBO_TRX ) );
410 $this->assertEquals( $origSsl, $db2->getFlag( DBO_SSL ) );
411
412 $db->restoreFlags();
413 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
414 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
415
416 $db->restoreFlags();
417 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
418 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
419 }
420 }