rdbms: make getMasterPos() ignore GTIDs outside of gtid_domain_id
[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 /**
141 * @covers Wikimedia\Rdbms\MySQLMasterPos
142 */
143 public function testBinLogName() {
144 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
145
146 $this->assertEquals( "db1052", $pos->getLogName() );
147 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
148 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
149 }
150
151 /**
152 * @dataProvider provideComparePositions
153 * @covers Wikimedia\Rdbms\MySQLMasterPos
154 */
155 public function testHasReached(
156 MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match, $hetero
157 ) {
158 if ( $match ) {
159 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
160
161 if ( $hetero ) {
162 // Each position is has one channel higher than the other
163 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
164 } else {
165 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
166 }
167 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
168 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
169 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
170 } else { // channels don't match
171 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
172
173 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
174 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
175 }
176 }
177
178 public static function provideComparePositions() {
179 $now = microtime( true );
180
181 return [
182 // Binlog style
183 [
184 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
185 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
186 true,
187 false
188 ],
189 [
190 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
191 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
192 true,
193 false
194 ],
195 [
196 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
197 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
198 false,
199 false
200 ],
201 // MySQL GTID style
202 [
203 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
204 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
205 true,
206 false
207 ],
208 [
209 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
210 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
211 true,
212 false
213 ],
214 [
215 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
216 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
217 false,
218 false
219 ],
220 // MariaDB GTID style
221 [
222 new MySQLMasterPos( '255-11-23', $now ),
223 new MySQLMasterPos( '255-11-24', $now ),
224 true,
225 false
226 ],
227 [
228 new MySQLMasterPos( '255-11-99', $now ),
229 new MySQLMasterPos( '255-11-100', $now ),
230 true,
231 false
232 ],
233 [
234 new MySQLMasterPos( '255-11-999', $now ),
235 new MySQLMasterPos( '254-11-1000', $now ),
236 false,
237 false
238 ],
239 [
240 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
241 new MySQLMasterPos( '255-11-24', $now ),
242 true,
243 false
244 ],
245 [
246 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
247 new MySQLMasterPos( '255-11-1000', $now ),
248 true,
249 false
250 ],
251 [
252 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
253 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
254 true,
255 false
256 ],
257 [
258 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
259 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
260 true,
261 false
262 ],
263 [
264 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
265 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
266 true,
267 true
268 ],
269 [
270 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
271 new MySQLMasterPos( '254-11-1000', $now ),
272 false,
273 false
274 ],
275 ];
276 }
277
278 /**
279 * @dataProvider provideChannelPositions
280 * @covers Wikimedia\Rdbms\MySQLMasterPos
281 */
282 public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
283 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
284 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
285
286 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
287 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
288 }
289
290 public static function provideChannelPositions() {
291 $now = microtime( true );
292
293 return [
294 [
295 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
296 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
297 true
298 ],
299 [
300 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
301 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
302 true
303 ],
304 [
305 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
306 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
307 false
308 ],
309 [
310 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
311 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
312 false
313 ],
314 ];
315 }
316
317 /**
318 * @dataProvider provideCommonDomainGTIDs
319 * @covers Wikimedia\Rdbms\MySQLMasterPos
320 */
321 public function testCommonGtidDomains( MySQLMasterPos $pos, MySQLMasterPos $ref, $gtids ) {
322 $this->assertEquals( $gtids, MySQLMasterPos::getCommonDomainGTIDs( $pos, $ref ) );
323 }
324
325 public static function provideCommonDomainGTIDs() {
326 return [
327 [
328 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
329 new MySQLMasterPos( '255-11-1000', 1 ),
330 [ '255-13-99' ]
331 ],
332 [
333 new MySQLMasterPos(
334 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
335 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
336 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
337 1
338 ),
339 new MySQLMasterPos(
340 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
341 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
342 1
343 ),
344 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
345 ]
346 ];
347 }
348
349 /**
350 * @dataProvider provideLagAmounts
351 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
352 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
353 */
354 public function testPtHeartbeat( $lag ) {
355 $db = $this->getMockBuilder( DatabaseMysqli::class )
356 ->disableOriginalConstructor()
357 ->setMethods( [
358 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
359 ->getMock();
360
361 $db->method( 'getLagDetectionMethod' )
362 ->willReturn( 'pt-heartbeat' );
363
364 $db->method( 'getMasterServerInfo' )
365 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
366
367 // Fake the current time.
368 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
369 $now = (float)$nowSec + (float)$nowSecFrac;
370 // Fake the heartbeat time.
371 // Work arounds for weak DataTime microseconds support.
372 $ptTime = $now - $lag;
373 $ptSec = (int)$ptTime;
374 $ptSecFrac = ( $ptTime - $ptSec );
375 $ptDateTime = new DateTime( "@$ptSec" );
376 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
377 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
378
379 $db->method( 'getHeartbeatData' )
380 ->with( [ 'server_id' => 172 ] )
381 ->willReturn( [ $ptTimeISO, $now ] );
382
383 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
384 $lagEst = $db->getLag();
385
386 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
387 $this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
388 }
389
390 public static function provideLagAmounts() {
391 return [
392 [ 0 ],
393 [ 0.3 ],
394 [ 6.5 ],
395 [ 10.1 ],
396 [ 200.2 ],
397 [ 400.7 ],
398 [ 600.22 ],
399 [ 1000.77 ],
400 ];
401 }
402
403 /**
404 * @dataProvider provideGtidData
405 * @covers Wikimedia\Rdbms\MySQLMasterPos
406 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
407 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
408 */
409 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
410 $db = $this->getMockBuilder( DatabaseMysqli::class )
411 ->disableOriginalConstructor()
412 ->setMethods( [
413 'useGTIDs',
414 'getServerGTIDs',
415 'getServerRoleStatus',
416 'getServerId',
417 'getServerUUID'
418 ] )
419 ->getMock();
420
421 $db->method( 'useGTIDs' )->willReturn( true );
422 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
423 $db->method( 'getServerRoleStatus' )->willReturnCallback(
424 function ( $role ) use ( $rBLtable, $mBLtable ) {
425 if ( $role === 'SLAVE' ) {
426 return $rBLtable;
427 } elseif ( $role === 'MASTER' ) {
428 return $mBLtable;
429 }
430
431 return null;
432 }
433 );
434 $db->method( 'getServerId' )->willReturn( 1 );
435 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
436
437 if ( is_array( $rGTIDs ) ) {
438 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
439 } else {
440 $this->assertEquals( false, $db->getReplicaPos() );
441 }
442 if ( is_array( $mGTIDs ) ) {
443 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
444 } else {
445 $this->assertEquals( false, $db->getMasterPos() );
446 }
447 }
448
449 public static function provideGtidData() {
450 return [
451 // MariaDB
452 [
453 [
454 'gtid_domain_id' => 100,
455 'gtid_current_pos' => '100-13-77',
456 'gtid_binlog_pos' => '100-13-77',
457 'gtid_slave_pos' => null // master
458 ],
459 [],
460 [
461 'File' => 'host.1600',
462 'Pos' => '77'
463 ],
464 [ '100' => '100-13-77' ],
465 [ '100' => '100-13-77' ]
466 ],
467 [
468 [
469 'gtid_domain_id' => 100,
470 'gtid_current_pos' => '100-13-77',
471 'gtid_binlog_pos' => '100-13-77',
472 'gtid_slave_pos' => '100-13-77' // replica
473 ],
474 [
475 'Relay_Master_Log_File' => 'host.1600',
476 'Exec_Master_Log_Pos' => '77'
477 ],
478 [],
479 [ '100' => '100-13-77' ],
480 [ '100' => '100-13-77' ]
481 ],
482 [
483 [
484 'gtid_current_pos' => '100-13-77',
485 'gtid_binlog_pos' => '100-13-77',
486 'gtid_slave_pos' => '100-13-77' // replica
487 ],
488 [
489 'Relay_Master_Log_File' => 'host.1600',
490 'Exec_Master_Log_Pos' => '77'
491 ],
492 [],
493 [ '100' => '100-13-77' ],
494 [ '100' => '100-13-77' ]
495 ],
496 // MySQL
497 [
498 [
499 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
500 ],
501 [
502 'Relay_Master_Log_File' => 'host.1600',
503 'Exec_Master_Log_Pos' => '77'
504 ],
505 [], // only a replica
506 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
507 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
508 // replica/master use same var
509 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
510 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
511 ],
512 [
513 [
514 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
515 '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
516 ],
517 [
518 'Relay_Master_Log_File' => 'host.1600',
519 'Exec_Master_Log_Pos' => '77'
520 ],
521 [], // only a replica
522 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
523 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
524 // replica/master use same var
525 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
526 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
527 ],
528 [
529 [
530 'gtid_executed' => null // not enabled?
531 ],
532 [
533 'Relay_Master_Log_File' => 'host.1600',
534 'Exec_Master_Log_Pos' => '77'
535 ],
536 [], // only a replica
537 [], // binlog fallback
538 false
539 ],
540 [
541 [
542 'gtid_executed' => null // not enabled?
543 ],
544 [], // no replication
545 [], // no replication
546 false,
547 false
548 ]
549 ];
550 }
551
552 /**
553 * @covers Wikimedia\Rdbms\MySQLMasterPos
554 */
555 public function testSerialize() {
556 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
557 $roundtripPos = unserialize( serialize( $pos ) );
558
559 $this->assertEquals( $pos, $roundtripPos );
560
561 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
562 $roundtripPos = unserialize( serialize( $pos ) );
563
564 $this->assertEquals( $pos, $roundtripPos );
565 }
566
567 /**
568 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
569 * @dataProvider provideInsertSelectCases
570 */
571 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
572 $db = $this->getMockBuilder( DatabaseMysqli::class )
573 ->disableOriginalConstructor()
574 ->setMethods( [ 'getReplicationSafetyInfo' ] )
575 ->getMock();
576 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
577 $dbw = TestingAccessWrapper::newFromObject( $db );
578
579 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
580 }
581
582 public function provideInsertSelectCases() {
583 return [
584 [
585 [],
586 [],
587 [
588 'innodb_autoinc_lock_mode' => '2',
589 'binlog_format' => 'ROW',
590 ],
591 true
592 ],
593 [
594 [],
595 [ 'LIMIT' => 100 ],
596 [
597 'innodb_autoinc_lock_mode' => '2',
598 'binlog_format' => 'ROW',
599 ],
600 true
601 ],
602 [
603 [],
604 [ 'LIMIT' => 100 ],
605 [
606 'innodb_autoinc_lock_mode' => '0',
607 'binlog_format' => 'STATEMENT',
608 ],
609 false
610 ],
611 [
612 [],
613 [],
614 [
615 'innodb_autoinc_lock_mode' => '2',
616 'binlog_format' => 'STATEMENT',
617 ],
618 false
619 ],
620 [
621 [ 'NO_AUTO_COLUMNS' ],
622 [ 'LIMIT' => 100 ],
623 [
624 'innodb_autoinc_lock_mode' => '0',
625 'binlog_format' => 'STATEMENT',
626 ],
627 false
628 ],
629 [
630 [],
631 [],
632 [
633 'innodb_autoinc_lock_mode' => 0,
634 'binlog_format' => 'STATEMENT',
635 ],
636 true
637 ],
638 [
639 [ 'NO_AUTO_COLUMNS' ],
640 [],
641 [
642 'innodb_autoinc_lock_mode' => 2,
643 'binlog_format' => 'STATEMENT',
644 ],
645 true
646 ],
647 [
648 [ 'NO_AUTO_COLUMNS' ],
649 [],
650 [
651 'innodb_autoinc_lock_mode' => 0,
652 'binlog_format' => 'STATEMENT',
653 ],
654 true
655 ],
656
657 ];
658 }
659
660 /**
661 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
662 */
663 public function testBuildIntegerCast() {
664 $db = $this->getMockBuilder( DatabaseMysqli::class )
665 ->disableOriginalConstructor()
666 ->setMethods( null )
667 ->getMock();
668 $output = $db->buildIntegerCast( 'fieldName' );
669 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
670 }
671
672 /*
673 * @covers Wikimedia\Rdbms\Database::setIndexAliases
674 */
675 public function testIndexAliases() {
676 $db = $this->getMockBuilder( DatabaseMysqli::class )
677 ->disableOriginalConstructor()
678 ->setMethods( [ 'mysqlRealEscapeString' ] )
679 ->getMock();
680 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
681 function ( $s ) {
682 return str_replace( "'", "\\'", $s );
683 }
684 );
685
686 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
687 $sql = $db->selectSQLText(
688 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
689
690 $this->assertEquals(
691 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
692 $sql
693 );
694
695 $db->setIndexAliases( [] );
696 $sql = $db->selectSQLText(
697 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
698
699 $this->assertEquals(
700 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
701 $sql
702 );
703 }
704
705 /**
706 * @covers Wikimedia\Rdbms\Database::setTableAliases
707 */
708 public function testTableAliases() {
709 $db = $this->getMockBuilder( DatabaseMysqli::class )
710 ->disableOriginalConstructor()
711 ->setMethods( [ 'mysqlRealEscapeString' ] )
712 ->getMock();
713 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
714 function ( $s ) {
715 return str_replace( "'", "\\'", $s );
716 }
717 );
718
719 $db->setTableAliases( [
720 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
721 ] );
722 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
723
724 $this->assertEquals(
725 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
726 $sql
727 );
728
729 $db->setTableAliases( [] );
730 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
731
732 $this->assertEquals(
733 "SELECT field FROM `meow` WHERE a = 'x' ",
734 $sql
735 );
736 }
737 }