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\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
;
34 * Fake class around abstract class so we can call concrete methods.
36 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
38 function __construct() {
39 $this->profiler
= new ProfilerStub( [] );
40 $this->trxProfiler
= new TransactionProfiler();
41 $this->cliMode
= true;
42 $this->connLogger
= new \Psr\Log\
NullLogger();
43 $this->queryLogger
= new \Psr\Log\
NullLogger();
44 $this->errorLogger
= function ( Exception
$e ) {
45 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
47 $this->currentDomain
= DatabaseDomain
::newUnspecified();
50 protected function closeConnection() {
53 protected function doQuery( $sql ) {
56 // From DatabaseMysqli
57 protected function mysqlConnect( $realServer ) {
60 protected function mysqlSetCharset( $charset ) {
63 protected function mysqlFreeResult( $res ) {
66 protected function mysqlFetchObject( $res ) {
69 protected function mysqlFetchArray( $res ) {
72 protected function mysqlNumRows( $res ) {
75 protected function mysqlNumFields( $res ) {
78 protected function mysqlFieldName( $res, $n ) {
81 protected function mysqlFieldType( $res, $n ) {
84 protected function mysqlDataSeek( $res, $row ) {
87 protected function mysqlError( $conn = null ) {
90 protected function mysqlFetchField( $res, $n ) {
93 protected function mysqlRealEscapeString( $s ) {
99 function lastErrno() {
102 function affectedRows() {
105 function getServerVersion() {
109 class DatabaseMysqlBaseTest
extends PHPUnit_Framework_TestCase
{
111 use MediaWikiCoversValidator
;
114 * @dataProvider provideDiapers
115 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
117 public function testAddIdentifierQuotes( $expected, $in ) {
118 $db = new FakeDatabaseMysqlBase();
119 $quoted = $db->addIdentifierQuotes( $in );
120 $this->assertEquals( $expected, $quoted );
124 * Feeds testAddIdentifierQuotes
126 * Named per T22281 convention.
128 public static function provideDiapers() {
130 // Format: expected, input
133 // Yeah I really hate loosely typed PHP idiocies nowadays
136 // Dear codereviewer, guess what addIdentifierQuotes()
137 // will return with thoses:
141 // We never know what could happen
145 // Whatchout! Should probably use something more meaningful
146 [ "`'`", "'" ], # single quote
147 [ '`"`', '"' ], # double quote
148 [ '````', '`' ], # backtick
149 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
151 // sneaky NUL bytes are lurking everywhere
153 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
157 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
158 self
::createUnicodeString( '\u0001a\uFFFFb' )
161 self
::createUnicodeString( '`\u0001\uFFFF`' ),
162 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
165 [ '`メインページ`', 'メインページ' ],
166 [ '`Басты_бет`', 'Басты_бет' ],
169 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
170 [ '`Backtick: ```', 'Backtick: `' ],
171 [ '`This is a test`', 'This is a test' ],
175 private static function createUnicodeString( $str ) {
176 return json_decode( '"' . $str . '"' );
179 private function getMockForViews() {
180 $db = $this->getMockBuilder( DatabaseMysqli
::class )
181 ->disableOriginalConstructor()
182 ->setMethods( [ 'fetchRow', 'query' ] )
185 $db->method( 'query' )
186 ->with( $this->anything() )
187 ->willReturn( new FakeResultWrapper( [
188 (object)[ 'Tables_in_' => 'view1' ],
189 (object)[ 'Tables_in_' => 'view2' ],
190 (object)[ 'Tables_in_' => 'myview' ]
197 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
199 public function testListviews() {
200 $db = $this->getMockForViews();
202 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
206 $this->assertEquals( [ 'view1', 'view2' ],
207 $db->listViews( 'view' ) );
208 $this->assertEquals( [ 'myview' ],
209 $db->listViews( 'my' ) );
210 $this->assertEquals( [],
211 $db->listViews( 'UNUSED_PREFIX' ) );
212 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
213 $db->listViews( '' ) );
217 * @dataProvider provideComparePositions
218 * @covers Wikimedia\Rdbms\MySQLMasterPos
220 public function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
222 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
224 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
225 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
226 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
227 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
228 } else { // channels don't match
229 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
231 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
232 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
236 public static function provideComparePositions() {
240 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
241 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
245 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
246 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
250 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
251 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
256 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
257 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
261 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
262 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
266 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
267 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
270 // MariaDB GTID style
272 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
273 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
277 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
278 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
282 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
283 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
290 * @dataProvider provideChannelPositions
291 * @covers Wikimedia\Rdbms\MySQLMasterPos
293 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
294 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
295 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
298 public static function provideChannelPositions() {
301 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
302 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
306 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
307 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
311 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
312 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
316 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
317 new MySQLMasterPos( 'trump2016.000976', '10000' ),
324 * @dataProvider provideLagAmounts
325 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
326 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
328 public function testPtHeartbeat( $lag ) {
329 $db = $this->getMockBuilder( DatabaseMysqli
::class )
330 ->disableOriginalConstructor()
332 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
335 $db->method( 'getLagDetectionMethod' )
336 ->willReturn( 'pt-heartbeat' );
338 $db->method( 'getMasterServerInfo' )
339 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
341 // Fake the current time.
342 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
343 $now = (float)$nowSec +
(float)$nowSecFrac;
344 // Fake the heartbeat time.
345 // Work arounds for weak DataTime microseconds support.
346 $ptTime = $now - $lag;
347 $ptSec = (int)$ptTime;
348 $ptSecFrac = ( $ptTime - $ptSec );
349 $ptDateTime = new DateTime( "@$ptSec" );
350 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
351 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
353 $db->method( 'getHeartbeatData' )
354 ->with( [ 'server_id' => 172 ] )
355 ->willReturn( [ $ptTimeISO, $now ] );
357 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
358 $lagEst = $db->getLag();
360 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
361 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
364 public static function provideLagAmounts() {
378 * @expectedException UnexpectedValueException
379 * @covers Wikimedia\Rdbms\Database::setFlag
381 public function testDBOIgnoreSet() {
382 $db = new FakeDatabaseMysqlBase();
384 $db->setFlag( Database
::DBO_IGNORE
);
388 * @expectedException UnexpectedValueException
389 * @covers Wikimedia\Rdbms\Database::clearFlag
391 public function testDBOIgnoreClear() {
392 $db = new FakeDatabaseMysqlBase();
394 $db->clearFlag( Database
::DBO_IGNORE
);