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