Backup of db before drupal security update
[yaffs-website] / web / core / modules / user / src / Tests / UserBlocksTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests user blocks.
9  *
10  * @group user
11  */
12 class UserBlocksTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['block', 'views'];
20
21   /**
22    * A user with the 'administer blocks' permission.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $adminUser;
27
28   protected function setUp() {
29     parent::setUp();
30
31     $this->adminUser = $this->drupalCreateUser(['administer blocks']);
32     $this->drupalLogin($this->adminUser);
33     $this->drupalPlaceBlock('user_login_block');
34     $this->drupalLogout($this->adminUser);
35   }
36
37   /**
38    * Tests that user login block is hidden from user/login.
39    */
40   public function testUserLoginBlockVisibility() {
41     // Array keyed list where key being the URL address and value being expected
42     // visibility as boolean type.
43     $paths = [
44       'node' => TRUE,
45       'user/login' => FALSE,
46       'user/register' => TRUE,
47       'user/password' => TRUE,
48     ];
49     foreach ($paths as $path => $expected_visibility) {
50       $this->drupalGet($path);
51       $elements = $this->xpath('//div[contains(@class,"block-user-login-block") and @role="form"]');
52       if ($expected_visibility) {
53         $this->assertTrue(!empty($elements), 'User login block in path "' . $path . '" should be visible');
54       }
55       else {
56         $this->assertTrue(empty($elements), 'User login block in path "' . $path . '" should not be visible');
57       }
58     }
59   }
60
61   /**
62    * Test the user login block.
63    */
64   public function testUserLoginBlock() {
65     // Create a user with some permission that anonymous users lack.
66     $user = $this->drupalCreateUser(['administer permissions']);
67
68     // Log in using the block.
69     $edit = [];
70     $edit['name'] = $user->getUsername();
71     $edit['pass'] = $user->pass_raw;
72     $this->drupalPostForm('admin/people/permissions', $edit, t('Log in'));
73     $this->assertNoText(t('User login'), 'Logged in.');
74
75     // Check that we are still on the same page.
76     $this->assertUrl(\Drupal::url('user.admin_permissions', [], ['absolute' => TRUE]), [], 'Still on the same page after login for access denied page');
77
78     // Now, log out and repeat with a non-403 page.
79     $this->drupalLogout();
80     $this->drupalPostForm('filter/tips', $edit, t('Log in'));
81     $this->assertNoText(t('User login'), 'Logged in.');
82     $this->assertPattern('!<title.*?' . t('Compose tips') . '.*?</title>!', 'Still on the same page after login for allowed page');
83
84     // Check that the user login block is not vulnerable to information
85     // disclosure to third party sites.
86     $this->drupalLogout();
87     $this->drupalPostForm('http://example.com/', $edit, t('Log in'), ['external' => FALSE]);
88     // Check that we remain on the site after login.
89     $this->assertUrl($user->url('canonical', ['absolute' => TRUE]), [], 'Redirected to user profile page after login from the frontpage');
90
91     // Verify that form validation errors are displayed immediately for forms
92     // in blocks and not on subsequent page requests.
93     $this->drupalLogout();
94     $edit = [];
95     $edit['name'] = 'foo';
96     $edit['pass'] = 'invalid password';
97     $this->drupalPostForm('filter/tips', $edit, t('Log in'));
98     $this->assertText(t('Unrecognized username or password. Forgot your password?'));
99     $this->drupalGet('filter/tips');
100     $this->assertNoText(t('Unrecognized username or password. Forgot your password?'));
101   }
102
103   /**
104    * Test the Who's Online block.
105    */
106   public function testWhosOnlineBlock() {
107     $block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');
108
109     // Generate users.
110     $user1 = $this->drupalCreateUser(['access user profiles']);
111     $user2 = $this->drupalCreateUser([]);
112     $user3 = $this->drupalCreateUser([]);
113
114     // Update access of two users to be within the active timespan.
115     $this->updateAccess($user1->id());
116     $this->updateAccess($user2->id(), REQUEST_TIME + 1);
117
118     // Insert an inactive user who should not be seen in the block, and ensure
119     // that the admin user used in setUp() does not appear.
120     $inactive_time = REQUEST_TIME - (15 * 60) - 1;
121     $this->updateAccess($user3->id(), $inactive_time);
122     $this->updateAccess($this->adminUser->id(), $inactive_time);
123
124     // Test block output.
125     \Drupal::currentUser()->setAccount($user1);
126     $content = entity_view($block, 'block');
127     $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
128     $this->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
129     $this->assertText($user1->getUsername(), 'Active user 1 found in online list.');
130     $this->assertText($user2->getUsername(), 'Active user 2 found in online list.');
131     $this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.');
132     $this->assertTrue(strpos($this->getRawContent(), $user1->getUsername()) > strpos($this->getRawContent(), $user2->getUsername()), 'Online users are ordered correctly.');
133   }
134
135   /**
136    * Updates the access column for a user.
137    */
138   private function updateAccess($uid, $access = REQUEST_TIME) {
139     db_update('users_field_data')
140       ->condition('uid', $uid)
141       ->fields(['access' => $access])
142       ->execute();
143   }
144
145 }