class WhoIsOnlineBlockTest

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Kernel/WhoIsOnlineBlockTest.php \Drupal\Tests\user\Kernel\WhoIsOnlineBlockTest

Tests the Who's Online Block.

@group user

Hierarchy

Expanded class hierarchy of WhoIsOnlineBlockTest

File

core/modules/user/tests/src/Kernel/WhoIsOnlineBlockTest.php, line 17

Namespace

Drupal\Tests\user\Kernel
View source
class WhoIsOnlineBlockTest extends KernelTestBase {
  use UserCreationTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'block',
    'views',
  ];
  
  /**
   * The block being tested.
   *
   * @var \Drupal\block\BlockInterface
   */
  protected $block;
  
  /**
   * The block storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $controller;
  
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->container
      ->get('theme_installer')
      ->install([
      'stark',
    ]);
    $this->installConfig([
      'system',
      'block',
      'views',
      'user',
    ]);
    $this->installEntitySchema('user');
    $this->controller = $this->container
      ->get('entity_type.manager')
      ->getStorage('block');
    // Create a block with only required values.
    $this->block = $this->controller
      ->create([
      'plugin' => 'views_block:who_s_online-who_s_online_block',
      'region' => 'sidebar_first',
      'id' => 'views_block__who_s_online_who_s_online_block',
      'theme' => \Drupal::configFactory()->get('system.theme')
        ->get('default'),
      'label' => "Who's online",
      'visibility' => [],
      'weight' => 0,
    ]);
    $this->block
      ->save();
    $this->container
      ->get('cache.render')
      ->deleteAll();
    $this->renderer = $this->container
      ->get('renderer');
  }
  
  /**
   * Tests the Who's Online block.
   */
  public function testWhoIsOnlineBlock() : void {
    // Generate users.
    $user1 = User::create([
      'name' => 'user1',
      'mail' => 'user1@example.com',
      'roles' => [
        $this->createRole([
          'access user profiles',
        ]),
      ],
    ]);
    $user1->activate();
    $requestTime = \Drupal::time()->getRequestTime();
    $user1->setLastAccessTime($requestTime);
    $user1->save();
    $user2 = User::create([
      'name' => 'user2',
      'mail' => 'user2@example.com',
    ]);
    $user2->activate();
    $user2->setLastAccessTime($requestTime + 1);
    $user2->save();
    $user3 = User::create([
      'name' => 'user3',
      'mail' => 'user2@example.com',
    ]);
    $user3->activate();
    // Insert an inactive user who should not be seen in the block.
    $inactive_time = $requestTime - 60 * 60;
    $user3->setLastAccessTime($inactive_time);
    $user3->save();
    // Test block output.
    \Drupal::currentUser()->setAccount($user1);
    // Test the rendering of a block.
    $entity = Block::load('views_block__who_s_online_who_s_online_block');
    $output = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())
      ->view($entity, 'block');
    $this->setRawContent($this->renderer
      ->renderRoot($output));
    $this->assertRaw('2 users', 'Correct number of online users (2 users).');
    $this->assertText($user1->getAccountName(), 'Active user 1 found in online list.');
    $this->assertText($user2->getAccountName(), 'Active user 2 found in online list.');
    $this->assertNoText($user3->getAccountName(), 'Inactive user not found in online list.');
    // Verify that online users are ordered correctly.
    $raw_content = (string) $this->getRawContent();
    $this->assertGreaterThan(strpos($raw_content, $user2->getAccountName()), strpos($raw_content, $user1->getAccountName()));
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
TestRequirementsTrait::checkModuleRequirements Deprecated private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements Deprecated protected function Check module requirements for the Drupal use case.
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.
WhoIsOnlineBlockTest::$block protected property The block being tested.
WhoIsOnlineBlockTest::$controller protected property The block storage.
WhoIsOnlineBlockTest::$modules protected static property Modules to install.
WhoIsOnlineBlockTest::$renderer protected property The renderer.
WhoIsOnlineBlockTest::setUp protected function
WhoIsOnlineBlockTest::testWhoIsOnlineBlock public function Tests the Who's Online block.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.