Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exact search in REST API #3677

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ public function get_items( $request ) {
}
}

if (
isset( $registered['search_semantics'], $request['search_semantics'] )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't technically need the isset check since the rest request object will just return null in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I feel like it's okay keeping though for consistency. Maybe also a tiny bit safer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me.

&& 'exact' === $request['search_semantics']
) {
$args['exact'] = true;
}

$args = $this->prepare_tax_query( $args, $request );

// Force the post_type argument, since it's not a user input variable.
Expand Down Expand Up @@ -2886,6 +2893,12 @@ public function get_collection_params() {
);
}

$query_params['search_semantics'] = array(
'description' => __( 'How to interpret the search input.' ),
'type' => 'string',
'enum' => array( 'exact' ),
);

$query_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.' ),
'type' => 'integer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
),
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/rest-api/rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
),
Expand Down
59 changes: 59 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
'sticky',
Expand Down Expand Up @@ -765,6 +766,64 @@ public function test_get_items_status_without_permissions() {
}
}

/**
* @ticket 56350
*
* @dataProvider data_get_items_exact_search
*
* @param string $search_term The search term.
* @param bool $exact_search Whether the search is an exact or general search.
* @param int $expected The expected number of matching posts.
*/
public function test_get_items_exact_search( $search_term, $exact_search, $expected ) {
self::factory()->post->create(
array(
'post_title' => 'Rye',
'post_content' => 'This is a post about Rye Bread',
)
);

self::factory()->post->create(
array(
'post_title' => 'Types of Bread',
'post_content' => 'Types of bread are White and Rye Bread',
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request['search'] = $search_term;
if ( $exact_search ) {
$request['search_semantics'] = 'exact';
}
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected, $response->get_data() );
}

/**
* Data provider for test_get_items_exact_search().
*
* @return array[]
*/
public function data_get_items_exact_search() {
return array(
'general search, one exact match and one partial match' => array(
'search_term' => 'Rye',
'exact_search' => false,
'expected' => 2,
),
'exact search, one exact match and one partial match' => array(
'search_term' => 'Rye',
'exact_search' => true,
'expected' => 1,
),
'exact search, no match and one partial match' => array(
'search_term' => 'Rye Bread',
'exact_search' => true,
'expected' => 0,
),
);
}

public function test_get_items_order_and_orderby() {
self::factory()->post->create(
array(
Expand Down
64 changes: 64 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -1719,6 +1727,14 @@ mockedApiResponse.Schema = {
"type": "integer",
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -2820,6 +2836,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -3571,6 +3595,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -4382,6 +4414,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -6995,6 +7035,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -7812,6 +7860,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down Expand Up @@ -8017,6 +8073,14 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
Expand Down
Loading