82e5df807c1e2d48f49950baa195787670f82899
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserAdminTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\RoleInterface;
8
9 /**
10  * Tests user administration page functionality.
11  *
12  * @group user
13  */
14 class UserAdminTest extends BrowserTestBase {
15
16   use AssertMailTrait {
17     getMails as drupalGetMails;
18   }
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['taxonomy', 'views'];
26
27   /**
28    * Registers a user and deletes it.
29    */
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';
35     $user_a->save();
36     $user_b = $this->drupalCreateUser(['administer taxonomy']);
37     $user_b->name = 'User B';
38     $user_b->save();
39     $user_c = $this->drupalCreateUser(['administer taxonomy']);
40     $user_c->name = 'User C';
41     $user_c->save();
42
43     $user_storage = $this->container->get('entity.manager')->getStorage('user');
44
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
48     // by name.
49     $admin_user->name = 'Admin user';
50     $admin_user->save();
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');
57
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');
61
62     // Test exposed filter elements.
63     foreach (['user', 'role', 'permission', 'status'] as $field) {
64       $this->assertField("edit-$field", "$field exposed filter found.");
65     }
66     // Make sure the reduce duplicates element from the ManyToOneHelper is not
67     // displayed.
68     $this->assertNoField('edit-reduce-duplicates', 'Reduce duplicates form element not found in exposed filters.');
69
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.');
75
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.');
80
81     // Filter the users by permission 'administer taxonomy'.
82     $this->drupalGet('admin/people', ['query' => ['permission' => 'administer taxonomy']]);
83
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');
88
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)]]);
93
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');
98
99     // Test blocking of a user.
100     $account = $user_storage->load($user_c->id());
101     $this->assertTrue($account->isActive(), 'User C not blocked');
102     $edit = [];
103     $edit['action'] = 'user_block_user_action';
104     $edit['user_bulk_form[4]'] = TRUE;
105     $config
106       ->set('notify.status_blocked', TRUE)
107       ->save();
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'],
112     ]);
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');
118
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');
124
125     // Test unblocking of a user from /admin/people page and sending of activation mail
126     $editunblock = [];
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'],
133     ]);
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");
138
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");
152   }
153
154   /**
155    * Tests the alternate notification email address for user mails.
156    */
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();
164
165     // Test custom user registration approval email address(es).
166     $config = $this->config('user.settings');
167     // Allow users to register with admin approval.
168     $config
169       ->set('verify_mail', TRUE)
170       ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
171       ->save();
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';
176     $system
177       ->set('mail', $server_address)
178       ->set('mail_notification', $notify_address)
179       ->save();
180     // Register a new user account.
181     $edit = [];
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,
192     ]);
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,
201     ]);
202     $this->assertTrue(count($user_mail), 'New user mail to user is sent from configured Notification Email address');
203   }
204
205 }