function UserBlocksTests::testWhosOnlineBlock

Test the Who's Online block.

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserBlocksTests.php, line 82
Definition of Drupal\user\Tests\UserBlocksTests.

Class

UserBlocksTests
Test user blocks.

Namespace

Drupal\user\Tests

Code

function testWhosOnlineBlock() {
  $block = $this
    ->drupalPlaceBlock('user_online_block');
  $config = $block
    ->get('settings');

  // Generate users.
  $user1 = $this
    ->drupalCreateUser(array());
  $user2 = $this
    ->drupalCreateUser(array());
  $user3 = $this
    ->drupalCreateUser(array());

  // Update access of two users to be within the active timespan.
  $this
    ->updateAccess($user1->uid);
  $this
    ->updateAccess($user2->uid, 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 - $config['seconds_online'] - 1;
  $this
    ->updateAccess($user3->uid, $inactive_time);
  $this
    ->updateAccess($this->adminUser->uid, $inactive_time);

  // Test block output.
  $content = entity_view($block, 'block');
  $this
    ->drupalSetContent(render($content));
  $this
    ->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
  $this
    ->assertText($user1->name, 'Active user 1 found in online list.');
  $this
    ->assertText($user2->name, 'Active user 2 found in online list.');
  $this
    ->assertNoText($user3->name, 'Inactive user not found in online list.');
  $this
    ->assertTrue(strpos($this
    ->drupalGetContent(), $user1->name) > strpos($this
    ->drupalGetContent(), $user2->name), 'Online users are ordered correctly.');
}