Updated to Drupal 8.5. Core Media not yet in use.
[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   /**
71    * Tests just using the filter.
72    */
73   public function testUserNameApi() {
74     $view = Views::getView('test_user_name');
75
76     $view->initHandlers();
77     $view->filter['uid']->value = [$this->accounts[0]->id()];
78
79     $this->executeView($view);
80     $this->assertIdenticalResultset($view, [['uid' => $this->accounts[0]->id()]], $this->columnMap);
81
82     $this->assertEqual($view->filter['uid']->getValueOptions(), NULL);
83   }
84
85   /**
86    * Tests using the user interface.
87    */
88   public function testAdminUserInterface() {
89     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
90     $this->drupalLogin($admin_user);
91
92     $path = 'admin/structure/views/nojs/handler/test_user_name/default/filter/uid';
93     $this->drupalGet($path);
94
95     // Pass in an invalid username, the validation should catch it.
96     $users = [$this->randomMachineName()];
97     $users = array_map('strtolower', $users);
98     $edit = [
99       'options[value]' => implode(', ', $users)
100     ];
101     $this->drupalPostForm($path, $edit, t('Apply'));
102     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
103
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);
108     $edit = [
109       'options[value]' => implode(', ', $users)
110     ];
111     $users = [$users[0]];
112     $this->drupalPostForm($path, $edit, t('Apply'));
113     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
114
115     // Pass in just valid usernames.
116     $users = $this->names;
117     $users = array_map('strtolower', $users);
118     $edit = [
119       'options[value]' => implode(', ', $users)
120     ];
121     $this->drupalPostForm($path, $edit, t('Apply'));
122     $this->assertNoRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
123   }
124
125   /**
126    * Tests exposed filters.
127    */
128   public function testExposedFilter() {
129     $path = 'test_user_name';
130
131     $options = [];
132
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)]));
139
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
143     // be empty.
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());
149     }
150
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]];
156
157     $this->drupalGet($path, $options);
158     $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
159
160     // Pass in just valid usernames.
161     $users = $this->names;
162     $options['query']['uid'] = implode(', ', $users);
163
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());
169     }
170
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()];
174     }, $this->accounts);
175
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());
181     }
182   }
183
184 }