diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAutocompleteTest.php b/core/modules/user/lib/Drupal/user/Tests/UserAutocompleteTest.php index d225150d691..5679b185d0f 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAutocompleteTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAutocompleteTest.php @@ -46,5 +46,13 @@ class UserAutocompleteTest extends WebTestBase { // Using first letter of the user's name, make sure the user's full name is in the results. $this->assertRaw($this->unprivileged_user->name, 'User name found in autocompletion results.'); + + // Test that anonymous username is in the result. + $anonymous_name = $this->randomString(); + config('user.settings')->set('anonymous', $anonymous_name)->save(); + $this->drupalGet('user/autocomplete', array('query' => array('q' => drupal_substr($anonymous_name, 0, 4), 'anonymous' => '1'))); + $this->assertRaw($anonymous_name, 'The anonymous name found in autocompletion results.'); + $this->drupalGet('user/autocomplete', array('query' => array('q' => drupal_substr($anonymous_name, 0, 4)))); + $this->assertNoRaw($anonymous_name, 'The anonymous name not found in autocompletion results without enabling anonymous username.'); } } diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc index c1b93496a8a..87408f10fec 100644 --- a/core/modules/user/user.pages.inc +++ b/core/modules/user/user.pages.inc @@ -12,11 +12,31 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; /** - * Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing users. + * Menu callback for user autocompletion. + * + * Like other autocomplete functions, this function inspects the 'q' query + * parameter for the string to use to search for suggestions. If the name used + * to indicate anonymous users (e.g. "Anonymous") is to be included as a + * possible suggestion, the 'anonymous' query parameter should be set + * additionally. For example, http://example.com/user/autocomplete?q=An might + * return "Andrew" and "Anne", while + * http://example.com/user/autocomplete?q=An&anonymous=1 will additionally + * return "Anonymous". + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + * A JSON response containing the autocomplete suggestions for existing users. */ function user_autocomplete() { $matches = array(); - if ($string = drupal_container()->get('request')->query->get('q')) { + $query = drupal_container()->get('request')->query; + if ($string = $query->get('q')) { + if ($query->get('anonymous')) { + $anonymous_name = config('user.settings')->get('anonymous'); + // Allow autocompletion for the anonymous user. + if (stripos($anonymous_name, $string) !== FALSE) { + $matches[$anonymous_name] = check_plain($anonymous_name); + } + } $result = db_select('users')->fields('users', array('name'))->condition('name', db_like($string) . '%', 'LIKE')->range(0, 10)->execute(); foreach ($result as $account) { $matches[$account->name] = check_plain($account->name);