3 * Holds tests for DatabaseMysqlBase MediaWiki 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
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
28 use Wikimedia\Rdbms\TransactionProfiler
;
29 use Wikimedia\Rdbms\DatabaseDomain
;
32 * Fake class around abstract class so we can call concrete methods.
34 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
36 function __construct() {
37 $this->profiler
= new ProfilerStub( [] );
38 $this->trxProfiler
= new TransactionProfiler();
39 $this->cliMode
= true;
40 $this->connLogger
= new \Psr\Log\
NullLogger();
41 $this->queryLogger
= new \Psr\Log\
NullLogger();
42 $this->errorLogger
= function ( Exception
$e ) {
43 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
45 $this->currentDomain
= DatabaseDomain
::newUnspecified();
48 protected function closeConnection() {
51 protected function doQuery( $sql ) {
55 protected function mysqlConnect( $realServer ) {
58 protected function mysqlSetCharset( $charset ) {
61 protected function mysqlFreeResult( $res ) {
64 protected function mysqlFetchObject( $res ) {
67 protected function mysqlFetchArray( $res ) {
70 protected function mysqlNumRows( $res ) {
73 protected function mysqlNumFields( $res ) {
76 protected function mysqlFieldName( $res, $n ) {
79 protected function mysqlFieldType( $res, $n ) {
82 protected function mysqlDataSeek( $res, $row ) {
85 protected function mysqlError( $conn = null ) {
88 protected function mysqlFetchField( $res, $n ) {
91 protected function mysqlRealEscapeString( $s ) {
98 function lastErrno() {
101 function affectedRows() {
104 function getServerVersion() {
108 class DatabaseMysqlBaseTest
extends MediaWikiTestCase
{
110 * @dataProvider provideDiapers
111 * @covers DatabaseMysqlBase::addIdentifierQuotes
113 public function testAddIdentifierQuotes( $expected, $in ) {
114 $db = new FakeDatabaseMysqlBase();
115 $quoted = $db->addIdentifierQuotes( $in );
116 $this->assertEquals( $expected, $quoted );
120 * Feeds testAddIdentifierQuotes
122 * Named per bug 20281 convention.
124 function provideDiapers() {
126 // Format: expected, input
129 // Yeah I really hate loosely typed PHP idiocies nowadays
132 // Dear codereviewer, guess what addIdentifierQuotes()
133 // will return with thoses:
137 // We never know what could happen
141 // Whatchout! Should probably use something more meaningful
142 [ "`'`", "'" ], # single quote
143 [ '`"`', '"' ], # double quote
144 [ '````', '`' ], # backtick
145 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
147 // sneaky NUL bytes are lurking everywhere
149 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
153 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
154 self
::createUnicodeString( '\u0001a\uFFFFb' )
157 self
::createUnicodeString( '`\u0001\uFFFF`' ),
158 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
161 [ '`メインページ`', 'メインページ' ],
162 [ '`Басты_бет`', 'Басты_бет' ],
165 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
166 [ '`Backtick: ```', 'Backtick: `' ],
167 [ '`This is a test`', 'This is a test' ],
171 private static function createUnicodeString( $str ) {
172 return json_decode( '"' . $str . '"' );
175 function getMockForViews() {
176 $db = $this->getMockBuilder( 'DatabaseMysqli' )
177 ->disableOriginalConstructor()
178 ->setMethods( [ 'fetchRow', 'query' ] )
181 $db->method( 'query' )
182 ->with( $this->anything() )
183 ->willReturn( new FakeResultWrapper( [
184 (object)[ 'Tables_in_' => 'view1' ],
185 (object)[ 'Tables_in_' => 'view2' ],
186 (object)[ 'Tables_in_' => 'myview' ]
192 * @covers DatabaseMysqlBase::listViews
194 function testListviews() {
195 $db = $this->getMockForViews();
197 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
201 $this->assertEquals( [ 'view1', 'view2' ],
202 $db->listViews( 'view' ) );
203 $this->assertEquals( [ 'myview' ],
204 $db->listViews( 'my' ) );
205 $this->assertEquals( [],
206 $db->listViews( 'UNUSED_PREFIX' ) );
207 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
208 $db->listViews( '' ) );
212 * @dataProvider provideComparePositions
214 function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
216 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
218 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
219 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
220 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
221 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
222 } else { // channels don't match
223 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
225 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
226 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
230 function provideComparePositions() {
234 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
235 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
239 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
240 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
244 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
245 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
250 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
251 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
255 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
256 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
260 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
261 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
264 // MariaDB GTID style
266 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
267 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
271 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
272 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
276 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
277 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
284 * @dataProvider provideChannelPositions
286 function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
287 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
288 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
291 function provideChannelPositions() {
294 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
295 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
299 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
300 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
304 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
305 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
309 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
310 new MySQLMasterPos( 'trump2016.000976', '10000' ),
317 * @dataProvider provideLagAmounts
319 function testPtHeartbeat( $lag ) {
320 $db = $this->getMockBuilder( 'DatabaseMysqli' )
321 ->disableOriginalConstructor()
323 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
326 $db->method( 'getLagDetectionMethod' )
327 ->willReturn( 'pt-heartbeat' );
329 $db->method( 'getMasterServerInfo' )
330 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
332 // Fake the current time.
333 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
334 $now = (float)$nowSec +
(float)$nowSecFrac;
335 // Fake the heartbeat time.
336 // Work arounds for weak DataTime microseconds support.
337 $ptTime = $now - $lag;
338 $ptSec = (int)$ptTime;
339 $ptSecFrac = ( $ptTime - $ptSec );
340 $ptDateTime = new DateTime( "@$ptSec" );
341 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
342 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
344 $db->method( 'getHeartbeatData' )
345 ->with( [ 'server_id' => 172 ] )
346 ->willReturn( [ $ptTimeISO, $now ] );
348 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
349 $lagEst = $db->getLag();
351 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
352 $this->assertLessThan( $lag +
.010, $lagEst, "Correct heatbeat lag" );
355 function provideLagAmounts() {