Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\Tests\user\Functional;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Core\Test\AssertMailTrait;
|
Chris@14
|
6 use Drupal\Tests\BrowserTestBase;
|
Chris@14
|
7 use Drupal\user\RoleInterface;
|
Chris@14
|
8
|
Chris@14
|
9 /**
|
Chris@14
|
10 * Tests user administration page functionality.
|
Chris@14
|
11 *
|
Chris@14
|
12 * @group user
|
Chris@14
|
13 */
|
Chris@14
|
14 class UserAdminTest extends BrowserTestBase {
|
Chris@14
|
15
|
Chris@14
|
16 use AssertMailTrait {
|
Chris@14
|
17 getMails as drupalGetMails;
|
Chris@14
|
18 }
|
Chris@14
|
19
|
Chris@14
|
20 /**
|
Chris@14
|
21 * Modules to enable.
|
Chris@14
|
22 *
|
Chris@14
|
23 * @var array
|
Chris@14
|
24 */
|
Chris@14
|
25 public static $modules = ['taxonomy', 'views'];
|
Chris@14
|
26
|
Chris@14
|
27 /**
|
Chris@14
|
28 * Registers a user and deletes it.
|
Chris@14
|
29 */
|
Chris@14
|
30 public function testUserAdmin() {
|
Chris@14
|
31 $config = $this->config('user.settings');
|
Chris@14
|
32 $user_a = $this->drupalCreateUser();
|
Chris@14
|
33 $user_a->name = 'User A';
|
Chris@14
|
34 $user_a->mail = $this->randomMachineName() . '@example.com';
|
Chris@14
|
35 $user_a->save();
|
Chris@14
|
36 $user_b = $this->drupalCreateUser(['administer taxonomy']);
|
Chris@14
|
37 $user_b->name = 'User B';
|
Chris@14
|
38 $user_b->save();
|
Chris@14
|
39 $user_c = $this->drupalCreateUser(['administer taxonomy']);
|
Chris@14
|
40 $user_c->name = 'User C';
|
Chris@14
|
41 $user_c->save();
|
Chris@14
|
42
|
Chris@14
|
43 $user_storage = $this->container->get('entity.manager')->getStorage('user');
|
Chris@14
|
44
|
Chris@14
|
45 // Create admin user to delete registered user.
|
Chris@14
|
46 $admin_user = $this->drupalCreateUser(['administer users']);
|
Chris@14
|
47 // Use a predictable name so that we can reliably order the user admin page
|
Chris@14
|
48 // by name.
|
Chris@14
|
49 $admin_user->name = 'Admin user';
|
Chris@14
|
50 $admin_user->save();
|
Chris@14
|
51 $this->drupalLogin($admin_user);
|
Chris@14
|
52 $this->drupalGet('admin/people');
|
Chris@14
|
53 $this->assertText($user_a->getUsername(), 'Found user A on admin users page');
|
Chris@14
|
54 $this->assertText($user_b->getUsername(), 'Found user B on admin users page');
|
Chris@14
|
55 $this->assertText($user_c->getUsername(), 'Found user C on admin users page');
|
Chris@14
|
56 $this->assertText($admin_user->getUsername(), 'Found Admin user on admin users page');
|
Chris@14
|
57
|
Chris@14
|
58 // Test for existence of edit link in table.
|
Chris@14
|
59 $link = $user_a->link(t('Edit'), 'edit-form', ['query' => ['destination' => $user_a->url('collection')]]);
|
Chris@14
|
60 $this->assertRaw($link, 'Found user A edit link on admin users page');
|
Chris@14
|
61
|
Chris@14
|
62 // Test exposed filter elements.
|
Chris@14
|
63 foreach (['user', 'role', 'permission', 'status'] as $field) {
|
Chris@14
|
64 $this->assertField("edit-$field", "$field exposed filter found.");
|
Chris@14
|
65 }
|
Chris@14
|
66 // Make sure the reduce duplicates element from the ManyToOneHelper is not
|
Chris@14
|
67 // displayed.
|
Chris@14
|
68 $this->assertNoField('edit-reduce-duplicates', 'Reduce duplicates form element not found in exposed filters.');
|
Chris@14
|
69
|
Chris@14
|
70 // Filter the users by name/email.
|
Chris@14
|
71 $this->drupalGet('admin/people', ['query' => ['user' => $user_a->getUsername()]]);
|
Chris@14
|
72 $result = $this->xpath('//table/tbody/tr');
|
Chris@14
|
73 $this->assertEqual(1, count($result), 'Filter by username returned the right amount.');
|
Chris@14
|
74 $this->assertEqual($user_a->getUsername(), $result[0]->find('xpath', '/td[2]/span')->getText(), 'Filter by username returned the right user.');
|
Chris@14
|
75
|
Chris@14
|
76 $this->drupalGet('admin/people', ['query' => ['user' => $user_a->getEmail()]]);
|
Chris@14
|
77 $result = $this->xpath('//table/tbody/tr');
|
Chris@14
|
78 $this->assertEqual(1, count($result), 'Filter by username returned the right amount.');
|
Chris@14
|
79 $this->assertEqual($user_a->getUsername(), $result[0]->find('xpath', '/td[2]/span')->getText(), 'Filter by username returned the right user.');
|
Chris@14
|
80
|
Chris@14
|
81 // Filter the users by permission 'administer taxonomy'.
|
Chris@14
|
82 $this->drupalGet('admin/people', ['query' => ['permission' => 'administer taxonomy']]);
|
Chris@14
|
83
|
Chris@14
|
84 // Check if the correct users show up.
|
Chris@14
|
85 $this->assertNoText($user_a->getUsername(), 'User A not on filtered by perm admin users page');
|
Chris@14
|
86 $this->assertText($user_b->getUsername(), 'Found user B on filtered by perm admin users page');
|
Chris@14
|
87 $this->assertText($user_c->getUsername(), 'Found user C on filtered by perm admin users page');
|
Chris@14
|
88
|
Chris@14
|
89 // Filter the users by role. Grab the system-generated role name for User C.
|
Chris@14
|
90 $roles = $user_c->getRoles();
|
Chris@14
|
91 unset($roles[array_search(RoleInterface::AUTHENTICATED_ID, $roles)]);
|
Chris@14
|
92 $this->drupalGet('admin/people', ['query' => ['role' => reset($roles)]]);
|
Chris@14
|
93
|
Chris@14
|
94 // Check if the correct users show up when filtered by role.
|
Chris@14
|
95 $this->assertNoText($user_a->getUsername(), 'User A not on filtered by role on admin users page');
|
Chris@14
|
96 $this->assertNoText($user_b->getUsername(), 'User B not on filtered by role on admin users page');
|
Chris@14
|
97 $this->assertText($user_c->getUsername(), 'User C on filtered by role on admin users page');
|
Chris@14
|
98
|
Chris@14
|
99 // Test blocking of a user.
|
Chris@14
|
100 $account = $user_storage->load($user_c->id());
|
Chris@14
|
101 $this->assertTrue($account->isActive(), 'User C not blocked');
|
Chris@14
|
102 $edit = [];
|
Chris@14
|
103 $edit['action'] = 'user_block_user_action';
|
Chris@14
|
104 $edit['user_bulk_form[4]'] = TRUE;
|
Chris@14
|
105 $config
|
Chris@14
|
106 ->set('notify.status_blocked', TRUE)
|
Chris@14
|
107 ->save();
|
Chris@14
|
108 $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'), [
|
Chris@14
|
109 // Sort the table by username so that we know reliably which user will be
|
Chris@14
|
110 // targeted with the blocking action.
|
Chris@14
|
111 'query' => ['order' => 'name', 'sort' => 'asc']
|
Chris@14
|
112 ]);
|
Chris@14
|
113 $site_name = $this->config('system.site')->get('name');
|
Chris@14
|
114 $this->assertMailString('body', 'Your account on ' . $site_name . ' has been blocked.', 1, 'Blocked message found in the mail sent to user C.');
|
Chris@14
|
115 $user_storage->resetCache([$user_c->id()]);
|
Chris@14
|
116 $account = $user_storage->load($user_c->id());
|
Chris@14
|
117 $this->assertTrue($account->isBlocked(), 'User C blocked');
|
Chris@14
|
118
|
Chris@14
|
119 // Test filtering on admin page for blocked users
|
Chris@14
|
120 $this->drupalGet('admin/people', ['query' => ['status' => 2]]);
|
Chris@14
|
121 $this->assertNoText($user_a->getUsername(), 'User A not on filtered by status on admin users page');
|
Chris@14
|
122 $this->assertNoText($user_b->getUsername(), 'User B not on filtered by status on admin users page');
|
Chris@14
|
123 $this->assertText($user_c->getUsername(), 'User C on filtered by status on admin users page');
|
Chris@14
|
124
|
Chris@14
|
125 // Test unblocking of a user from /admin/people page and sending of activation mail
|
Chris@14
|
126 $editunblock = [];
|
Chris@14
|
127 $editunblock['action'] = 'user_unblock_user_action';
|
Chris@14
|
128 $editunblock['user_bulk_form[4]'] = TRUE;
|
Chris@14
|
129 $this->drupalPostForm('admin/people', $editunblock, t('Apply to selected items'), [
|
Chris@14
|
130 // Sort the table by username so that we know reliably which user will be
|
Chris@14
|
131 // targeted with the blocking action.
|
Chris@14
|
132 'query' => ['order' => 'name', 'sort' => 'asc']
|
Chris@14
|
133 ]);
|
Chris@14
|
134 $user_storage->resetCache([$user_c->id()]);
|
Chris@14
|
135 $account = $user_storage->load($user_c->id());
|
Chris@14
|
136 $this->assertTrue($account->isActive(), 'User C unblocked');
|
Chris@14
|
137 $this->assertMail("to", $account->getEmail(), "Activation mail sent to user C");
|
Chris@14
|
138
|
Chris@14
|
139 // Test blocking and unblocking another user from /user/[uid]/edit form and sending of activation mail
|
Chris@14
|
140 $user_d = $this->drupalCreateUser([]);
|
Chris@14
|
141 $user_storage->resetCache([$user_d->id()]);
|
Chris@14
|
142 $account1 = $user_storage->load($user_d->id());
|
Chris@14
|
143 $this->drupalPostForm('user/' . $account1->id() . '/edit', ['status' => 0], t('Save'));
|
Chris@14
|
144 $user_storage->resetCache([$user_d->id()]);
|
Chris@14
|
145 $account1 = $user_storage->load($user_d->id());
|
Chris@14
|
146 $this->assertTrue($account1->isBlocked(), 'User D blocked');
|
Chris@14
|
147 $this->drupalPostForm('user/' . $account1->id() . '/edit', ['status' => TRUE], t('Save'));
|
Chris@14
|
148 $user_storage->resetCache([$user_d->id()]);
|
Chris@14
|
149 $account1 = $user_storage->load($user_d->id());
|
Chris@14
|
150 $this->assertTrue($account1->isActive(), 'User D unblocked');
|
Chris@14
|
151 $this->assertMail("to", $account1->getEmail(), "Activation mail sent to user D");
|
Chris@14
|
152 }
|
Chris@14
|
153
|
Chris@14
|
154 /**
|
Chris@14
|
155 * Tests the alternate notification email address for user mails.
|
Chris@14
|
156 */
|
Chris@14
|
157 public function testNotificationEmailAddress() {
|
Chris@14
|
158 // Test that the Notification Email address field is on the config page.
|
Chris@14
|
159 $admin_user = $this->drupalCreateUser(['administer users', 'administer account settings']);
|
Chris@14
|
160 $this->drupalLogin($admin_user);
|
Chris@14
|
161 $this->drupalGet('admin/config/people/accounts');
|
Chris@14
|
162 $this->assertRaw('id="edit-mail-notification-address"', 'Notification Email address field exists');
|
Chris@14
|
163 $this->drupalLogout();
|
Chris@14
|
164
|
Chris@14
|
165 // Test custom user registration approval email address(es).
|
Chris@14
|
166 $config = $this->config('user.settings');
|
Chris@14
|
167 // Allow users to register with admin approval.
|
Chris@14
|
168 $config
|
Chris@14
|
169 ->set('verify_mail', TRUE)
|
Chris@14
|
170 ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
|
Chris@14
|
171 ->save();
|
Chris@14
|
172 // Set the site and notification email addresses.
|
Chris@14
|
173 $system = $this->config('system.site');
|
Chris@14
|
174 $server_address = $this->randomMachineName() . '@example.com';
|
Chris@14
|
175 $notify_address = $this->randomMachineName() . '@example.com';
|
Chris@14
|
176 $system
|
Chris@14
|
177 ->set('mail', $server_address)
|
Chris@14
|
178 ->set('mail_notification', $notify_address)
|
Chris@14
|
179 ->save();
|
Chris@14
|
180 // Register a new user account.
|
Chris@14
|
181 $edit = [];
|
Chris@14
|
182 $edit['name'] = $this->randomMachineName();
|
Chris@14
|
183 $edit['mail'] = $edit['name'] . '@example.com';
|
Chris@14
|
184 $this->drupalPostForm('user/register', $edit, t('Create new account'));
|
Chris@14
|
185 $subject = 'Account details for ' . $edit['name'] . ' at ' . $system->get('name') . ' (pending admin approval)';
|
Chris@14
|
186 // Ensure that admin notification mail is sent to the configured
|
Chris@14
|
187 // Notification Email address.
|
Chris@14
|
188 $admin_mail = $this->drupalGetMails([
|
Chris@14
|
189 'to' => $notify_address,
|
Chris@14
|
190 'from' => $server_address,
|
Chris@14
|
191 'subject' => $subject,
|
Chris@14
|
192 ]);
|
Chris@14
|
193 $this->assertTrue(count($admin_mail), 'New user mail to admin is sent to configured Notification Email address');
|
Chris@14
|
194 // Ensure that user notification mail is sent from the configured
|
Chris@14
|
195 // Notification Email address.
|
Chris@14
|
196 $user_mail = $this->drupalGetMails([
|
Chris@14
|
197 'to' => $edit['mail'],
|
Chris@14
|
198 'from' => $server_address,
|
Chris@14
|
199 'reply-to' => $notify_address,
|
Chris@14
|
200 'subject' => $subject,
|
Chris@14
|
201 ]);
|
Chris@14
|
202 $this->assertTrue(count($user_mail), 'New user mail to user is sent from configured Notification Email address');
|
Chris@14
|
203 }
|
Chris@14
|
204
|
Chris@14
|
205 }
|