class SimplePageVariantTest

Tests Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant.

Attributes

#[CoversClass(SimplePageVariant::class)] #[Group('Render')]

Hierarchy

Expanded class hierarchy of SimplePageVariantTest

File

core/tests/Drupal/Tests/Core/Render/Plugin/DisplayVariant/SimplePageVariantTest.php, line 18

Namespace

Drupal\Tests\Core\Render\Plugin\DisplayVariant
View source
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']);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
SimplePageVariantTest::setUpDisplayVariant public function Sets up a display variant plugin for testing.
SimplePageVariantTest::testBuild public function Tests the build method.
SimplePageVariantTest::testCacheMetadataFromPlugin public function Tests that cache metadata in the plugin are present in the build.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 369
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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