@@ -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