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