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

REST API: Add support for search_columns argument to the REST API #7909

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Nov 28, 2024

Backports to change originally made in WordPress/gutenberg#67330

This adds support to search_columns to the user controller, mirroring a change that was done in the past for the post controller.

Trac ticket: https://core.trac.wordpress.org/ticket/62596

Todo

  • Add unit test.

@youknowriad youknowriad requested a review from Mamaduka November 28, 2024 08:52
@youknowriad youknowriad self-assigned this Nov 28, 2024
Copy link

github-actions bot commented Nov 28, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props youknowriad, ntsekouras, joemcgill, mamaduka.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

This looks good to me. It would be nice to add a PHPUnit test that confirms that users without list_users caps are not able to use the user_email search column.

@Mamaduka
Copy link
Member

Mamaduka commented Feb 4, 2025

@youknowriad, here's the patch.

diff --git tests/phpunit/tests/rest-api/rest-users-controller.php tests/phpunit/tests/rest-api/rest-users-controller.php
index 009200f401..fcaf7cdf5f 100644
--- tests/phpunit/tests/rest-api/rest-users-controller.php
+++ tests/phpunit/tests/rest-api/rest-users-controller.php
@@ -713,6 +713,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
 		$this->assertCount( 0, $response->get_data() );
 	}
 
+	/**
+	 * @ticket 62596
+	 */
 	public function test_get_items_search_columns() {
 		$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
 		$request->set_param( 'search', 'yololololo' );
@@ -746,6 +749,27 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
 		$this->assertCount( 1, $response->get_data() );
 	}
 
+	/**
+	 * @ticket 62596
+	 */
+	public function test_get_items_seach_columns_without_permission() {
+		self::factory()->user->create(
+			array(
+				'display_name' => 'Adam',
+				'user_email'   => 'yololololo@example.localhost',
+			)
+		);
+
+		// Test user without sufficient capabilities - 'list_users'.
+		wp_set_current_user( self::$editor );
+
+		$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
+		$request->set_param( 'search', 'yololololo' );
+		$request->set_param( 'search_columns', 'email' );
+		$response = rest_get_server()->dispatch( $request );
+		$this->assertCount( 0, $response->get_data() );
+	}
+
 	public function test_get_items_slug_query() {
 		wp_set_current_user( self::$user );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants