function HandlerFilterUserNameTest::testExposedFilter

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Functional/Views/HandlerFilterUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFilterUserNameTest::testExposedFilter()
  2. 10 core/modules/user/tests/src/Functional/Views/HandlerFilterUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFilterUserNameTest::testExposedFilter()
  3. 8.9.x core/modules/user/tests/src/Functional/Views/HandlerFilterUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFilterUserNameTest::testExposedFilter()

Tests exposed filters.

File

core/modules/user/tests/src/Functional/Views/HandlerFilterUserNameTest.php, line 138

Class

HandlerFilterUserNameTest
Tests the handler of the user: name filter.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testExposedFilter() {
  $path = 'test_user_name';
  $options = [];
  // Pass in an invalid username, the validation should catch it.
  $users = [
    $this->randomMachineName(),
  ];
  $users = array_map('strtolower', $users);
  $options['query']['uid'] = implode(', ', $users);
  $this->drupalGet($path, $options);
  $this->assertSession()
    ->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
  // Pass in an invalid target_id in for the entity_autocomplete value format.
  // There should be no errors, but all results should be returned as the
  // default value for the autocomplete will not match any users so should
  // be empty.
  $options['query']['uid'] = [
    [
      'target_id' => 9999,
    ],
  ];
  $this->drupalGet($path, $options);
  // The actual result should contain all of the user ids.
  foreach ($this->accounts as $account) {
    $this->assertSession()
      ->pageTextContains($account->id());
  }
  // Pass in an invalid username and a valid username.
  $users = [
    $this->randomMachineName(),
    $this->names[0],
  ];
  $users = array_map('strtolower', $users);
  $options['query']['uid'] = implode(', ', $users);
  $users = [
    $users[0],
  ];
  $this->drupalGet($path, $options);
  $this->assertSession()
    ->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
  // Pass in just valid usernames.
  $users = $this->names;
  $options['query']['uid'] = implode(', ', $users);
  $this->drupalGet($path, $options);
  $this->assertSession()
    ->pageTextNotContains('Unable to find user');
  // The actual result should contain all of the user ids.
  foreach ($this->accounts as $account) {
    $this->assertSession()
      ->pageTextContains($account->id());
  }
  // Pass in just valid user IDs in the entity_autocomplete target_id format.
  $options['query']['uid'] = array_map(function ($account) {
    return [
      'target_id' => $account->id(),
    ];
  }, $this->accounts);
  $this->drupalGet($path, $options);
  $this->assertSession()
    ->pageTextNotContains('Unable to find user');
  // The actual result should contain all of the user ids.
  foreach ($this->accounts as $account) {
    $this->assertSession()
      ->pageTextContains($account->id());
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.