Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / db / DatabaseMysqlBaseTest.php
blob81b75dea174b75d16940908806054650d9f79295
1 <?php
2 /**
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
20 * @file
21 * @author Antoine Musso
22 * @author Bryan Davis
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
28 /**
29 * Fake class around abstract class so we can call concrete methods.
31 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
32 // From Database
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 ) {
51 // From DatabaseMysql
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 function insertId() {
95 function lastErrno() {
98 function affectedRows() {
101 function getServerVersion() {
105 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
107 * @dataProvider provideDiapers
108 * @covers DatabaseMysqlBase::addIdentifierQuotes
110 public function testAddIdentifierQuotes( $expected, $in ) {
111 $db = new FakeDatabaseMysqlBase();
112 $quoted = $db->addIdentifierQuotes( $in );
113 $this->assertEquals( $expected, $quoted );
117 * Feeds testAddIdentifierQuotes
119 * Named per bug 20281 convention.
121 function provideDiapers() {
122 return [
123 // Format: expected, input
124 [ '``', '' ],
126 // Yeah I really hate loosely typed PHP idiocies nowadays
127 [ '``', null ],
129 // Dear codereviewer, guess what addIdentifierQuotes()
130 // will return with thoses:
131 [ '``', false ],
132 [ '`1`', true ],
134 // We never know what could happen
135 [ '`0`', 0 ],
136 [ '`1`', 1 ],
138 // Whatchout! Should probably use something more meaningful
139 [ "`'`", "'" ], # single quote
140 [ '`"`', '"' ], # double quote
141 [ '````', '`' ], # backtick
142 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
144 // sneaky NUL bytes are lurking everywhere
145 [ '``', "\0" ],
146 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
148 // unicode chars
150 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
151 self::createUnicodeString( '\u0001a\uFFFFb' )
154 self::createUnicodeString( '`\u0001\uFFFF`' ),
155 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
157 [ '`☃`', '☃' ],
158 [ '`メインページ`', 'メインページ' ],
159 [ '`Басты_бет`', 'Басты_бет' ],
161 // Real world:
162 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
163 [ '`Backtick: ```', 'Backtick: `' ],
164 [ '`This is a test`', 'This is a test' ],
168 private static function createUnicodeString( $str ) {
169 return json_decode( '"' . $str . '"' );
172 function getMockForViews() {
173 $db = $this->getMockBuilder( 'DatabaseMysqli' )
174 ->disableOriginalConstructor()
175 ->setMethods( [ 'fetchRow', 'query' ] )
176 ->getMock();
178 $db->method( 'query' )
179 ->with( $this->anything() )
180 ->willReturn( new FakeResultWrapper( [
181 (object)[ 'Tables_in_' => 'view1' ],
182 (object)[ 'Tables_in_' => 'view2' ],
183 (object)[ 'Tables_in_' => 'myview' ]
184 ] ) );
186 return $db;
189 * @covers DatabaseMysqlBase::listViews
191 function testListviews() {
192 $db = $this->getMockForViews();
194 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
195 $db->listViews() );
197 // Prefix filtering
198 $this->assertEquals( [ 'view1', 'view2' ],
199 $db->listViews( 'view' ) );
200 $this->assertEquals( [ 'myview' ],
201 $db->listViews( 'my' ) );
202 $this->assertEquals( [],
203 $db->listViews( 'UNUSED_PREFIX' ) );
204 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
205 $db->listViews( '' ) );
209 * @dataProvider provideComparePositions
211 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
212 if ( $match ) {
213 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
215 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
216 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
217 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
218 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
219 } else { // channels don't match
220 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
222 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
223 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
227 function provideComparePositions() {
228 return [
229 // Binlog style
231 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
232 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
233 true
236 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
237 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
238 true
241 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
242 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
243 false
245 // MySQL GTID style
247 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
248 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
249 true
252 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
253 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
254 true
257 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
258 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
259 false
261 // MariaDB GTID style
263 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
264 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
265 true
268 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
269 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
270 true
273 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
274 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
275 false
281 * @dataProvider provideChannelPositions
283 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
284 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
285 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
288 function provideChannelPositions() {
289 return [
291 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
292 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
293 true
296 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
297 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
298 true
301 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
302 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
303 false
306 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307 new MySQLMasterPos( 'trump2016.000976', '10000' ),
308 false
314 * @dataProvider provideLagAmounts
316 function testPtHeartbeat( $lag ) {
317 $db = $this->getMockBuilder( 'DatabaseMysqli' )
318 ->disableOriginalConstructor()
319 ->setMethods( [
320 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
321 ->getMock();
323 $db->method( 'getLagDetectionMethod' )
324 ->willReturn( 'pt-heartbeat' );
326 $db->method( 'getMasterServerInfo' )
327 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
329 // Fake the current time.
330 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
331 $now = (float)$nowSec + (float)$nowSecFrac;
332 // Fake the heartbeat time.
333 // Work arounds for weak DataTime microseconds support.
334 $ptTime = $now - $lag;
335 $ptSec = (int)$ptTime;
336 $ptSecFrac = ( $ptTime - $ptSec );
337 $ptDateTime = new DateTime( "@$ptSec" );
338 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
339 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
341 $db->method( 'getHeartbeatData' )
342 ->with( [ 'server_id' => 172 ] )
343 ->willReturn( [ $ptTimeISO, $now ] );
345 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
346 $lagEst = $db->getLag();
348 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
349 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
352 function provideLagAmounts() {
353 return [
354 [ 0 ],
355 [ 0.3 ],
356 [ 6.5 ],
357 [ 10.1 ],
358 [ 200.2 ],
359 [ 400.7 ],
360 [ 600.22 ],
361 [ 1000.77 ],