Merge "Add support for 'hu-formal'"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase class.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
24 */
25
26 use Wikimedia\Rdbms\TransactionProfiler;
27 use Wikimedia\Rdbms\DatabaseDomain;
28 use Wikimedia\Rdbms\MySQLMasterPos;
29 use Wikimedia\Rdbms\DatabaseMysqlBase;
30 use Wikimedia\Rdbms\DatabaseMysqli;
31 use Wikimedia\Rdbms\Database;
32 use Wikimedia\TestingAccessWrapper;
33
34 /**
35 * Fake class around abstract class so we can call concrete methods.
36 */
37 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
38 // From Database
39 function __construct() {
40 $this->profiler = new ProfilerStub( [] );
41 $this->trxProfiler = new TransactionProfiler();
42 $this->cliMode = true;
43 $this->connLogger = new \Psr\Log\NullLogger();
44 $this->queryLogger = new \Psr\Log\NullLogger();
45 $this->errorLogger = function ( Exception $e ) {
46 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
47 };
48 $this->currentDomain = DatabaseDomain::newUnspecified();
49 }
50
51 protected function closeConnection() {
52 }
53
54 protected function doQuery( $sql ) {
55 }
56
57 protected function fetchAffectedRowCount() {
58 }
59
60 // From DatabaseMysqli
61 protected function mysqlConnect( $realServer ) {
62 }
63
64 protected function mysqlSetCharset( $charset ) {
65 }
66
67 protected function mysqlFreeResult( $res ) {
68 }
69
70 protected function mysqlFetchObject( $res ) {
71 }
72
73 protected function mysqlFetchArray( $res ) {
74 }
75
76 protected function mysqlNumRows( $res ) {
77 }
78
79 protected function mysqlNumFields( $res ) {
80 }
81
82 protected function mysqlFieldName( $res, $n ) {
83 }
84
85 protected function mysqlFieldType( $res, $n ) {
86 }
87
88 protected function mysqlDataSeek( $res, $row ) {
89 }
90
91 protected function mysqlError( $conn = null ) {
92 }
93
94 protected function mysqlFetchField( $res, $n ) {
95 }
96
97 protected function mysqlRealEscapeString( $s ) {
98 }
99
100 function insertId() {
101 }
102
103 function lastErrno() {
104 }
105
106 function affectedRows() {
107 }
108
109 function getServerVersion() {
110 }
111 }
112
113 class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
114
115 use MediaWikiCoversValidator;
116
117 /**
118 * @dataProvider provideDiapers
119 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
120 */
121 public function testAddIdentifierQuotes( $expected, $in ) {
122 $db = new FakeDatabaseMysqlBase();
123 $quoted = $db->addIdentifierQuotes( $in );
124 $this->assertEquals( $expected, $quoted );
125 }
126
127 /**
128 * Feeds testAddIdentifierQuotes
129 *
130 * Named per T22281 convention.
131 */
132 public static function provideDiapers() {
133 return [
134 // Format: expected, input
135 [ '``', '' ],
136
137 // Yeah I really hate loosely typed PHP idiocies nowadays
138 [ '``', null ],
139
140 // Dear codereviewer, guess what addIdentifierQuotes()
141 // will return with thoses:
142 [ '``', false ],
143 [ '`1`', true ],
144
145 // We never know what could happen
146 [ '`0`', 0 ],
147 [ '`1`', 1 ],
148
149 // Whatchout! Should probably use something more meaningful
150 [ "`'`", "'" ], # single quote
151 [ '`"`', '"' ], # double quote
152 [ '````', '`' ], # backtick
153 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
154
155 // sneaky NUL bytes are lurking everywhere
156 [ '``', "\0" ],
157 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
158
159 // unicode chars
160 [
161 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
162 self::createUnicodeString( '\u0001a\uFFFFb' )
163 ],
164 [
165 self::createUnicodeString( '`\u0001\uFFFF`' ),
166 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
167 ],
168 [ '`☃`', '☃' ],
169 [ '`メインページ`', 'メインページ' ],
170 [ '`Басты_бет`', 'Басты_бет' ],
171
172 // Real world:
173 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
174 [ '`Backtick: ```', 'Backtick: `' ],
175 [ '`This is a test`', 'This is a test' ],
176 ];
177 }
178
179 private static function createUnicodeString( $str ) {
180 return json_decode( '"' . $str . '"' );
181 }
182
183 private function getMockForViews() {
184 $db = $this->getMockBuilder( DatabaseMysqli::class )
185 ->disableOriginalConstructor()
186 ->setMethods( [ 'fetchRow', 'query' ] )
187 ->getMock();
188
189 $db->method( 'query' )
190 ->with( $this->anything() )
191 ->willReturn( new FakeResultWrapper( [
192 (object)[ 'Tables_in_' => 'view1' ],
193 (object)[ 'Tables_in_' => 'view2' ],
194 (object)[ 'Tables_in_' => 'myview' ]
195 ] ) );
196
197 return $db;
198 }
199
200 /**
201 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
202 */
203 public function testListviews() {
204 $db = $this->getMockForViews();
205
206 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
207 $db->listViews() );
208
209 // Prefix filtering
210 $this->assertEquals( [ 'view1', 'view2' ],
211 $db->listViews( 'view' ) );
212 $this->assertEquals( [ 'myview' ],
213 $db->listViews( 'my' ) );
214 $this->assertEquals( [],
215 $db->listViews( 'UNUSED_PREFIX' ) );
216 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
217 $db->listViews( '' ) );
218 }
219
220 public function testBinLogName() {
221 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
222
223 $this->assertEquals( "db1052", $pos->binlog );
224 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
225 $this->assertEquals( [ 2424, 4643 ], $pos->pos );
226 }
227
228 /**
229 * @dataProvider provideComparePositions
230 * @covers Wikimedia\Rdbms\MySQLMasterPos
231 */
232 public function testHasReached(
233 MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match, $hetero
234 ) {
235 if ( $match ) {
236 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
237
238 if ( $hetero ) {
239 // Each position is has one channel higher than the other
240 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
241 } else {
242 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
243 }
244 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
245 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
246 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
247 } else { // channels don't match
248 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
249
250 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
251 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
252 }
253 }
254
255 public static function provideComparePositions() {
256 $now = microtime( true );
257
258 return [
259 // Binlog style
260 [
261 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
262 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
263 true,
264 false
265 ],
266 [
267 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
268 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
269 true,
270 false
271 ],
272 [
273 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
274 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
275 false,
276 false
277 ],
278 // MySQL GTID style
279 [
280 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
281 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
282 true,
283 false
284 ],
285 [
286 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
287 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
288 true,
289 false
290 ],
291 [
292 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
293 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
294 false,
295 false
296 ],
297 // MariaDB GTID style
298 [
299 new MySQLMasterPos( '255-11-23', $now ),
300 new MySQLMasterPos( '255-11-24', $now ),
301 true,
302 false
303 ],
304 [
305 new MySQLMasterPos( '255-11-99', $now ),
306 new MySQLMasterPos( '255-11-100', $now ),
307 true,
308 false
309 ],
310 [
311 new MySQLMasterPos( '255-11-999', $now ),
312 new MySQLMasterPos( '254-11-1000', $now ),
313 false,
314 false
315 ],
316 [
317 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
318 new MySQLMasterPos( '255-11-24', $now ),
319 true,
320 false
321 ],
322 [
323 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
324 new MySQLMasterPos( '255-11-1000', $now ),
325 true,
326 false
327 ],
328 [
329 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
330 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
331 true,
332 false
333 ],
334 [
335 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
336 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
337 true,
338 false
339 ],
340 [
341 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
342 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
343 true,
344 true
345 ],
346 [
347 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
348 new MySQLMasterPos( '254-11-1000', $now ),
349 false,
350 false
351 ],
352 ];
353 }
354
355 /**
356 * @dataProvider provideChannelPositions
357 * @covers Wikimedia\Rdbms\MySQLMasterPos
358 */
359 public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
360 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
361 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
362
363 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
364 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
365 }
366
367 public static function provideChannelPositions() {
368 $now = microtime( true );
369
370 return [
371 [
372 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
373 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
374 true
375 ],
376 [
377 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
378 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
379 true
380 ],
381 [
382 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
383 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
384 false
385 ],
386 [
387 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
388 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
389 false
390 ],
391 ];
392 }
393
394 /**
395 * @dataProvider provideCommonDomainGTIDs
396 * @covers Wikimedia\Rdbms\MySQLMasterPos
397 */
398 public function testCommonGtidDomains( MySQLMasterPos $pos, MySQLMasterPos $ref, $gtids ) {
399 $this->assertEquals( $gtids, MySQLMasterPos::getCommonDomainGTIDs( $pos, $ref ) );
400 }
401
402 public static function provideCommonDomainGTIDs() {
403 return [
404 [
405 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
406 new MySQLMasterPos( '255-11-1000', 1 ),
407 [ '255-13-99' ]
408 ],
409 [
410 new MySQLMasterPos(
411 '2E11FA47-71CA-11E1-9E33-C80AA9429562:5,' .
412 '3E11FA47-71CA-11E1-9E33-C80AA9429562:99,' .
413 '7E11FA47-71CA-11E1-9E33-C80AA9429562:30',
414 1
415 ),
416 new MySQLMasterPos(
417 '1E11FA47-71CA-11E1-9E33-C80AA9429562:100,' .
418 '3E11FA47-71CA-11E1-9E33-C80AA9429562:66',
419 1
420 ),
421 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ]
422 ]
423 ];
424 }
425
426 /**
427 * @dataProvider provideLagAmounts
428 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
429 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
430 */
431 public function testPtHeartbeat( $lag ) {
432 $db = $this->getMockBuilder( DatabaseMysqli::class )
433 ->disableOriginalConstructor()
434 ->setMethods( [
435 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
436 ->getMock();
437
438 $db->method( 'getLagDetectionMethod' )
439 ->willReturn( 'pt-heartbeat' );
440
441 $db->method( 'getMasterServerInfo' )
442 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
443
444 // Fake the current time.
445 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
446 $now = (float)$nowSec + (float)$nowSecFrac;
447 // Fake the heartbeat time.
448 // Work arounds for weak DataTime microseconds support.
449 $ptTime = $now - $lag;
450 $ptSec = (int)$ptTime;
451 $ptSecFrac = ( $ptTime - $ptSec );
452 $ptDateTime = new DateTime( "@$ptSec" );
453 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
454 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
455
456 $db->method( 'getHeartbeatData' )
457 ->with( [ 'server_id' => 172 ] )
458 ->willReturn( [ $ptTimeISO, $now ] );
459
460 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
461 $lagEst = $db->getLag();
462
463 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
464 $this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
465 }
466
467 public static function provideLagAmounts() {
468 return [
469 [ 0 ],
470 [ 0.3 ],
471 [ 6.5 ],
472 [ 10.1 ],
473 [ 200.2 ],
474 [ 400.7 ],
475 [ 600.22 ],
476 [ 1000.77 ],
477 ];
478 }
479
480 /**
481 * @expectedException UnexpectedValueException
482 * @covers Wikimedia\Rdbms\Database::setFlag
483 */
484 public function testDBOIgnoreSet() {
485 $db = new FakeDatabaseMysqlBase();
486
487 $db->setFlag( Database::DBO_IGNORE );
488 }
489
490 /**
491 * @expectedException UnexpectedValueException
492 * @covers Wikimedia\Rdbms\Database::clearFlag
493 */
494 public function testDBOIgnoreClear() {
495 $db = new FakeDatabaseMysqlBase();
496
497 $db->clearFlag( Database::DBO_IGNORE );
498 }
499
500 /**
501 * @covers Wikimedia\Rdbms\MySQLMasterPos
502 */
503 public function testSerialize() {
504 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
505 $roundtripPos = unserialize( serialize( $pos ) );
506
507 $this->assertEquals( $pos, $roundtripPos );
508
509 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
510 $roundtripPos = unserialize( serialize( $pos ) );
511
512 $this->assertEquals( $pos, $roundtripPos );
513 }
514
515 /**
516 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
517 * @dataProvider provideInsertSelectCases
518 */
519 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
520 $db = $this->getMockBuilder( DatabaseMysqli::class )
521 ->disableOriginalConstructor()
522 ->setMethods( [ 'getReplicationSafetyInfo' ] )
523 ->getMock();
524 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
525 $dbw = TestingAccessWrapper::newFromObject( $db );
526
527 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
528 }
529
530 public function provideInsertSelectCases() {
531 return [
532 [
533 [],
534 [],
535 [
536 'innodb_autoinc_lock_mode' => '2',
537 'binlog_format' => 'ROW',
538 ],
539 true
540 ],
541 [
542 [],
543 [ 'LIMIT' => 100 ],
544 [
545 'innodb_autoinc_lock_mode' => '2',
546 'binlog_format' => 'ROW',
547 ],
548 true
549 ],
550 [
551 [],
552 [ 'LIMIT' => 100 ],
553 [
554 'innodb_autoinc_lock_mode' => '0',
555 'binlog_format' => 'STATEMENT',
556 ],
557 false
558 ],
559 [
560 [],
561 [],
562 [
563 'innodb_autoinc_lock_mode' => '2',
564 'binlog_format' => 'STATEMENT',
565 ],
566 false
567 ],
568 [
569 [ 'NO_AUTO_COLUMNS' ],
570 [ 'LIMIT' => 100 ],
571 [
572 'innodb_autoinc_lock_mode' => '0',
573 'binlog_format' => 'STATEMENT',
574 ],
575 false
576 ],
577 [
578 [],
579 [],
580 [
581 'innodb_autoinc_lock_mode' => 0,
582 'binlog_format' => 'STATEMENT',
583 ],
584 true
585 ],
586 [
587 [ 'NO_AUTO_COLUMNS' ],
588 [],
589 [
590 'innodb_autoinc_lock_mode' => 2,
591 'binlog_format' => 'STATEMENT',
592 ],
593 true
594 ],
595 [
596 [ 'NO_AUTO_COLUMNS' ],
597 [],
598 [
599 'innodb_autoinc_lock_mode' => 0,
600 'binlog_format' => 'STATEMENT',
601 ],
602 true
603 ],
604
605 ];
606 }
607 }