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