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() {
36 protected function closeConnection() {
39 protected function doQuery( $sql ) {
43 protected function mysqlConnect( $realServer ) {
46 protected function mysqlSetCharset( $charset ) {
49 protected function mysqlFreeResult( $res ) {
52 protected function mysqlFetchObject( $res ) {
55 protected function mysqlFetchArray( $res ) {
58 protected function mysqlNumRows( $res ) {
61 protected function mysqlNumFields( $res ) {
64 protected function mysqlFieldName( $res, $n ) {
67 protected function mysqlFieldType( $res, $n ) {
70 protected function mysqlDataSeek( $res, $row ) {
73 protected function mysqlError( $conn = null ) {
76 protected function mysqlFetchField( $res, $n ) {
79 protected function mysqlPing() {
82 protected function mysqlRealEscapeString( $s ) {
86 // From interface DatabaseType
90 function lastErrno() {
93 function affectedRows() {
96 function getServerVersion() {
100 class DatabaseMysqlBaseTest
extends MediaWikiTestCase
{
102 * @dataProvider provideDiapers
103 * @covers DatabaseMysqlBase::addIdentifierQuotes
105 public function testAddIdentifierQuotes( $expected, $in ) {
106 $db = new FakeDatabaseMysqlBase();
107 $quoted = $db->addIdentifierQuotes( $in );
108 $this->assertEquals( $expected, $quoted );
112 * Feeds testAddIdentifierQuotes
114 * Named per bug 20281 convention.
116 function provideDiapers() {
118 // Format: expected, input
121 // Yeah I really hate loosely typed PHP idiocies nowadays
124 // Dear codereviewer, guess what addIdentifierQuotes()
125 // will return with thoses:
129 // We never know what could happen
133 // Whatchout! Should probably use something more meaningful
134 [ "`'`", "'" ], # single quote
135 [ '`"`', '"' ], # double quote
136 [ '````', '`' ], # backtick
137 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
139 // sneaky NUL bytes are lurking everywhere
141 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
145 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
146 self
::createUnicodeString( '\u0001a\uFFFFb' )
149 self
::createUnicodeString( '`\u0001\uFFFF`' ),
150 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
153 [ '`メインページ`', 'メインページ' ],
154 [ '`Басты_бет`', 'Басты_бет' ],
157 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
158 [ '`Backtick: ```', 'Backtick: `' ],
159 [ '`This is a test`', 'This is a test' ],
163 private static function createUnicodeString( $str ) {
164 return json_decode( '"' . $str . '"' );
167 function getMockForViews() {
168 $db = $this->getMockBuilder( 'DatabaseMysql' )
169 ->disableOriginalConstructor()
170 ->setMethods( [ 'fetchRow', 'query' ] )
173 $db->expects( $this->any() )
175 ->with( $this->anything() )
177 $this->returnValue( null )
180 $db->expects( $this->any() )
181 ->method( 'fetchRow' )
182 ->with( $this->anything() )
183 ->will( $this->onConsecutiveCalls(
184 [ 'Tables_in_' => 'view1' ],
185 [ 'Tables_in_' => 'view2' ],
186 [ 'Tables_in_' => 'myview' ],
192 * @covers DatabaseMysqlBase::listViews
194 function testListviews() {
195 $db = $this->getMockForViews();
197 // The first call populate an internal cache of views
198 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
200 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
204 $this->assertEquals( [ 'view1', 'view2' ],
205 $db->listViews( 'view' ) );
206 $this->assertEquals( [ 'myview' ],
207 $db->listViews( 'my' ) );
208 $this->assertEquals( [],
209 $db->listViews( 'UNUSED_PREFIX' ) );
210 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
211 $db->listViews( '' ) );
215 * @covers DatabaseMysqlBase::isView
216 * @dataProvider provideViewExistanceChecks
218 function testIsView( $isView, $viewName ) {
219 $db = $this->getMockForViews();
223 $this->assertTrue( $db->isView( $viewName ),
224 "$viewName should be considered a view" );
228 $this->assertFalse( $db->isView( $viewName ),
229 "$viewName has not been defined as a view" );
235 function provideViewExistanceChecks() {
237 // format: whether it is a view, view name
246 [ false, 'OH_MY_GOD' ], # they killed kenny!
251 * @dataProvider provideComparePositions
253 function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
255 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
257 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
258 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
259 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
260 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
261 } else { // channels don't match
262 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
264 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
265 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
269 function provideComparePositions() {
273 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
274 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
278 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
279 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
283 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
284 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
289 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
290 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
294 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
295 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
299 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
300 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
303 // MariaDB GTID style
305 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
306 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
310 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
311 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
315 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
316 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
323 * @dataProvider provideChannelPositions
325 function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
326 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
327 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
330 function provideChannelPositions() {
333 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
334 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
338 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
339 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
343 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
344 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
348 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
349 new MySQLMasterPos( 'trump2016.000976', '10000' ),
356 * @dataProvider provideLagAmounts
358 function testPtHeartbeat( $lag ) {
359 $db = $this->getMockBuilder( 'DatabaseMysql' )
360 ->disableOriginalConstructor()
362 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
365 $db->expects( $this->any() )
366 ->method( 'getLagDetectionMethod' )
367 ->will( $this->returnValue( 'pt-heartbeat' ) );
369 $db->expects( $this->any() )
370 ->method( 'getMasterServerInfo' )
371 ->will( $this->returnValue( [ '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->expects( $this->any() )
386 ->method( 'getHeartbeatData' )
387 ->with( [ 'server_id' => 172 ] )
388 ->will( $this->returnValue( [ $ptTimeISO, $now ] ) );
390 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
391 $lagEst = $db->getLag();
393 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
394 $this->assertLessThan( $lag +
.010, $lagEst, "Correct heatbeat lag" );
397 function provideLagAmounts() {