Backup of db before drupal security update
[yaffs-website] / web / core / modules / user / src / Tests / UserAdminTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\user\RoleInterface;
7
8 /**
9  * Tests user administration page functionality.
10  *
11  * @group user
12  */
13 class UserAdminTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['taxonomy', 'views'];
21
22   /**
23    * Registers a user and deletes it.
24    */
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';
30     $user_a->save();
31     $user_b = $this->drupalCreateUser(['administer taxonomy']);
32     $user_b->name = 'User B';
33     $user_b->save();
34     $user_c = $this->drupalCreateUser(['administer taxonomy']);
35     $user_c->name = 'User C';
36     $user_c->save();
37
38     $user_storage = $this->container->get('entity.manager')->getStorage('user');
39
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
43     // by name.
44     $admin_user->name = 'Admin user';
45     $admin_user->save();
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');
52
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');
56
57     // Test exposed filter elements.
58     foreach (['user', 'role', 'permission', 'status'] as $field) {
59       $this->assertField("edit-$field", "$field exposed filter found.");
60     }
61     // Make sure the reduce duplicates element from the ManyToOneHelper is not
62     // displayed.
63     $this->assertNoField('edit-reduce-duplicates', 'Reduce duplicates form element not found in exposed filters.');
64
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.');
70
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.');
75
76     // Filter the users by permission 'administer taxonomy'.
77     $this->drupalGet('admin/people', ['query' => ['permission' => 'administer taxonomy']]);
78
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');
83
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)]]);
88
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');
93
94     // Test blocking of a user.
95     $account = $user_storage->load($user_c->id());
96     $this->assertTrue($account->isActive(), 'User C not blocked');
97     $edit = [];
98     $edit['action'] = 'user_block_user_action';
99     $edit['user_bulk_form[4]'] = TRUE;
100     $config
101       ->set('notify.status_blocked', TRUE)
102       ->save();
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']
107     ]);
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');
113
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');
119
120     // Test unblocking of a user from /admin/people page and sending of activation mail
121     $editunblock = [];
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']
128     ]);
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");
133
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");
147   }
148
149   /**
150    * Tests the alternate notification email address for user mails.
151    */
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();
159
160     // Test custom user registration approval email address(es).
161     $config = $this->config('user.settings');
162     // Allow users to register with admin approval.
163     $config
164       ->set('verify_mail', TRUE)
165       ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
166       ->save();
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';
171     $system
172       ->set('mail', $server_address)
173       ->set('mail_notification', $notify_address)
174       ->save();
175     // Register a new user account.
176     $edit = [];
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,
187     ]);
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,
196     ]);
197     $this->assertTrue(count($user_mail), 'New user mail to user is sent from configured Notification Email address');
198   }
199
200 }