
| Current Path : /var/www/html/strat/web/core/modules/node/tests/src/Kernel/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/strat/web/core/modules/node/tests/src/Kernel/NodeTemplateSuggestionsTest.php |
<?php
declare(strict_types=1);
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
/**
* Tests node template suggestions.
*
* @group node
*/
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.');
}
}