Skip to content

Commit 0175f13

Browse files
authored
Merge pull request #6088 from wp-cli/fix/esc_like-for-sqlite
2 parents 98e8ec8 + c1c90e8 commit 0175f13

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

php/utils.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,15 @@ function _proc_open_compat_win_env( $cmd, &$env ) {
16741674
* or real_escape next.
16751675
*/
16761676
function esc_like( $text ) {
1677+
global $wpdb;
1678+
1679+
// Check if the esc_like() method exists on the global $wpdb object.
1680+
// We need to do this because to ensure compatibilty layers like the
1681+
// SQLite integration plugin still work.
1682+
if ( null !== $wpdb && method_exists( $wpdb, 'esc_like' ) ) {
1683+
return $wpdb->esc_like( $text );
1684+
}
1685+
16771686
return addcslashes( $text, '_%\\' );
16781687
}
16791688

tests/UtilsTest.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -777,28 +777,52 @@ public static function dataProcOpenCompatWinEnv() {
777777
];
778778
}
779779

780+
public static function dataEscLike() {
781+
return [
782+
[ 'howdy%', 'howdy\\%' ],
783+
[ 'howdy_', 'howdy\\_' ],
784+
[ 'howdy\\', 'howdy\\\\' ],
785+
[ 'howdy\\howdy%howdy_', 'howdy\\\\howdy\\%howdy\\_' ],
786+
[ 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?' ],
787+
];
788+
}
789+
780790
/**
781-
* Copied from core "tests/phpunit/tests/db.php" (adapted to not use `$wpdb`).
791+
* @dataProvider dataEscLike
782792
*/
783-
public function test_esc_like() {
784-
$inputs = [
785-
'howdy%', // Single Percent.
786-
'howdy_', // Single Underscore.
787-
'howdy\\', // Single slash.
788-
'howdy\\howdy%howdy_', // The works.
789-
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', // Plain text.
790-
];
791-
$expected = [
792-
'howdy\\%',
793-
'howdy\\_',
794-
'howdy\\\\',
795-
'howdy\\\\howdy\\%howdy\\_',
796-
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
797-
];
793+
public function test_esc_like( $input, $expected ) {
794+
$this->assertEquals( $expected, Utils\esc_like( $input ) );
795+
}
798796

799-
foreach ( $inputs as $key => $input ) {
800-
$this->assertEquals( $expected[ $key ], Utils\esc_like( $input ) );
797+
/**
798+
* @dataProvider dataEscLike
799+
*/
800+
public function test_esc_like_with_wpdb( $input, $expected ) {
801+
global $wpdb;
802+
$wpdb = $this->getMockBuilder( 'stdClass' );
803+
804+
// Handle different PHPUnit versions (5.7 for PHP 5.6 vs newer versions)
805+
// This can be simplified if we drop support for PHP 5.6.
806+
if ( method_exists( $wpdb, 'addMethods' ) ) {
807+
$wpdb = $wpdb->addMethods( [ 'esc_like' ] );
808+
} else {
809+
$wpdb = $wpdb->setMethods( [ 'esc_like' ] );
801810
}
811+
812+
$wpdb = $wpdb->getMock();
813+
$wpdb->method( 'esc_like' )
814+
->willReturn( addcslashes( $input, '_%\\' ) );
815+
$this->assertEquals( $expected, Utils\esc_like( $input ) );
816+
$this->assertEquals( $expected, Utils\esc_like( $input ) );
817+
}
818+
819+
/**
820+
* @dataProvider dataEscLike
821+
*/
822+
public function test_esc_like_with_wpdb_being_null( $input, $expected ) {
823+
global $wpdb;
824+
$wpdb = null;
825+
$this->assertEquals( $expected, Utils\esc_like( $input ) );
802826
}
803827

804828
/**

0 commit comments

Comments
 (0)