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.
29 * Fake class around abstract class so we can call concrete methods.
31 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
33 function __construct() {
34 $this->profiler
= new ProfilerStub( [] );
35 $this->trxProfiler
= new TransactionProfiler();
36 $this->cliMode
= true;
37 $this->connLogger
= new \Psr\Log\
NullLogger();
38 $this->queryLogger
= new \Psr\Log\
NullLogger();
39 $this->errorLogger
= function ( Exception
$e ) {
40 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
42 $this->currentDomain
= DatabaseDomain
::newUnspecified();
45 protected function closeConnection() {
48 protected function doQuery( $sql ) {
52 protected function mysqlConnect( $realServer ) {
55 protected function mysqlSetCharset( $charset ) {
58 protected function mysqlFreeResult( $res ) {
61 protected function mysqlFetchObject( $res ) {
64 protected function mysqlFetchArray( $res ) {
67 protected function mysqlNumRows( $res ) {
70 protected function mysqlNumFields( $res ) {
73 protected function mysqlFieldName( $res, $n ) {
76 protected function mysqlFieldType( $res, $n ) {
79 protected function mysqlDataSeek( $res, $row ) {
82 protected function mysqlError( $conn = null ) {
85 protected function mysqlFetchField( $res, $n ) {
88 protected function mysqlRealEscapeString( $s ) {
92 // From interface DatabaseType
96 function lastErrno() {
99 function affectedRows() {
102 function getServerVersion() {
106 class DatabaseMysqlBaseTest
extends MediaWikiTestCase
{
108 * @dataProvider provideDiapers
109 * @covers DatabaseMysqlBase::addIdentifierQuotes
111 public function testAddIdentifierQuotes( $expected, $in ) {
112 $db = new FakeDatabaseMysqlBase();
113 $quoted = $db->addIdentifierQuotes( $in );
114 $this->assertEquals( $expected, $quoted );
118 * Feeds testAddIdentifierQuotes
120 * Named per bug 20281 convention.
122 function provideDiapers() {
124 // Format: expected, input
127 // Yeah I really hate loosely typed PHP idiocies nowadays
130 // Dear codereviewer, guess what addIdentifierQuotes()
131 // will return with thoses:
135 // We never know what could happen
139 // Whatchout! Should probably use something more meaningful
140 [ "`'`", "'" ], # single quote
141 [ '`"`', '"' ], # double quote
142 [ '````', '`' ], # backtick
143 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
145 // sneaky NUL bytes are lurking everywhere
147 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
151 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
152 self
::createUnicodeString( '\u0001a\uFFFFb' )
155 self
::createUnicodeString( '`\u0001\uFFFF`' ),
156 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
159 [ '`メインページ`', 'メインページ' ],
160 [ '`Басты_бет`', 'Басты_бет' ],
163 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
164 [ '`Backtick: ```', 'Backtick: `' ],
165 [ '`This is a test`', 'This is a test' ],
169 private static function createUnicodeString( $str ) {
170 return json_decode( '"' . $str . '"' );
173 function getMockForViews() {
174 $db = $this->getMockBuilder( 'DatabaseMysql' )
175 ->disableOriginalConstructor()
176 ->setMethods( [ 'fetchRow', 'query' ] )
179 $db->method( 'query' )
180 ->with( $this->anything() )
181 ->willReturn( null );
183 $db->method( 'fetchRow' )
184 ->with( $this->anything() )
185 ->will( $this->onConsecutiveCalls(
186 [ 'Tables_in_' => 'view1' ],
187 [ 'Tables_in_' => 'view2' ],
188 [ 'Tables_in_' => 'myview' ],
194 * @covers DatabaseMysqlBase::listViews
196 function testListviews() {
197 $db = $this->getMockForViews();
199 // The first call populate an internal cache of views
200 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
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 * @covers DatabaseMysqlBase::isView
218 * @dataProvider provideViewExistanceChecks
220 function testIsView( $isView, $viewName ) {
221 $db = $this->getMockForViews();
225 $this->assertTrue( $db->isView( $viewName ),
226 "$viewName should be considered a view" );
230 $this->assertFalse( $db->isView( $viewName ),
231 "$viewName has not been defined as a view" );
237 function provideViewExistanceChecks() {
239 // format: whether it is a view, view name
248 [ false, 'OH_MY_GOD' ], # they killed kenny!
253 * @dataProvider provideComparePositions
255 function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
257 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
259 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
260 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
261 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
262 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
263 } else { // channels don't match
264 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
266 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
267 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
271 function provideComparePositions() {
275 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
276 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
280 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
281 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
285 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
286 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
291 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
292 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
296 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
297 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
301 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
302 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
305 // MariaDB GTID style
307 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
308 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
312 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
313 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
317 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
318 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
325 * @dataProvider provideChannelPositions
327 function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
328 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
329 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
332 function provideChannelPositions() {
335 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
336 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
340 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
341 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
345 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
346 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
350 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
351 new MySQLMasterPos( 'trump2016.000976', '10000' ),
358 * @dataProvider provideLagAmounts
360 function testPtHeartbeat( $lag ) {
361 $db = $this->getMockBuilder( 'DatabaseMysql' )
362 ->disableOriginalConstructor()
364 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
367 $db->method( 'getLagDetectionMethod' )
368 ->willReturn( 'pt-heartbeat' );
370 $db->method( 'getMasterServerInfo' )
371 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
373 // Fake the current time.
374 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
375 $now = (float)$nowSec +
(float)$nowSecFrac;
376 // Fake the heartbeat time.
377 // Work arounds for weak DataTime microseconds support.
378 $ptTime = $now - $lag;
379 $ptSec = (int)$ptTime;
380 $ptSecFrac = ( $ptTime - $ptSec );
381 $ptDateTime = new DateTime( "@$ptSec" );
382 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
383 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
385 $db->method( 'getHeartbeatData' )
386 ->with( [ 'server_id' => 172 ] )
387 ->willReturn( [ $ptTimeISO, $now ] );
389 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
390 $lagEst = $db->getLag();
392 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
393 $this->assertLessThan( $lag +
.010, $lagEst, "Correct heatbeat lag" );
396 function provideLagAmounts() {