You are here

function UserBlocksTest::testWhosOnlineBlock in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserBlocksTest.php \Drupal\user\Tests\UserBlocksTest::testWhosOnlineBlock()

Test the Who's Online block.

File

core/modules/user/src/Tests/UserBlocksTest.php, line 100
Contains \Drupal\user\Tests\UserBlocksTest.

Class

UserBlocksTest
Tests user blocks.

Namespace

Drupal\user\Tests

Code

function testWhosOnlineBlock() {
  $block = $this
    ->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');

  // Generate users.
  $user1 = $this
    ->drupalCreateUser(array(
    'access user profiles',
  ));
  $user2 = $this
    ->drupalCreateUser(array());
  $user3 = $this
    ->drupalCreateUser(array());

  // Update access of two users to be within the active timespan.
  $this
    ->updateAccess($user1
    ->id());
  $this
    ->updateAccess($user2
    ->id(), REQUEST_TIME + 1);

  // Insert an inactive user who should not be seen in the block, and ensure
  // that the admin user used in setUp() does not appear.
  $inactive_time = REQUEST_TIME - 15 * 60 - 1;
  $this
    ->updateAccess($user3
    ->id(), $inactive_time);
  $this
    ->updateAccess($this->adminUser
    ->id(), $inactive_time);

  // Test block output.
  \Drupal::currentUser()
    ->setAccount($user1);
  $content = entity_view($block, 'block');
  $this
    ->setRawContent(\Drupal::service('renderer')
    ->renderRoot($content));
  $this
    ->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
  $this
    ->assertText($user1
    ->getUsername(), 'Active user 1 found in online list.');
  $this
    ->assertText($user2
    ->getUsername(), 'Active user 2 found in online list.');
  $this
    ->assertNoText($user3
    ->getUsername(), 'Inactive user not found in online list.');
  $this
    ->assertTrue(strpos($this
    ->getRawContent(), $user1
    ->getUsername()) > strpos($this
    ->getRawContent(), $user2
    ->getUsername()), 'Online users are ordered correctly.');
}