Loading core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php +6 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Core\Render\Plugin\DisplayVariant; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Display\Attribute\PageDisplayVariant; use Drupal\Core\Display\PageVariantInterface; use Drupal\Core\Display\VariantBase; Loading Loading @@ -65,6 +66,11 @@ public function build() { 'main_content' => ['#weight' => -800] + $this->mainContent, ], ]; $cache = new CacheableMetadata(); $cache->addCacheableDependency($this); $cache->applyTo($build); return $build; } Loading core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php +1 −0 Original line number Diff line number Diff line Loading @@ -197,6 +197,7 @@ public function build() { foreach ($cacheable_metadata_list as $cacheable_metadata) { $merged_cacheable_metadata = $merged_cacheable_metadata->merge($cacheable_metadata); } $merged_cacheable_metadata->addCacheableDependency($this); $merged_cacheable_metadata->applyTo($build); return $build; Loading core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php +34 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,8 @@ use Drupal\block\Plugin\DisplayVariant\BlockPageVariant; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Render\PageDisplayVariantSelectionEvent; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; Loading Loading @@ -264,4 +266,36 @@ public function testBuildWithoutMainContent(): void { $this->assertSame($expected, $display_variant->build()); } /** * Tests that cache metadata in the plugin are present in the build. * * @legacy-covers ::build */ public function testCacheMetadataFromPlugin(): void { $display_variant = $this->setUpDisplayVariant(); $this->blockRepository->expects($this->once()) ->method('getVisibleBlocksPerRegion') ->willReturn([]); $route_match = $this->createMock(RouteMatchInterface::class); $event = new PageDisplayVariantSelectionEvent($display_variant->getPluginId(), $route_match); $event->addCacheTags(['my_tag']); $event->addCacheContexts(['my_context']); $event->mergeCacheMaxAge(50); $display_variant->addCacheableDependency($event); $expectedCache = [ 'tags' => [ 'config:block_list', 'my_tag', ], 'contexts' => [ 'my_context', ], 'max-age' => 50, ]; $this->assertSame($expectedCache, $display_variant->build()['#cache']); } } core/tests/Drupal/Tests/Core/Render/Plugin/DisplayVariant/SimplePageVariantTest.php 0 → 100644 +123 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\Core\Render\Plugin\DisplayVariant; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Render\PageDisplayVariantSelectionEvent; use Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant. */ #[CoversClass(SimplePageVariant::class)] #[Group('Render')] class SimplePageVariantTest extends UnitTestCase { /** * Sets up a display variant plugin for testing. * * @param array $configuration * An array of plugin configuration. * @param array $definition * The plugin definition array. * * @return \Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant * A test display variant plugin. */ public function setUpDisplayVariant($configuration = [], $definition = []) { $container = new Container(); $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') ->disableOriginalConstructor() ->onlyMethods(['assertValidTokens']) ->getMock(); $container->set('cache_contexts_manager', $cache_context_manager); $cache_context_manager->expects($this->any()) ->method('assertValidTokens') ->willReturn(TRUE); \Drupal::setContainer($container); $plugin = new SimplePageVariant($configuration, 'test', $definition); $plugin->setTitle('Test'); $plugin->setMainContent([ '#markup' => 'Test content', ]); return $plugin; } /** * Tests the build method. * * @legacy-covers ::build */ public function testBuild(): void { $title = $this->randomString(); $content = $this->randomString(); $display_variant = $this->setUpDisplayVariant(); $display_variant->setTitle($title); $display_variant->setMainContent([ '#markup' => $content, ]); $expected = [ 'content' => [ 'messages' => [ '#type' => 'status_messages', '#weight' => -1000, '#include_fallback' => TRUE, ], 'page_title' => [ '#type' => 'page_title', '#title' => $title, '#weight' => -900, ], 'main_content' => [ '#weight' => -800, '#markup' => $content, ], ], '#cache' => [ 'contexts' => [], 'tags' => [], 'max-age' => -1, ], ]; $this->assertSame($expected, $display_variant->build()); } /** * Tests that cache metadata in the plugin are present in the build. * * @legacy-covers ::build */ public function testCacheMetadataFromPlugin(): void { $display_variant = $this->setUpDisplayVariant(); $route_match = $this->createMock(RouteMatchInterface::class); $event = new PageDisplayVariantSelectionEvent($display_variant->getPluginId(), $route_match); $event->addCacheTags(['my_tag']); $event->addCacheContexts(['my_context']); $event->mergeCacheMaxAge(50); $display_variant->addCacheableDependency($event); $expectedCache = [ 'contexts' => [ 'my_context', ], 'tags' => [ 'my_tag', ], 'max-age' => 50, ]; $this->assertSame($expectedCache, $display_variant->build()['#cache']); } } Loading
core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php +6 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Core\Render\Plugin\DisplayVariant; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Display\Attribute\PageDisplayVariant; use Drupal\Core\Display\PageVariantInterface; use Drupal\Core\Display\VariantBase; Loading Loading @@ -65,6 +66,11 @@ public function build() { 'main_content' => ['#weight' => -800] + $this->mainContent, ], ]; $cache = new CacheableMetadata(); $cache->addCacheableDependency($this); $cache->applyTo($build); return $build; } Loading
core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php +1 −0 Original line number Diff line number Diff line Loading @@ -197,6 +197,7 @@ public function build() { foreach ($cacheable_metadata_list as $cacheable_metadata) { $merged_cacheable_metadata = $merged_cacheable_metadata->merge($cacheable_metadata); } $merged_cacheable_metadata->addCacheableDependency($this); $merged_cacheable_metadata->applyTo($build); return $build; Loading
core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php +34 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,8 @@ use Drupal\block\Plugin\DisplayVariant\BlockPageVariant; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Render\PageDisplayVariantSelectionEvent; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; Loading Loading @@ -264,4 +266,36 @@ public function testBuildWithoutMainContent(): void { $this->assertSame($expected, $display_variant->build()); } /** * Tests that cache metadata in the plugin are present in the build. * * @legacy-covers ::build */ public function testCacheMetadataFromPlugin(): void { $display_variant = $this->setUpDisplayVariant(); $this->blockRepository->expects($this->once()) ->method('getVisibleBlocksPerRegion') ->willReturn([]); $route_match = $this->createMock(RouteMatchInterface::class); $event = new PageDisplayVariantSelectionEvent($display_variant->getPluginId(), $route_match); $event->addCacheTags(['my_tag']); $event->addCacheContexts(['my_context']); $event->mergeCacheMaxAge(50); $display_variant->addCacheableDependency($event); $expectedCache = [ 'tags' => [ 'config:block_list', 'my_tag', ], 'contexts' => [ 'my_context', ], 'max-age' => 50, ]; $this->assertSame($expectedCache, $display_variant->build()['#cache']); } }
core/tests/Drupal/Tests/Core/Render/Plugin/DisplayVariant/SimplePageVariantTest.php 0 → 100644 +123 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\Core\Render\Plugin\DisplayVariant; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Render\PageDisplayVariantSelectionEvent; use Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant. */ #[CoversClass(SimplePageVariant::class)] #[Group('Render')] class SimplePageVariantTest extends UnitTestCase { /** * Sets up a display variant plugin for testing. * * @param array $configuration * An array of plugin configuration. * @param array $definition * The plugin definition array. * * @return \Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant * A test display variant plugin. */ public function setUpDisplayVariant($configuration = [], $definition = []) { $container = new Container(); $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') ->disableOriginalConstructor() ->onlyMethods(['assertValidTokens']) ->getMock(); $container->set('cache_contexts_manager', $cache_context_manager); $cache_context_manager->expects($this->any()) ->method('assertValidTokens') ->willReturn(TRUE); \Drupal::setContainer($container); $plugin = new SimplePageVariant($configuration, 'test', $definition); $plugin->setTitle('Test'); $plugin->setMainContent([ '#markup' => 'Test content', ]); return $plugin; } /** * Tests the build method. * * @legacy-covers ::build */ public function testBuild(): void { $title = $this->randomString(); $content = $this->randomString(); $display_variant = $this->setUpDisplayVariant(); $display_variant->setTitle($title); $display_variant->setMainContent([ '#markup' => $content, ]); $expected = [ 'content' => [ 'messages' => [ '#type' => 'status_messages', '#weight' => -1000, '#include_fallback' => TRUE, ], 'page_title' => [ '#type' => 'page_title', '#title' => $title, '#weight' => -900, ], 'main_content' => [ '#weight' => -800, '#markup' => $content, ], ], '#cache' => [ 'contexts' => [], 'tags' => [], 'max-age' => -1, ], ]; $this->assertSame($expected, $display_variant->build()); } /** * Tests that cache metadata in the plugin are present in the build. * * @legacy-covers ::build */ public function testCacheMetadataFromPlugin(): void { $display_variant = $this->setUpDisplayVariant(); $route_match = $this->createMock(RouteMatchInterface::class); $event = new PageDisplayVariantSelectionEvent($display_variant->getPluginId(), $route_match); $event->addCacheTags(['my_tag']); $event->addCacheContexts(['my_context']); $event->mergeCacheMaxAge(50); $display_variant->addCacheableDependency($event); $expectedCache = [ 'contexts' => [ 'my_context', ], 'tags' => [ 'my_tag', ], 'max-age' => 50, ]; $this->assertSame($expectedCache, $display_variant->build()['#cache']); } }