.../block/src/Tests/BlockViewBuilderTest.php | 57 +++------------------- .../tests/modules/block_test/block_test.module | 8 +-- 2 files changed, 7 insertions(+), 58 deletions(-) diff --git a/core/modules/block/src/Tests/BlockViewBuilderTest.php b/core/modules/block/src/Tests/BlockViewBuilderTest.php index 7e3c39a..1175480 100644 --- a/core/modules/block/src/Tests/BlockViewBuilderTest.php +++ b/core/modules/block/src/Tests/BlockViewBuilderTest.php @@ -190,57 +190,20 @@ protected function verifyRenderCacheHandling() { public function testBlockViewBuilderAlter() { // Establish baseline. $build = $this->getBlockRenderArray(); - $this->assertIdentical((string) $this->renderer->renderRoot($build), 'Llamas > unicorns!'); + $this->setRawContent((string)$this->renderer->renderRoot($build)); + $this->assertIdentical(trim((string)$this->cssSelect('div')[0]), 'Llamas > unicorns!'); - // Enable the block view alter hook that adds a suffix, for basic testing. + // Enable the block view alter hook that adds a foo=bar attribute. \Drupal::state()->set('block_test_view_alter_suffix', TRUE); Cache::invalidateTags($this->block->getCacheTagsToInvalidate()); $build = $this->getBlockRenderArray(); - $this->assertTrue(isset($build['#suffix']) && $build['#suffix'] === '
Goodbye!', 'A block with content is altered.'); - $this->assertIdentical((string) $this->renderer->renderRoot($build), 'Llamas > unicorns!
Goodbye!'); + $this->setRawContent((string)$this->renderer->renderRoot($build)); + $this->assertIdentical(trim((string)$this->cssSelect('[foo=bar]')[0]), 'Llamas > unicorns!'); \Drupal::state()->set('block_test_view_alter_suffix', FALSE); - // Force a request via GET so we can test the render cache. - $request = \Drupal::request(); - $request_method = $request->server->get('REQUEST_METHOD'); - $request->setMethod('GET'); - \Drupal::state()->set('block_test.content', NULL); Cache::invalidateTags($this->block->getCacheTagsToInvalidate()); - $default_keys = array('entity_view', 'block', 'test_block'); - $default_tags = array('block_view', 'config:block.block.test_block'); - - // Advanced: cached block, but an alter hook adds an additional cache key. - $alter_add_key = $this->randomMachineName(); - \Drupal::state()->set('block_test_view_alter_cache_key', $alter_add_key); - $cid = 'entity_view:block:test_block:' . $alter_add_key . ':' . implode(':', \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'])->getKeys()); - $expected_keys = array_merge($default_keys, array($alter_add_key)); - $build = $this->getBlockRenderArray(); - $this->assertIdentical($expected_keys, $build['#cache']['keys'], 'An altered cacheable block has the expected cache keys.'); - $this->assertIdentical((string) $this->renderer->renderRoot($build), ''); - $cache_entry = $this->container->get('cache.render')->get($cid); - $this->assertTrue($cache_entry, 'The block render element has been cached with the expected cache ID.'); - $expected_tags = array_merge($default_tags, ['rendered']); - sort($expected_tags); - $this->assertIdentical($cache_entry->tags, $expected_tags, 'The block render element has been cached with the expected cache tags.'); - $this->container->get('cache.render')->delete($cid); - - // Advanced: cached block, but an alter hook adds an additional cache tag. - $alter_add_tag = $this->randomMachineName(); - \Drupal::state()->set('block_test_view_alter_cache_tag', $alter_add_tag); - $expected_tags = Cache::mergeTags($default_tags, array($alter_add_tag)); - $build = $this->getBlockRenderArray(); - sort($build['#cache']['tags']); - $this->assertIdentical($expected_tags, $build['#cache']['tags'], 'An altered cacheable block has the expected cache tags.'); - $this->assertIdentical((string) $this->renderer->renderRoot($build), ''); - $cache_entry = $this->container->get('cache.render')->get($cid); - $this->assertTrue($cache_entry, 'The block render element has been cached with the expected cache ID.'); - $expected_tags = array_merge($default_tags, [$alter_add_tag, 'rendered']); - sort($expected_tags); - $this->assertIdentical($cache_entry->tags, $expected_tags, 'The block render element has been cached with the expected cache tags.'); - $this->container->get('cache.render')->delete($cid); - // Advanced: cached block, but an alter hook adds a #pre_render callback to // alter the eventual content. \Drupal::state()->set('block_test_view_alter_append_pre_render_prefix', TRUE); @@ -248,9 +211,6 @@ public function testBlockViewBuilderAlter() { $this->assertFalse(isset($build['#prefix']), 'The appended #pre_render callback has not yet run before rendering.'); $this->assertIdentical((string) $this->renderer->renderRoot($build), 'Hiya!
'); $this->assertTrue(isset($build['#prefix']) && $build['#prefix'] === 'Hiya!
', 'A cached block without content is altered.'); - - // Restore the previous request method. - $request->setMethod($request_method); } /** @@ -260,12 +220,7 @@ public function testBlockViewBuilderAlter() { * The render array. */ protected function getBlockRenderArray() { - $build = $this->container->get('entity.manager')->getViewBuilder('block')->view($this->block, 'block'); - - // Mock the build array to not require the theme registry. - unset($build['#theme']); - - return $build; + return $this->container->get('entity.manager')->getViewBuilder('block')->view($this->block, 'block'); } } diff --git a/core/modules/block/tests/modules/block_test/block_test.module b/core/modules/block/tests/modules/block_test/block_test.module index cec0b24..00f4ce9 100644 --- a/core/modules/block/tests/modules/block_test/block_test.module +++ b/core/modules/block/tests/modules/block_test/block_test.module @@ -22,13 +22,7 @@ function block_test_block_alter(&$block_info) { */ function block_test_block_view_test_cache_alter(array &$build, BlockPluginInterface $block) { if (\Drupal::state()->get('block_test_view_alter_suffix') !== NULL) { - $build['#suffix'] = '
Goodbye!'; - } - if (\Drupal::state()->get('block_test_view_alter_cache_key') !== NULL) { - $build['#cache']['keys'][] = \Drupal::state()->get('block_test_view_alter_cache_key'); - } - if (\Drupal::state()->get('block_test_view_alter_cache_tag') !== NULL) { - $build['#cache']['tags'][] = \Drupal::state()->get('block_test_view_alter_cache_tag'); + $build['#attributes']['foo'] = 'bar'; } if (\Drupal::state()->get('block_test_view_alter_append_pre_render_prefix') !== NULL) { $build['#pre_render'][] = 'block_test_pre_render_alter_content';