Make WordPress Core


Ignore:
Timestamp:
01/10/2019 09:05:50 PM (6 years ago)
Author:
flixos90
Message:

General: Fix problematic string to array parsing.

WordPress has historically often used code like preg_split( '/[\s,]+/', $var ) to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element.

This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string.

Props david.binda, sstoqnov.
Fixes #43977.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/bookmark/getBookmarks.php

    r42343 r44546  
    8585        $this->assertTrue( $num_queries < $wpdb->num_queries );
    8686    }
     87
     88    public function test_exclude_param_gets_properly_parsed_as_list() {
     89        $bookmarks = self::factory()->bookmark->create_many( 3 );
     90
     91        $found = get_bookmarks(
     92            array(
     93                'exclude' => ',,',
     94            )
     95        );
     96        $found_ids = array();
     97        foreach( $found as $bookmark ) {
     98            $found_ids[] = $bookmark->link_id;
     99        }
     100
     101        // equal sets != same order.
     102        $this->assertEqualSets( $bookmarks, $found_ids );
     103    }
     104
     105    public function test_include_param_gets_properly_parsed_as_list() {
     106        $bookmarks = self::factory()->bookmark->create_many( 3 );
     107
     108        $found = get_bookmarks(
     109            array(
     110                'include' => ',,',
     111            )
     112        );
     113        $found_ids = array();
     114        foreach( $found as $bookmark ) {
     115            $found_ids[] = $bookmark->link_id;
     116        }
     117
     118        // equal sets != same order.
     119        $this->assertEqualSets( $bookmarks, $found_ids );
     120    }
     121
     122    public function test_category_param_propelry_gets_parsed_as_list() {
     123        $bookmarks = self::factory()->bookmark->create_many( 3 );
     124        $categories = self::factory()->term->create_many( 3, array(
     125            'taxonomy' => 'link_category',
     126        ) );
     127        $add = wp_add_object_terms( $bookmarks[0], $categories[0], 'link_category' );
     128        $add = wp_add_object_terms( $bookmarks[1], $categories[1], 'link_category' );
     129        $add = wp_add_object_terms( $bookmarks[2], $categories[2], 'link_category' );
     130
     131        $found = get_bookmarks(
     132            array(
     133                'category' => ',,',
     134            )
     135        );
     136        $found_ids = array();
     137        foreach( $found as $bookmark ) {
     138            $found_ids[] = $bookmark->link_id;
     139        }
     140
     141        // equal sets != same order.
     142        $this->assertEqualSets( $bookmarks, $found_ids );
     143    }
    87144}
Note: See TracChangeset for help on using the changeset viewer.