3 * Holds tests for DatabaseMysqlBase class.
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.
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.
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
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
26 use Wikimedia\Rdbms\MySQLMasterPos
;
27 use Wikimedia\TestingAccessWrapper
;
29 class DatabaseMysqlBaseTest
extends PHPUnit\Framework\TestCase
{
31 use MediaWikiCoversValidator
;
34 * @dataProvider provideDiapers
35 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
37 public function testAddIdentifierQuotes( $expected, $in ) {
38 $db = $this->getMockBuilder( DatabaseMysqli
::class )
39 ->disableOriginalConstructor()
43 $quoted = $db->addIdentifierQuotes( $in );
44 $this->assertEquals( $expected, $quoted );
48 * Feeds testAddIdentifierQuotes
50 * Named per T22281 convention.
52 public static function provideDiapers() {
54 // Format: expected, input
57 // Yeah I really hate loosely typed PHP idiocies nowadays
60 // Dear codereviewer, guess what addIdentifierQuotes()
61 // will return with thoses:
65 // We never know what could happen
69 // Whatchout! Should probably use something more meaningful
70 [ "`'`", "'" ], # single quote
71 [ '`"`', '"' ], # double quote
72 [ '````', '`' ], # backtick
73 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
75 // sneaky NUL bytes are lurking everywhere
77 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
81 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
82 self
::createUnicodeString( '\u0001a\uFFFFb' )
85 self
::createUnicodeString( '`\u0001\uFFFF`' ),
86 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
89 [ '`メインページ`', 'メインページ' ],
90 [ '`Басты_бет`', 'Басты_бет' ],
93 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
94 [ '`Backtick: ```', 'Backtick: `' ],
95 [ '`This is a test`', 'This is a test' ],
99 private static function createUnicodeString( $str ) {
100 return json_decode( '"' . $str . '"' );
103 private function getMockForViews() {
104 $db = $this->getMockBuilder( DatabaseMysqli
::class )
105 ->disableOriginalConstructor()
106 ->setMethods( [ 'fetchRow', 'query' ] )
109 $db->method( 'query' )
110 ->with( $this->anything() )
111 ->willReturn( new FakeResultWrapper( [
112 (object)[ 'Tables_in_' => 'view1' ],
113 (object)[ 'Tables_in_' => 'view2' ],
114 (object)[ 'Tables_in_' => 'myview' ]
121 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
123 public function testListviews() {
124 $db = $this->getMockForViews();
126 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
130 $this->assertEquals( [ 'view1', 'view2' ],
131 $db->listViews( 'view' ) );
132 $this->assertEquals( [ 'myview' ],
133 $db->listViews( 'my' ) );
134 $this->assertEquals( [],
135 $db->listViews( 'UNUSED_PREFIX' ) );
136 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
137 $db->listViews( '' ) );
140 public function testBinLogName() {
141 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
143 $this->assertEquals( "db1052", $pos->binlog
);
144 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
145 $this->assertEquals( [ 2424, 4643 ], $pos->pos
);
149 * @dataProvider provideComparePositions
150 * @covers Wikimedia\Rdbms\MySQLMasterPos
152 public function testHasReached(
153 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
156 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
159 // Each position is has one channel higher than the other
160 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
162 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
164 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
165 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
166 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
167 } else { // channels don't match
168 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
170 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
171 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
175 public static function provideComparePositions() {
176 $now = microtime( true );
181 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
182 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
187 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
188 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
193 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
194 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
200 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
201 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
206 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
207 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
212 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
213 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
217 // MariaDB GTID style
219 new MySQLMasterPos( '255-11-23', $now ),
220 new MySQLMasterPos( '255-11-24', $now ),
225 new MySQLMasterPos( '255-11-99', $now ),
226 new MySQLMasterPos( '255-11-100', $now ),
231 new MySQLMasterPos( '255-11-999', $now ),
232 new MySQLMasterPos( '254-11-1000', $now ),
237 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
238 new MySQLMasterPos( '255-11-24', $now ),
243 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
244 new MySQLMasterPos( '255-11-1000', $now ),
249 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
250 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
255 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
256 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
261 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
262 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
267 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
268 new MySQLMasterPos( '254-11-1000', $now ),
276 * @dataProvider provideChannelPositions
277 * @covers Wikimedia\Rdbms\MySQLMasterPos
279 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
280 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
281 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
283 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
284 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
287 public static function provideChannelPositions() {
288 $now = microtime( true );
292 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
293 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
297 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
298 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
302 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
303 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
307 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
308 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
315 * @dataProvider provideCommonDomainGTIDs
316 * @covers Wikimedia\Rdbms\MySQLMasterPos
318 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
319 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
322 public static function provideCommonDomainGTIDs() {
325 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
326 new MySQLMasterPos( '255-11-1000', 1 ),
331 '2E11FA47-71CA-11E1-9E33-C80AA9429562:5,' .
332 '3E11FA47-71CA-11E1-9E33-C80AA9429562:99,' .
333 '7E11FA47-71CA-11E1-9E33-C80AA9429562:30',
337 '1E11FA47-71CA-11E1-9E33-C80AA9429562:100,' .
338 '3E11FA47-71CA-11E1-9E33-C80AA9429562:66',
341 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ]
347 * @dataProvider provideLagAmounts
348 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
349 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
351 public function testPtHeartbeat( $lag ) {
352 $db = $this->getMockBuilder( DatabaseMysqli
::class )
353 ->disableOriginalConstructor()
355 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
358 $db->method( 'getLagDetectionMethod' )
359 ->willReturn( 'pt-heartbeat' );
361 $db->method( 'getMasterServerInfo' )
362 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
364 // Fake the current time.
365 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
366 $now = (float)$nowSec +
(float)$nowSecFrac;
367 // Fake the heartbeat time.
368 // Work arounds for weak DataTime microseconds support.
369 $ptTime = $now - $lag;
370 $ptSec = (int)$ptTime;
371 $ptSecFrac = ( $ptTime - $ptSec );
372 $ptDateTime = new DateTime( "@$ptSec" );
373 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
374 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
376 $db->method( 'getHeartbeatData' )
377 ->with( [ 'server_id' => 172 ] )
378 ->willReturn( [ $ptTimeISO, $now ] );
380 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
381 $lagEst = $db->getLag();
383 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
384 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
387 public static function provideLagAmounts() {
401 * @covers Wikimedia\Rdbms\MySQLMasterPos
403 public function testSerialize() {
404 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
405 $roundtripPos = unserialize( serialize( $pos ) );
407 $this->assertEquals( $pos, $roundtripPos );
409 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
410 $roundtripPos = unserialize( serialize( $pos ) );
412 $this->assertEquals( $pos, $roundtripPos );
416 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
417 * @dataProvider provideInsertSelectCases
419 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
420 $db = $this->getMockBuilder( DatabaseMysqli
::class )
421 ->disableOriginalConstructor()
422 ->setMethods( [ 'getReplicationSafetyInfo' ] )
424 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
425 $dbw = TestingAccessWrapper
::newFromObject( $db );
427 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
430 public function provideInsertSelectCases() {
436 'innodb_autoinc_lock_mode' => '2',
437 'binlog_format' => 'ROW',
445 'innodb_autoinc_lock_mode' => '2',
446 'binlog_format' => 'ROW',
454 'innodb_autoinc_lock_mode' => '0',
455 'binlog_format' => 'STATEMENT',
463 'innodb_autoinc_lock_mode' => '2',
464 'binlog_format' => 'STATEMENT',
469 [ 'NO_AUTO_COLUMNS' ],
472 'innodb_autoinc_lock_mode' => '0',
473 'binlog_format' => 'STATEMENT',
481 'innodb_autoinc_lock_mode' => 0,
482 'binlog_format' => 'STATEMENT',
487 [ 'NO_AUTO_COLUMNS' ],
490 'innodb_autoinc_lock_mode' => 2,
491 'binlog_format' => 'STATEMENT',
496 [ 'NO_AUTO_COLUMNS' ],
499 'innodb_autoinc_lock_mode' => 0,
500 'binlog_format' => 'STATEMENT',
509 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
511 public function testBuildIntegerCast() {
512 $db = $this->getMockBuilder( DatabaseMysqli
::class )
513 ->disableOriginalConstructor()
516 $output = $db->buildIntegerCast( 'fieldName' );
517 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
521 * @covers Wikimedia\Rdbms\Database::setIndexAliases
523 public function testIndexAliases() {
524 $db = $this->getMockBuilder( DatabaseMysqli
::class )
525 ->disableOriginalConstructor()
526 ->setMethods( [ 'mysqlRealEscapeString' ] )
528 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
530 return str_replace( "'", "\\'", $s );
534 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
535 $sql = $db->selectSQLText(
536 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
539 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
543 $db->setIndexAliases( [] );
544 $sql = $db->selectSQLText(
545 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
548 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
554 * @covers Wikimedia\Rdbms\Database::setTableAliases
556 public function testTableAliases() {
557 $db = $this->getMockBuilder( DatabaseMysqli
::class )
558 ->disableOriginalConstructor()
559 ->setMethods( [ 'mysqlRealEscapeString' ] )
561 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
563 return str_replace( "'", "\\'", $s );
567 $db->setTableAliases( [
568 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
570 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
573 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
577 $db->setTableAliases( [] );
578 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
581 "SELECT field FROM `meow` WHERE a = 'x' ",