build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase class.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
24 */
25
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;
32
33 /**
34 * Fake class around abstract class so we can call concrete methods.
35 */
36 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
37 // From Database
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()}" );
46 };
47 $this->currentDomain = DatabaseDomain::newUnspecified();
48 }
49
50 protected function closeConnection() {
51 }
52
53 protected function doQuery( $sql ) {
54 }
55
56 protected function fetchAffectedRowCount() {
57 }
58
59 // From DatabaseMysqli
60 protected function mysqlConnect( $realServer ) {
61 }
62
63 protected function mysqlSetCharset( $charset ) {
64 }
65
66 protected function mysqlFreeResult( $res ) {
67 }
68
69 protected function mysqlFetchObject( $res ) {
70 }
71
72 protected function mysqlFetchArray( $res ) {
73 }
74
75 protected function mysqlNumRows( $res ) {
76 }
77
78 protected function mysqlNumFields( $res ) {
79 }
80
81 protected function mysqlFieldName( $res, $n ) {
82 }
83
84 protected function mysqlFieldType( $res, $n ) {
85 }
86
87 protected function mysqlDataSeek( $res, $row ) {
88 }
89
90 protected function mysqlError( $conn = null ) {
91 }
92
93 protected function mysqlFetchField( $res, $n ) {
94 }
95
96 protected function mysqlRealEscapeString( $s ) {
97 }
98
99 function insertId() {
100 }
101
102 function lastErrno() {
103 }
104
105 function affectedRows() {
106 }
107
108 function getServerVersion() {
109 }
110 }
111
112 class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
113
114 use MediaWikiCoversValidator;
115
116 /**
117 * @dataProvider provideDiapers
118 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
119 */
120 public function testAddIdentifierQuotes( $expected, $in ) {
121 $db = new FakeDatabaseMysqlBase();
122 $quoted = $db->addIdentifierQuotes( $in );
123 $this->assertEquals( $expected, $quoted );
124 }
125
126 /**
127 * Feeds testAddIdentifierQuotes
128 *
129 * Named per T22281 convention.
130 */
131 public static function provideDiapers() {
132 return [
133 // Format: expected, input
134 [ '``', '' ],
135
136 // Yeah I really hate loosely typed PHP idiocies nowadays
137 [ '``', null ],
138
139 // Dear codereviewer, guess what addIdentifierQuotes()
140 // will return with thoses:
141 [ '``', false ],
142 [ '`1`', true ],
143
144 // We never know what could happen
145 [ '`0`', 0 ],
146 [ '`1`', 1 ],
147
148 // Whatchout! Should probably use something more meaningful
149 [ "`'`", "'" ], # single quote
150 [ '`"`', '"' ], # double quote
151 [ '````', '`' ], # backtick
152 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
153
154 // sneaky NUL bytes are lurking everywhere
155 [ '``', "\0" ],
156 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
157
158 // unicode chars
159 [
160 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
161 self::createUnicodeString( '\u0001a\uFFFFb' )
162 ],
163 [
164 self::createUnicodeString( '`\u0001\uFFFF`' ),
165 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
166 ],
167 [ '`☃`', '☃' ],
168 [ '`メインページ`', 'メインページ' ],
169 [ '`Басты_бет`', 'Басты_бет' ],
170
171 // Real world:
172 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
173 [ '`Backtick: ```', 'Backtick: `' ],
174 [ '`This is a test`', 'This is a test' ],
175 ];
176 }
177
178 private static function createUnicodeString( $str ) {
179 return json_decode( '"' . $str . '"' );
180 }
181
182 private function getMockForViews() {
183 $db = $this->getMockBuilder( DatabaseMysqli::class )
184 ->disableOriginalConstructor()
185 ->setMethods( [ 'fetchRow', 'query' ] )
186 ->getMock();
187
188 $db->method( 'query' )
189 ->with( $this->anything() )
190 ->willReturn( new FakeResultWrapper( [
191 (object)[ 'Tables_in_' => 'view1' ],
192 (object)[ 'Tables_in_' => 'view2' ],
193 (object)[ 'Tables_in_' => 'myview' ]
194 ] ) );
195
196 return $db;
197 }
198
199 /**
200 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
201 */
202 public function testListviews() {
203 $db = $this->getMockForViews();
204
205 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
206 $db->listViews() );
207
208 // Prefix filtering
209 $this->assertEquals( [ 'view1', 'view2' ],
210 $db->listViews( 'view' ) );
211 $this->assertEquals( [ 'myview' ],
212 $db->listViews( 'my' ) );
213 $this->assertEquals( [],
214 $db->listViews( 'UNUSED_PREFIX' ) );
215 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
216 $db->listViews( '' ) );
217 }
218
219 public function testBinLogName() {
220 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
221
222 $this->assertEquals( "db1052", $pos->binlog );
223 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
224 $this->assertEquals( [ 2424, 4643 ], $pos->pos );
225 }
226
227 /**
228 * @dataProvider provideComparePositions
229 * @covers Wikimedia\Rdbms\MySQLMasterPos
230 */
231 public function testHasReached(
232 MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match, $hetero
233 ) {
234 if ( $match ) {
235 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
236
237 if ( $hetero ) {
238 // Each position is has one channel higher than the other
239 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
240 } else {
241 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
242 }
243 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
244 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
245 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
246 } else { // channels don't match
247 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
248
249 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
250 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
251 }
252 }
253
254 public static function provideComparePositions() {
255 $now = microtime( true );
256
257 return [
258 // Binlog style
259 [
260 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
261 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
262 true,
263 false
264 ],
265 [
266 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
267 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
268 true,
269 false
270 ],
271 [
272 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
273 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
274 false,
275 false
276 ],
277 // MySQL GTID style
278 [
279 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
280 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
281 true,
282 false
283 ],
284 [
285 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
286 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
287 true,
288 false
289 ],
290 [
291 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
292 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
293 false,
294 false
295 ],
296 // MariaDB GTID style
297 [
298 new MySQLMasterPos( '255-11-23', $now ),
299 new MySQLMasterPos( '255-11-24', $now ),
300 true,
301 false
302 ],
303 [
304 new MySQLMasterPos( '255-11-99', $now ),
305 new MySQLMasterPos( '255-11-100', $now ),
306 true,
307 false
308 ],
309 [
310 new MySQLMasterPos( '255-11-999', $now ),
311 new MySQLMasterPos( '254-11-1000', $now ),
312 false,
313 false
314 ],
315 [
316 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
317 new MySQLMasterPos( '255-11-24', $now ),
318 true,
319 false
320 ],
321 [
322 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
323 new MySQLMasterPos( '255-11-1000', $now ),
324 true,
325 false
326 ],
327 [
328 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
329 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
330 true,
331 false
332 ],
333 [
334 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
335 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
336 true,
337 false
338 ],
339 [
340 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
341 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
342 true,
343 true
344 ],
345 [
346 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
347 new MySQLMasterPos( '254-11-1000', $now ),
348 false,
349 false
350 ],
351 ];
352 }
353
354 /**
355 * @dataProvider provideChannelPositions
356 * @covers Wikimedia\Rdbms\MySQLMasterPos
357 */
358 public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
359 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
360 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
361
362 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
363 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
364 }
365
366 public static function provideChannelPositions() {
367 $now = microtime( true );
368
369 return [
370 [
371 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
372 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
373 true
374 ],
375 [
376 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
377 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
378 true
379 ],
380 [
381 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
382 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
383 false
384 ],
385 [
386 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
387 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
388 false
389 ],
390 ];
391 }
392
393 /**
394 * @dataProvider provideLagAmounts
395 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
396 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
397 */
398 public function testPtHeartbeat( $lag ) {
399 $db = $this->getMockBuilder( DatabaseMysqli::class )
400 ->disableOriginalConstructor()
401 ->setMethods( [
402 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
403 ->getMock();
404
405 $db->method( 'getLagDetectionMethod' )
406 ->willReturn( 'pt-heartbeat' );
407
408 $db->method( 'getMasterServerInfo' )
409 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
410
411 // Fake the current time.
412 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
413 $now = (float)$nowSec + (float)$nowSecFrac;
414 // Fake the heartbeat time.
415 // Work arounds for weak DataTime microseconds support.
416 $ptTime = $now - $lag;
417 $ptSec = (int)$ptTime;
418 $ptSecFrac = ( $ptTime - $ptSec );
419 $ptDateTime = new DateTime( "@$ptSec" );
420 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
421 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
422
423 $db->method( 'getHeartbeatData' )
424 ->with( [ 'server_id' => 172 ] )
425 ->willReturn( [ $ptTimeISO, $now ] );
426
427 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
428 $lagEst = $db->getLag();
429
430 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
431 $this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
432 }
433
434 public static function provideLagAmounts() {
435 return [
436 [ 0 ],
437 [ 0.3 ],
438 [ 6.5 ],
439 [ 10.1 ],
440 [ 200.2 ],
441 [ 400.7 ],
442 [ 600.22 ],
443 [ 1000.77 ],
444 ];
445 }
446
447 /**
448 * @expectedException UnexpectedValueException
449 * @covers Wikimedia\Rdbms\Database::setFlag
450 */
451 public function testDBOIgnoreSet() {
452 $db = new FakeDatabaseMysqlBase();
453
454 $db->setFlag( Database::DBO_IGNORE );
455 }
456
457 /**
458 * @expectedException UnexpectedValueException
459 * @covers Wikimedia\Rdbms\Database::clearFlag
460 */
461 public function testDBOIgnoreClear() {
462 $db = new FakeDatabaseMysqlBase();
463
464 $db->clearFlag( Database::DBO_IGNORE );
465 }
466 }