3 namespace Drupal\Tests\user\Functional\Views;
5 use Drupal\views\Views;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Tests\ViewTestData;
10 * Tests the handler of the user: name filter.
13 * @see Views\user\Plugin\views\filter\Name
15 class HandlerFilterUserNameTest extends ViewTestBase {
22 public static $modules = ['views_ui', 'user_test_views'];
25 * Views used by this test.
29 public static $testViews = ['test_user_name'];
32 * Accounts used by this test.
36 protected $accounts = [];
39 * Usernames of $accounts.
43 protected $names = [];
46 * Stores the column map for this testCase.
54 protected function setUp($import_test_views = TRUE) {
55 parent::setUp($import_test_views);
57 ViewTestData::createTestViews(get_class($this), ['user_test_views']);
59 $this->enableViewsTestModule();
63 for ($i = 0; $i < 3; $i++) {
64 $this->accounts[] = $account = $this->drupalCreateUser();
65 $this->names[] = $account->label();
71 * Tests just using the filter.
73 public function testUserNameApi() {
74 $view = Views::getView('test_user_name');
76 $view->initHandlers();
77 $view->filter['uid']->value = [$this->accounts[0]->id()];
79 $this->executeView($view);
80 $this->assertIdenticalResultset($view, [['uid' => $this->accounts[0]->id()]], $this->columnMap);
82 $this->assertEqual($view->filter['uid']->getValueOptions(), NULL);
86 * Tests using the user interface.
88 public function testAdminUserInterface() {
89 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
90 $this->drupalLogin($admin_user);
92 $path = 'admin/structure/views/nojs/handler/test_user_name/default/filter/uid';
93 $this->drupalGet($path);
95 // Pass in an invalid username, the validation should catch it.
96 $users = [$this->randomMachineName()];
97 $users = array_map('strtolower', $users);
99 'options[value]' => implode(', ', $users)
101 $this->drupalPostForm($path, $edit, t('Apply'));
102 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
104 // Pass in an invalid username and a valid username.
105 $random_name = $this->randomMachineName();
106 $users = [$random_name, $this->names[0]];
107 $users = array_map('strtolower', $users);
109 'options[value]' => implode(', ', $users)
111 $users = [$users[0]];
112 $this->drupalPostForm($path, $edit, t('Apply'));
113 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
115 // Pass in just valid usernames.
116 $users = $this->names;
117 $users = array_map('strtolower', $users);
119 'options[value]' => implode(', ', $users)
121 $this->drupalPostForm($path, $edit, t('Apply'));
122 $this->assertNoRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
126 * Tests exposed filters.
128 public function testExposedFilter() {
129 $path = 'test_user_name';
133 // Pass in an invalid username, the validation should catch it.
134 $users = [$this->randomMachineName()];
135 $users = array_map('strtolower', $users);
136 $options['query']['uid'] = implode(', ', $users);
137 $this->drupalGet($path, $options);
138 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
140 // Pass in an invalid target_id in for the entity_autocomplete value format.
141 // There should be no errors, but all results should be returned as the
142 // default value for the autocomplete will not match any users so should
144 $options['query']['uid'] = [['target_id' => 9999]];
145 $this->drupalGet($path, $options);
146 // The actual result should contain all of the user ids.
147 foreach ($this->accounts as $account) {
148 $this->assertRaw($account->id());
151 // Pass in an invalid username and a valid username.
152 $users = [$this->randomMachineName(), $this->names[0]];
153 $users = array_map('strtolower', $users);
154 $options['query']['uid'] = implode(', ', $users);
155 $users = [$users[0]];
157 $this->drupalGet($path, $options);
158 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
160 // Pass in just valid usernames.
161 $users = $this->names;
162 $options['query']['uid'] = implode(', ', $users);
164 $this->drupalGet($path, $options);
165 $this->assertNoRaw('Unable to find user');
166 // The actual result should contain all of the user ids.
167 foreach ($this->accounts as $account) {
168 $this->assertRaw($account->id());
171 // Pass in just valid user IDs in the entity_autocomplete target_id format.
172 $options['query']['uid'] = array_map(function ($account) {
173 return ['target_id' => $account->id()];
176 $this->drupalGet($path, $options);
177 $this->assertNoRaw('Unable to find user');
178 // The actual result should contain all of the user ids.
179 foreach ($this->accounts as $account) {
180 $this->assertRaw($account->id());