Merge "Hide TOC with CSS instead of JavaScript"
[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 use PHPUnit4And6Compat;
33
34 /**
35 * @dataProvider provideDiapers
36 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
37 */
38 public function testAddIdentifierQuotes( $expected, $in ) {
39 $db = $this->getMockBuilder( DatabaseMysqli::class )
40 ->disableOriginalConstructor()
41 ->setMethods( null )
42 ->getMock();
43
44 $quoted = $db->addIdentifierQuotes( $in );
45 $this->assertEquals( $expected, $quoted );
46 }
47
48 /**
49 * Feeds testAddIdentifierQuotes
50 *
51 * Named per T22281 convention.
52 */
53 public static function provideDiapers() {
54 return [
55 // Format: expected, input
56 [ '``', '' ],
57
58 // Yeah I really hate loosely typed PHP idiocies nowadays
59 [ '``', null ],
60
61 // Dear codereviewer, guess what addIdentifierQuotes()
62 // will return with thoses:
63 [ '``', false ],
64 [ '`1`', true ],
65
66 // We never know what could happen
67 [ '`0`', 0 ],
68 [ '`1`', 1 ],
69
70 // Whatchout! Should probably use something more meaningful
71 [ "`'`", "'" ], # single quote
72 [ '`"`', '"' ], # double quote
73 [ '````', '`' ], # backtick
74 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
75
76 // sneaky NUL bytes are lurking everywhere
77 [ '``', "\0" ],
78 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
79
80 // unicode chars
81 [
82 "`\u{0001}a\u{FFFF}b`",
83 "\u{0001}a\u{FFFF}b"
84 ],
85 [
86 "`\u{0001}\u{FFFF}`",
87 "\u{0001}\u{0000}\u{FFFF}\u{0000}"
88 ],
89 [ '`☃`', '☃' ],
90 [ '`メインページ`', 'メインページ' ],
91 [ '`Басты_бет`', 'Басты_бет' ],
92
93 // Real world:
94 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
95 [ '`Backtick: ```', 'Backtick: `' ],
96 [ '`This is a test`', 'This is a test' ],
97 ];
98 }
99
100 private function getMockForViews() {
101 $db = $this->getMockBuilder( DatabaseMysqli::class )
102 ->disableOriginalConstructor()
103 ->setMethods( [ 'fetchRow', 'query' ] )
104 ->getMock();
105
106 $db->method( 'query' )
107 ->with( $this->anything() )
108 ->willReturn( new FakeResultWrapper( [
109 (object)[ 'Tables_in_' => 'view1' ],
110 (object)[ 'Tables_in_' => 'view2' ],
111 (object)[ 'Tables_in_' => 'myview' ]
112 ] ) );
113
114 return $db;
115 }
116
117 /**
118 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
119 */
120 public function testListviews() {
121 $db = $this->getMockForViews();
122
123 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
124 $db->listViews() );
125
126 // Prefix filtering
127 $this->assertEquals( [ 'view1', 'view2' ],
128 $db->listViews( 'view' ) );
129 $this->assertEquals( [ 'myview' ],
130 $db->listViews( 'my' ) );
131 $this->assertEquals( [],
132 $db->listViews( 'UNUSED_PREFIX' ) );
133 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
134 $db->listViews( '' ) );
135 }
136
137 /**
138 * @covers Wikimedia\Rdbms\MySQLMasterPos
139 */
140 public function testBinLogName() {
141 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
142
143 $this->assertEquals( "db1052", $pos->getLogName() );
144 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
145 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
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:1-23', $now ),
201 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
202 true,
203 false
204 ],
205 [
206 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
207 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
208 true,
209 false
210 ],
211 [
212 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
213 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-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:1-5,' .
332 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
333 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
334 1
335 ),
336 new MySQLMasterPos(
337 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
338 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
339 1
340 ),
341 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-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 * @dataProvider provideGtidData
402 * @covers Wikimedia\Rdbms\MySQLMasterPos
403 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
404 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
405 */
406 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
407 $db = $this->getMockBuilder( DatabaseMysqli::class )
408 ->disableOriginalConstructor()
409 ->setMethods( [
410 'useGTIDs',
411 'getServerGTIDs',
412 'getServerRoleStatus',
413 'getServerId',
414 'getServerUUID'
415 ] )
416 ->getMock();
417
418 $db->method( 'useGTIDs' )->willReturn( true );
419 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
420 $db->method( 'getServerRoleStatus' )->willReturnCallback(
421 function ( $role ) use ( $rBLtable, $mBLtable ) {
422 if ( $role === 'SLAVE' ) {
423 return $rBLtable;
424 } elseif ( $role === 'MASTER' ) {
425 return $mBLtable;
426 }
427
428 return null;
429 }
430 );
431 $db->method( 'getServerId' )->willReturn( 1 );
432 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
433
434 if ( is_array( $rGTIDs ) ) {
435 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
436 } else {
437 $this->assertEquals( false, $db->getReplicaPos() );
438 }
439 if ( is_array( $mGTIDs ) ) {
440 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
441 } else {
442 $this->assertEquals( false, $db->getMasterPos() );
443 }
444 }
445
446 public static function provideGtidData() {
447 return [
448 // MariaDB
449 [
450 [
451 'gtid_domain_id' => 100,
452 'gtid_current_pos' => '100-13-77',
453 'gtid_binlog_pos' => '100-13-77',
454 'gtid_slave_pos' => null // master
455 ],
456 [
457 'Relay_Master_Log_File' => 'host.1600',
458 'Exec_Master_Log_Pos' => '77'
459 ],
460 [
461 'File' => 'host.1600',
462 'Position' => '77'
463 ],
464 [],
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 'gtid_binlog_pos' => null
532 ],
533 [
534 'Relay_Master_Log_File' => 'host.1600',
535 'Exec_Master_Log_Pos' => '77'
536 ],
537 [], // only a replica
538 [], // binlog fallback
539 false
540 ],
541 [
542 [
543 'gtid_executed' => null, // not enabled?
544 'gtid_binlog_pos' => null
545 ],
546 [], // no replication
547 [], // no replication
548 false,
549 false
550 ]
551 ];
552 }
553
554 /**
555 * @covers Wikimedia\Rdbms\MySQLMasterPos
556 */
557 public function testSerialize() {
558 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
559 $roundtripPos = unserialize( serialize( $pos ) );
560
561 $this->assertEquals( $pos, $roundtripPos );
562
563 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
564 $roundtripPos = unserialize( serialize( $pos ) );
565
566 $this->assertEquals( $pos, $roundtripPos );
567 }
568
569 /**
570 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
571 * @dataProvider provideInsertSelectCases
572 */
573 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
574 $db = $this->getMockBuilder( DatabaseMysqli::class )
575 ->disableOriginalConstructor()
576 ->setMethods( [ 'getReplicationSafetyInfo' ] )
577 ->getMock();
578 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
579 $dbw = TestingAccessWrapper::newFromObject( $db );
580
581 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
582 }
583
584 public function provideInsertSelectCases() {
585 return [
586 [
587 [],
588 [],
589 [
590 'innodb_autoinc_lock_mode' => '2',
591 'binlog_format' => 'ROW',
592 ],
593 true
594 ],
595 [
596 [],
597 [ 'LIMIT' => 100 ],
598 [
599 'innodb_autoinc_lock_mode' => '2',
600 'binlog_format' => 'ROW',
601 ],
602 true
603 ],
604 [
605 [],
606 [ 'LIMIT' => 100 ],
607 [
608 'innodb_autoinc_lock_mode' => '0',
609 'binlog_format' => 'STATEMENT',
610 ],
611 false
612 ],
613 [
614 [],
615 [],
616 [
617 'innodb_autoinc_lock_mode' => '2',
618 'binlog_format' => 'STATEMENT',
619 ],
620 false
621 ],
622 [
623 [ 'NO_AUTO_COLUMNS' ],
624 [ 'LIMIT' => 100 ],
625 [
626 'innodb_autoinc_lock_mode' => '0',
627 'binlog_format' => 'STATEMENT',
628 ],
629 false
630 ],
631 [
632 [],
633 [],
634 [
635 'innodb_autoinc_lock_mode' => 0,
636 'binlog_format' => 'STATEMENT',
637 ],
638 true
639 ],
640 [
641 [ 'NO_AUTO_COLUMNS' ],
642 [],
643 [
644 'innodb_autoinc_lock_mode' => 2,
645 'binlog_format' => 'STATEMENT',
646 ],
647 true
648 ],
649 [
650 [ 'NO_AUTO_COLUMNS' ],
651 [],
652 [
653 'innodb_autoinc_lock_mode' => 0,
654 'binlog_format' => 'STATEMENT',
655 ],
656 true
657 ],
658
659 ];
660 }
661
662 /**
663 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
664 */
665 public function testBuildIntegerCast() {
666 $db = $this->getMockBuilder( DatabaseMysqli::class )
667 ->disableOriginalConstructor()
668 ->setMethods( null )
669 ->getMock();
670 $output = $db->buildIntegerCast( 'fieldName' );
671 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
672 }
673
674 /*
675 * @covers Wikimedia\Rdbms\Database::setIndexAliases
676 */
677 public function testIndexAliases() {
678 $db = $this->getMockBuilder( DatabaseMysqli::class )
679 ->disableOriginalConstructor()
680 ->setMethods( [ 'mysqlRealEscapeString' ] )
681 ->getMock();
682 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
683 function ( $s ) {
684 return str_replace( "'", "\\'", $s );
685 }
686 );
687
688 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
689 $sql = $db->selectSQLText(
690 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
691
692 $this->assertEquals(
693 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
694 $sql
695 );
696
697 $db->setIndexAliases( [] );
698 $sql = $db->selectSQLText(
699 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
700
701 $this->assertEquals(
702 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
703 $sql
704 );
705 }
706
707 /**
708 * @covers Wikimedia\Rdbms\Database::setTableAliases
709 */
710 public function testTableAliases() {
711 $db = $this->getMockBuilder( DatabaseMysqli::class )
712 ->disableOriginalConstructor()
713 ->setMethods( [ 'mysqlRealEscapeString' ] )
714 ->getMock();
715 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
716 function ( $s ) {
717 return str_replace( "'", "\\'", $s );
718 }
719 );
720
721 $db->setTableAliases( [
722 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
723 ] );
724 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
725
726 $this->assertEquals(
727 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
728 $sql
729 );
730
731 $db->setTableAliases( [] );
732 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
733
734 $this->assertEquals(
735 "SELECT field FROM `meow` WHERE a = 'x' ",
736 $sql
737 );
738 }
739 }