class NodeTemplateSuggestionsTest

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

Tests node template suggestions.

@group node

Hierarchy

Expanded class hierarchy of NodeTemplateSuggestionsTest

File

core/modules/node/tests/src/Kernel/NodeTemplateSuggestionsTest.php, line 16

Namespace

Drupal\Tests\node\Kernel
View source
class NodeTemplateSuggestionsTest extends KernelTestBase {
  use NodeCreationTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'user',
    'system',
  ];
  
  /**
   * Tests if template_preprocess_node() generates the correct suggestions.
   */
  public function testNodeThemeHookSuggestions() : void {
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installConfig([
      'system',
    ]);
    NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ])->save();
    // Create node to be rendered.
    $node = $this->createNode();
    $view_mode = 'full';
    // Simulate theming of the node.
    $build = \Drupal::entityTypeManager()->getViewBuilder('node')
      ->view($node, $view_mode);
    $variables['elements'] = $build;
    $suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_node', [
      $variables,
    ]);
    $this->assertEquals([
      'node__full',
      'node__page',
      'node__page__full',
      'node__' . $node->id(),
      'node__' . $node->id() . '__full',
    ], $suggestions, 'Found expected node suggestions.');
    // Change the view mode.
    $view_mode = 'node.my_custom_view_mode';
    $build = \Drupal::entityTypeManager()->getViewBuilder('node')
      ->view($node, $view_mode);
    $variables['elements'] = $build;
    $suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_node', [
      $variables,
    ]);
    $this->assertEquals([
      'node__node_my_custom_view_mode',
      'node__page',
      'node__page__node_my_custom_view_mode',
      'node__' . $node->id(),
      'node__' . $node->id() . '__node_my_custom_view_mode',
    ], $suggestions, 'Found expected node suggestions.');
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
NodeTemplateSuggestionsTest::$modules protected static property Modules to install.
NodeTemplateSuggestionsTest::testNodeThemeHookSuggestions public function Tests if template_preprocess_node() generates the correct suggestions.
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.

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