Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserSearchTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests the user search page and verifies that sensitive information is hidden
9  * from unauthorized users.
10  *
11  * @group user
12  */
13 class UserSearchTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['search'];
21
22   public function testUserSearch() {
23     // Verify that a user without 'administer users' permission cannot search
24     // for users by email address. Additionally, ensure that the username has a
25     // plus sign to ensure searching works with that.
26     $user1 = $this->drupalCreateUser(['access user profiles', 'search content'], "foo+bar");
27     $this->drupalLogin($user1);
28     $keys = $user1->getEmail();
29     $edit = ['keys' => $keys];
30     $this->drupalPostForm('search/user', $edit, t('Search'));
31     $this->assertText(t('Your search yielded no results.'), 'Search by email did not work for non-admin user');
32     $this->assertText('no results', 'Search by email gave no-match message');
33
34     // Verify that a non-matching query gives an appropriate message.
35     $keys = 'nomatch';
36     $edit = ['keys' => $keys];
37     $this->drupalPostForm('search/user', $edit, t('Search'));
38     $this->assertText('no results', 'Non-matching search gave appropriate message');
39
40     // Verify that a user with search permission can search for users by name.
41     $keys = $user1->getUsername();
42     $edit = ['keys' => $keys];
43     $this->drupalPostForm('search/user', $edit, t('Search'));
44     $this->assertLink($keys, 0, 'Search by username worked for non-admin user');
45
46     // Verify that searching by sub-string works too.
47     $subkey = substr($keys, 1, 5);
48     $edit = ['keys' => $subkey];
49     $this->drupalPostForm('search/user', $edit, t('Search'));
50     $this->assertLink($keys, 0, 'Search by username substring worked for non-admin user');
51
52     // Verify that wildcard search works.
53     $subkey = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
54     $edit = ['keys' => $subkey];
55     $this->drupalPostForm('search/user', $edit, t('Search'));
56     $this->assertLink($keys, 0, 'Search with wildcard worked for non-admin user');
57
58     // Verify that a user with 'administer users' permission can search by
59     // email.
60     $user2 = $this->drupalCreateUser(['administer users', 'access user profiles', 'search content']);
61     $this->drupalLogin($user2);
62     $keys = $user2->getEmail();
63     $edit = ['keys' => $keys];
64     $this->drupalPostForm('search/user', $edit, t('Search'));
65     $this->assertText($keys, 'Search by email works for administrative user');
66     $this->assertText($user2->getUsername(), 'Search by email resulted in username on page for administrative user');
67
68     // Verify that a substring works too for email.
69     $subkey = substr($keys, 1, 5);
70     $edit = ['keys' => $subkey];
71     $this->drupalPostForm('search/user', $edit, t('Search'));
72     $this->assertText($keys, 'Search by email substring works for administrative user');
73     $this->assertText($user2->getUsername(), 'Search by email substring resulted in username on page for administrative user');
74
75     // Verify that wildcard search works for email
76     $subkey = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
77     $edit = ['keys' => $subkey];
78     $this->drupalPostForm('search/user', $edit, t('Search'));
79     $this->assertText($user2->getUsername(), 'Search for email wildcard resulted in username on page for administrative user');
80
81     // Verify that if they search by user name, they see email address too.
82     $keys = $user1->getUsername();
83     $edit = ['keys' => $keys];
84     $this->drupalPostForm('search/user', $edit, t('Search'));
85     $this->assertText($keys, 'Search by username works for admin user');
86     $this->assertText($user1->getEmail(), 'Search by username for admin shows email address too');
87
88     // Create a blocked user.
89     $blocked_user = $this->drupalCreateUser();
90     $blocked_user->block();
91     $blocked_user->save();
92
93     // Verify that users with "administer users" permissions can see blocked
94     // accounts in search results.
95     $edit = ['keys' => $blocked_user->getUsername()];
96     $this->drupalPostForm('search/user', $edit, t('Search'));
97     $this->assertText($blocked_user->getUsername(), 'Blocked users are listed on the user search results for users with the "administer users" permission.');
98
99     // Verify that users without "administer users" permissions do not see
100     // blocked accounts in search results.
101     $this->drupalLogin($user1);
102     $edit = ['keys' => $blocked_user->getUsername()];
103     $this->drupalPostForm('search/user', $edit, t('Search'));
104     $this->assertText(t('Your search yielded no results.'), 'Blocked users are hidden from the user search results.');
105
106     // Create a user without search permission, and one without user page view
107     // permission. Verify that neither one can access the user search page.
108     $user3 = $this->drupalCreateUser(['search content']);
109     $this->drupalLogin($user3);
110     $this->drupalGet('search/user');
111     $this->assertResponse('403', 'User without user profile access cannot search');
112
113     $user4 = $this->drupalCreateUser(['access user profiles']);
114     $this->drupalLogin($user4);
115     $this->drupalGet('search/user');
116     $this->assertResponse('403', 'User without search permission cannot search');
117     $this->drupalLogout();
118   }
119
120 }