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();
70 * Tests just using the filter.
72 public function testUserNameApi() {
73 $view = Views::getView('test_user_name');
75 $view->initHandlers();
76 $view->filter['uid']->value = [$this->accounts[0]->id()];
78 $this->executeView($view);
79 $this->assertIdenticalResultset($view, [['uid' => $this->accounts[0]->id()]], $this->columnMap);
81 $this->assertEqual($view->filter['uid']->getValueOptions(), NULL);
85 * Tests using the user interface.
87 public function testAdminUserInterface() {
88 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
89 $this->drupalLogin($admin_user);
91 $path = 'admin/structure/views/nojs/handler/test_user_name/default/filter/uid';
92 $this->drupalGet($path);
94 // Pass in an invalid username, the validation should catch it.
95 $users = [$this->randomMachineName()];
96 $users = array_map('strtolower', $users);
98 'options[value]' => implode(', ', $users),
100 $this->drupalPostForm($path, $edit, t('Apply'));
101 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
103 // Pass in an invalid username and a valid username.
104 $random_name = $this->randomMachineName();
105 $users = [$random_name, $this->names[0]];
106 $users = array_map('strtolower', $users);
108 'options[value]' => implode(', ', $users),
110 $users = [$users[0]];
111 $this->drupalPostForm($path, $edit, t('Apply'));
112 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
114 // Pass in just valid usernames.
115 $users = $this->names;
116 $users = array_map('strtolower', $users);
118 'options[value]' => implode(', ', $users),
120 $this->drupalPostForm($path, $edit, t('Apply'));
121 $this->assertNoRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
125 * Tests exposed filters.
127 public function testExposedFilter() {
128 $path = 'test_user_name';
132 // Pass in an invalid username, the validation should catch it.
133 $users = [$this->randomMachineName()];
134 $users = array_map('strtolower', $users);
135 $options['query']['uid'] = implode(', ', $users);
136 $this->drupalGet($path, $options);
137 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
139 // Pass in an invalid target_id in for the entity_autocomplete value format.
140 // There should be no errors, but all results should be returned as the
141 // default value for the autocomplete will not match any users so should
143 $options['query']['uid'] = [['target_id' => 9999]];
144 $this->drupalGet($path, $options);
145 // The actual result should contain all of the user ids.
146 foreach ($this->accounts as $account) {
147 $this->assertRaw($account->id());
150 // Pass in an invalid username and a valid username.
151 $users = [$this->randomMachineName(), $this->names[0]];
152 $users = array_map('strtolower', $users);
153 $options['query']['uid'] = implode(', ', $users);
154 $users = [$users[0]];
156 $this->drupalGet($path, $options);
157 $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
159 // Pass in just valid usernames.
160 $users = $this->names;
161 $options['query']['uid'] = implode(', ', $users);
163 $this->drupalGet($path, $options);
164 $this->assertNoRaw('Unable to find user');
165 // The actual result should contain all of the user ids.
166 foreach ($this->accounts as $account) {
167 $this->assertRaw($account->id());
170 // Pass in just valid user IDs in the entity_autocomplete target_id format.
171 $options['query']['uid'] = array_map(function ($account) {
172 return ['target_id' => $account->id()];
175 $this->drupalGet($path, $options);
176 $this->assertNoRaw('Unable to find user');
177 // The actual result should contain all of the user ids.
178 foreach ($this->accounts as $account) {
179 $this->assertRaw($account->id());