diff --git a/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php b/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php index 9a1658ad84..1f73a670f1 100644 --- a/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php +++ b/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php @@ -19,6 +19,27 @@ class WhosOnlineBlockTest extends KernelTestBase { public static $modules = ['system', 'user', 'block', 'views']; /** + * The block being tested. + * + * @var \Drupal\block\Entity\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() { @@ -27,6 +48,24 @@ protected function setUp() { $this->installSchema('system', ['sequences']); $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'); } /** @@ -63,21 +102,11 @@ public function testWhosOnlineBlock() { // Test block output. \Drupal::currentUser()->setAccount($user1); - // Create a block with only required values. - $block = Block::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, - ]); - $block->save(); - $this->container->get('cache.render')->deleteAll(); - $render_controller = \Drupal::entityTypeManager()->getViewBuilder($block->getEntityTypeId()); - $content = $render_controller->view($block, 'block'); - $this->setRawContent($this->render($content)); + + // Test the rendering of a block. + $entity = Block::load('views_block__who_s_online_who_s_online_block'); + $output = entity_view($entity, 'block'); + $this->setRawContent($this->renderer->renderRoot($output)); $this->assertRaw('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.');