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