Make WordPress Core

Ticket #56350: 56350.tests.improved.diff

File 56350.tests.improved.diff, 2.6 KB (added by johnregan3, 3 years ago)

Improved testing.

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 2c9bf40f19..65d410d5ec 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    251251                        'search'         => 's',
    252252                        'slug'           => 'post_name__in',
    253253                        'status'         => 'post_status',
     254                        'exact_search'   => 'exact',
    254255                );
    255256
    256257                /*
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    28342835                        );
    28352836                }
    28362837
     2838                $query_params['exact_search'] = array(
     2839                        'description' => __( 'Use exact search instead of full search.' ),
     2840                        'type'        => 'boolean',
     2841                );
     2842
    28372843                $query_params['offset'] = array(
    28382844                        'description' => __( 'Offset the result set by a specific number of items.' ),
    28392845                        'type'        => 'integer',
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 6ff1edae3e..a65903f01d 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    738738                }
    739739        }
    740740
     741        /**
     742         * @ticket 56350
     743         */
     744        public function test_get_items_exact_search() {
     745
     746                $this->factory->post->create(
     747                        array(
     748                                'post_title'   => 'Rye',
     749                                'post_content' => 'This is a post about Rye Bread',
     750                                'post_status'  => 'publish',
     751                        )
     752                );
     753
     754                $this->factory->post->create(
     755                        array(
     756                                'post_title'   => 'Types of Bread',
     757                                'post_content' => 'Types of bread are White and Rye Bread',
     758                                'post_status'  => 'publish',
     759                        )
     760                );
     761
     762                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     763
     764                // General search.
     765                $request->set_param( 'search', 'Rye' );
     766                $response = rest_get_server()->dispatch( $request );
     767                $data     = $response->get_data();
     768                $this->assertCount( 2, $data );
     769
     770                // Exact search using same search param.
     771                $request->set_param( 'exact_search', true );
     772                $response = rest_get_server()->dispatch( $request );
     773                $data     = $response->get_data();
     774                $this->assertCount( 1, $data );
     775
     776                // Note that "exact_search" is still true.
     777                $request->set_param( 'search', 'Rye Bread' );
     778                $response = rest_get_server()->dispatch( $request );
     779                $data     = $response->get_data();
     780                $this->assertCount( 0, $data );
     781        }
     782
    741783        public function test_get_items_order_and_orderby() {
    742784                $this->factory->post->create(
    743785                        array(