diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php new file mode 100644 index 0000000000000..a73bba4879757 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php @@ -0,0 +1,151 @@ +user->create( + array( + 'user_login' => 'testuser', + 'user_email' => 'testuser@example.com', + 'first_name' => 'Test', + 'last_name' => 'User', + ) + ); + } + + /** + * Tests user autocomplete via AJAX. + * + * @ticket 65252 + * + * @dataProvider data_autocomplete_user + * + * @param array $request_params Request parameters. + * @param array $expected Expected results. + * @param bool $is_member Whether the user should be a member of the blog. + */ + public function test_autocomplete_user( array $request_params, array $expected, bool $is_member = true ): void { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'wp_ajax_autocomplete_user() is multisite only.' ); + } + + // Mock the user with necessary capabilities. + $this->_setRole( 'administrator' ); + wp_set_current_user( self::$user_id ); + grant_super_admin( self::$user_id ); + + if ( ! $is_member ) { + remove_user_from_blog( self::$user_id, get_current_blog_id() ); + } else { + add_user_to_blog( get_current_blog_id(), self::$user_id, 'subscriber' ); + } + + $_GET = array_merge( $_GET, $request_params ); + $_POST = array_merge( $_POST, $request_params ); + $_REQUEST = array_merge( $_REQUEST, $request_params ); + + try { + add_action( 'admin_init', 'wp_ajax_autocomplete_user', 1 ); + $this->_handleAjax( 'autocomplete_user' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = (string) $e->getMessage(); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response, 'Response should be a JSON array' ); + + foreach ( $expected as $index => $expected_item ) { + $this->assertArrayHasKey( $index, $response, "Response should have index $index" ); + $this->assertSame( $expected_item['label'], $response[ $index ]['label'], "Label mismatch at index $index" ); + $this->assertSame( $expected_item['value'], $response[ $index ]['value'], "Value mismatch at index $index" ); + } + } + + /** + * Data provider for test_autocomplete_user. + * + * @return array + */ + public function data_autocomplete_user(): array { + return array( + 'search by login' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'search', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser', + ), + ), + ), + 'add by login (exclude existing)' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'add', + ), + 'expected' => array(), + ), + 'add by login (include non-member)' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'add', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser', + ), + ), + 'is_member' => false, + ), + 'search by email' => array( + 'request_params' => array( + 'term' => 'testuser@example.com', + 'autocomplete_type' => 'search', + 'autocomplete_field' => 'user_email', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser@example.com', + ), + ), + ), + ); + } +}