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
Changes from 6 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
@@ -251,6 +251,7 @@ public function get_items( $request ) {
'search' => 's',
'slug' => 'post_name__in',
'status' => 'post_status',
'exact_search' => 'exact',
Copy link
Contributor

Choose a reason for hiding this comment

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

These appear to be organised alphabetically by key, so the new exact_search key should be placed above exclude.

Copy link
Member Author

Choose a reason for hiding this comment

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

@costdev I have updated the placement for exact_search now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @mehulkaklotar!

);

/*
@@ -2836,6 +2837,11 @@ public function get_collection_params() {
);
}

$query_params['exact_search'] = array(
'description' => __( 'Use exact search instead of full search.' ),
'type' => 'boolean',
);

$query_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.' ),
'type' => 'integer',
Original file line number Diff line number Diff line change
@@ -216,6 +216,7 @@ public function test_registered_query_params() {
'author_exclude',
'before',
'context',
'exact_search',
'exclude',
'include',
'media_type',
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
@@ -73,6 +73,7 @@ public function test_registered_query_params() {
'author_exclude',
'before',
'context',
'exact_search',
'exclude',
'include',
'menu_order',
57 changes: 57 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
@@ -194,6 +194,7 @@ public function test_registered_query_params() {
'categories',
'categories_exclude',
'context',
'exact_search',
'exclude',
'include',
'modified_after',
@@ -749,6 +750,62 @@ 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->set_param( 'search', $search_term );
$request->set_param( 'exact_search', $exact_search );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
$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(
30 changes: 30 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
@@ -362,6 +362,11 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
@@ -1700,6 +1705,11 @@ mockedApiResponse.Schema = {
"type": "integer",
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
@@ -2782,6 +2792,11 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
@@ -3509,6 +3524,11 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
@@ -4306,6 +4326,11 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",
@@ -6457,6 +6482,11 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"required": false
},
"offset": {
"description": "Offset the result set by a specific number of items.",
"type": "integer",