3 namespace Drupal\user\Tests;
5 use Drupal\simpletest\WebTestBase;
12 class UserBlocksTest extends WebTestBase {
19 public static $modules = ['block', 'views'];
22 * A user with the 'administer blocks' permission.
24 * @var \Drupal\user\UserInterface
28 protected function setUp() {
31 $this->adminUser = $this->drupalCreateUser(['administer blocks']);
32 $this->drupalLogin($this->adminUser);
33 $this->drupalPlaceBlock('user_login_block');
34 $this->drupalLogout($this->adminUser);
38 * Tests that user login block is hidden from user/login.
40 public function testUserLoginBlockVisibility() {
41 // Array keyed list where key being the URL address and value being expected
42 // visibility as boolean type.
45 'user/login' => FALSE,
46 'user/register' => TRUE,
47 'user/password' => TRUE,
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');
56 $this->assertTrue(empty($elements), 'User login block in path "' . $path . '" should not be visible');
62 * Test the user login block.
64 public function testUserLoginBlock() {
65 // Create a user with some permission that anonymous users lack.
66 $user = $this->drupalCreateUser(['administer permissions']);
68 // Log in using the block.
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.');
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');
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');
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');
91 // Verify that form validation errors are displayed immediately for forms
92 // in blocks and not on subsequent page requests.
93 $this->drupalLogout();
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?'));
104 * Test the Who's Online block.
106 public function testWhosOnlineBlock() {
107 $block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');
110 $user1 = $this->drupalCreateUser(['access user profiles']);
111 $user2 = $this->drupalCreateUser([]);
112 $user3 = $this->drupalCreateUser([]);
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);
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);
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.');
136 * Updates the access column for a user.
138 private function updateAccess($uid, $access = REQUEST_TIME) {
139 db_update('users_field_data')
140 ->condition('uid', $uid)
141 ->fields(['access' => $access])