Make WordPress Core


Ignore:
Timestamp:
06/10/2014 12:29:35 AM (12 years ago)
Author:
wonderboymusic
Message:

LIKE escape sanity:

  • Deprecate like_escape()
  • Add a method to $wpdb, ->esc_like(), and add unit tests

$wpdb::esc_like() is not used yet. As such, many unit tests will throw Unexpected deprecated notice for like_escape. Subsequent commits will alleviate this.

Props miqrogroove.
See #10041.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/db.php

    r28635 r28711  
    110110
    111111        /**
     112         * @ticket 10041
     113         */
     114        function test_esc_like() {
     115                global $wpdb;
     116
     117                $inputs = array(
     118                        'howdy%', //Single Percent
     119                        'howdy_', //Single Underscore
     120                        'howdy\\', //Single slash
     121                        'howdy\\howdy%howdy_', //The works
     122                        'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', //Plain text
     123                );
     124                $expected = array(
     125                        'howdy\\%',
     126                        'howdy\\_',
     127                        'howdy\\\\',
     128                        'howdy\\\\howdy\\%howdy\\_',
     129                        'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
     130                );
     131
     132                foreach ($inputs as $key => $input) {
     133                        $this->assertEquals($expected[$key], $wpdb->esc_like($input));
     134                }
     135        }
     136
     137        /**
     138         * Test LIKE Queries
     139         *
     140         * Make sure $wpdb is fully compatible with esc_like() by testing the identity of various strings.
     141         * When escaped properly, a string literal is always LIKE itself (1)
     142         * and never LIKE any other string literal (0) no matter how crazy the SQL looks.
     143         *
     144         * @ticket 10041
     145         * @dataProvider data_like_query
     146         * @param $data string The haystack, raw.
     147         * @param $like string The like phrase, raw.
     148         * @param $result string The expected comparison result; '1' = true, '0' = false
     149         */
     150        function test_like_query( $data, $like, $result ) {
     151                global $wpdb;
     152                return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( "SELECT %s LIKE %s", $data, $wpdb->esc_like( $like ) ) ) );
     153        }
     154
     155        function data_like_query() {
     156                return array(
     157                        array(
     158                                'aaa',
     159                                'aaa',
     160                                '1',
     161                        ),
     162                        array(
     163                                'a\\aa', // SELECT 'a\\aa'  # This represents a\aa in both languages.
     164                                'a\\aa', // LIKE 'a\\\\aa'
     165                                '1',
     166                        ),
     167                        array(
     168                                'a%aa',
     169                                'a%aa',
     170                                '1',
     171                        ),
     172                        array(
     173                                'aaaa',
     174                                'a%aa',
     175                                '0',
     176                        ),
     177                        array(
     178                                'a\\%aa', // SELECT 'a\\%aa'
     179                                'a\\%aa', // LIKE 'a\\\\\\%aa' # The PHP literal would be "LIKE 'a\\\\\\\\\\\\%aa'".  This is why we need reliable escape functions!
     180                                '1',
     181                        ),
     182                        array(
     183                                'a%aa',
     184                                'a\\%aa',
     185                                '0',
     186                        ),
     187                        array(
     188                                'a\\%aa',
     189                                'a%aa',
     190                                '0',
     191                        ),
     192                        array(
     193                                'a_aa',
     194                                'a_aa',
     195                                '1',
     196                        ),
     197                        array(
     198                                'aaaa',
     199                                'a_aa',
     200                                '0',
     201                        ),
     202                        array(
     203                                'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
     204                                'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
     205                                '1',
     206                        ),
     207                );
     208        }
     209
     210        /**
    112211         * @ticket 18510
    113212         */
Note: See TracChangeset for help on using the changeset viewer.