Pull merge.
[yaffs-website] / web / core / modules / user / tests / src / Functional / Views / HandlerFilterUserNameTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional\Views;
4
5 use Drupal\views\Views;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Tests\ViewTestData;
8
9 /**
10  * Tests the handler of the user: name filter.
11  *
12  * @group user
13  * @see Views\user\Plugin\views\filter\Name
14  */
15 class HandlerFilterUserNameTest extends ViewTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['views_ui', 'user_test_views'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_user_name'];
30
31   /**
32    * Accounts used by this test.
33    *
34    * @var array
35    */
36   protected $accounts = [];
37
38   /**
39    * Usernames of $accounts.
40    *
41    * @var array
42    */
43   protected $names = [];
44
45   /**
46    * Stores the column map for this testCase.
47    *
48    * @var array
49    */
50   public $columnMap = [
51     'uid' => 'uid',
52   ];
53
54   protected function setUp($import_test_views = TRUE) {
55     parent::setUp($import_test_views);
56
57     ViewTestData::createTestViews(get_class($this), ['user_test_views']);
58
59     $this->enableViewsTestModule();
60
61     $this->accounts = [];
62     $this->names = [];
63     for ($i = 0; $i < 3; $i++) {
64       $this->accounts[] = $account = $this->drupalCreateUser();
65       $this->names[] = $account->label();
66     }
67   }
68
69   /**
70    * Tests just using the filter.
71    */
72   public function testUserNameApi() {
73     $view = Views::getView('test_user_name');
74
75     $view->initHandlers();
76     $view->filter['uid']->value = [$this->accounts[0]->id()];
77
78     $this->executeView($view);
79     $this->assertIdenticalResultset($view, [['uid' => $this->accounts[0]->id()]], $this->columnMap);
80
81     $this->assertEqual($view->filter['uid']->getValueOptions(), NULL);
82   }
83
84   /**
85    * Tests using the user interface.
86    */
87   public function testAdminUserInterface() {
88     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
89     $this->drupalLogin($admin_user);
90
91     $path = 'admin/structure/views/nojs/handler/test_user_name/default/filter/uid';
92     $this->drupalGet($path);
93
94     // Pass in an invalid username, the validation should catch it.
95     $users = [$this->randomMachineName()];
96     $users = array_map('strtolower', $users);
97     $edit = [
98       'options[value]' => implode(', ', $users),
99     ];
100     $this->drupalPostForm($path, $edit, t('Apply'));
101     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
102
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);
107     $edit = [
108       'options[value]' => implode(', ', $users),
109     ];
110     $users = [$users[0]];
111     $this->drupalPostForm($path, $edit, t('Apply'));
112     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
113
114     // Pass in just valid usernames.
115     $users = $this->names;
116     $users = array_map('strtolower', $users);
117     $edit = [
118       'options[value]' => implode(', ', $users),
119     ];
120     $this->drupalPostForm($path, $edit, t('Apply'));
121     $this->assertNoRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
122   }
123
124   /**
125    * Tests exposed filters.
126    */
127   public function testExposedFilter() {
128     $path = 'test_user_name';
129
130     $options = [];
131
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)]));
138
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
142     // be empty.
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());
148     }
149
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]];
155
156     $this->drupalGet($path, $options);
157     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
158
159     // Pass in just valid usernames.
160     $users = $this->names;
161     $options['query']['uid'] = implode(', ', $users);
162
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());
168     }
169
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()];
173     }, $this->accounts);
174
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());
180     }
181   }
182
183 }