View source
<?php
namespace Drupal\Tests\system\Kernel\Theme;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Component\Render\MarkupInterface;
class ThemeTest extends KernelTestBase {
protected static $modules = [
'theme_test',
'node',
'system',
];
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
}
public function testAttributeMerging() {
$theme_test_render_element = [
'elements' => [
'#attributes' => [
'data-foo' => 'bar',
],
],
'attributes' => [
'id' => 'bazinga',
],
];
$this
->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
}
public function testThemeDataTypes() {
$foos = [
'null' => NULL,
'false' => FALSE,
'integer' => 1,
'string' => 'foo',
'empty_string' => '',
];
foreach ($foos as $type => $example) {
$output = \Drupal::theme()
->render('theme_test_foo', [
'foo' => $example,
]);
$this
->assertTrue($output instanceof MarkupInterface || is_string($output), new FormattableMarkup('\\Drupal::theme() returns an object that implements MarkupInterface or a string for data type @type.', [
'@type' => $type,
]));
if ($output instanceof MarkupInterface) {
$this
->assertSame((string) $example, $output
->__toString());
}
elseif (is_string($output)) {
$this
->assertSame('', $output, 'A string will be return when the theme returns an empty string.');
}
}
$output = \Drupal::theme()
->render([
'suggestionnotimplemented',
], []);
$this
->assertFalse($output, '\\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
}
public function testThemeSuggestions() {
$this
->config('system.site')
->set('page.front', '/nobody-home')
->save();
$args = [
'node',
'1',
'edit',
];
$suggestions = theme_get_suggestions($args, 'page');
$this
->assertEquals([
'page__node',
'page__node__%',
'page__node__1',
'page__node__edit',
], $suggestions, 'Found expected node edit page suggestions');
$args = [
'node',
'\\1',
];
$suggestions = theme_get_suggestions($args, 'page');
$this
->assertEquals([
'page__node',
'page__node__%',
'page__node__1',
], $suggestions, 'Removed invalid \\ from suggestions');
$args = [
'node',
'1/',
];
$suggestions = theme_get_suggestions($args, 'page');
$this
->assertEquals([
'page__node',
'page__node__%',
'page__node__1',
], $suggestions, 'Removed invalid / from suggestions');
$args = [
'node',
"1\0",
];
$suggestions = theme_get_suggestions($args, 'page');
$this
->assertEquals([
'page__node',
'page__node__%',
'page__node__1',
], $suggestions, 'Removed invalid \\0 from suggestions');
$args = [
'node',
'1',
'hyphen-path',
];
$result = [
'page__node',
'page__node__%',
'page__node__1',
'page__node__hyphen_path',
];
$suggestions = theme_get_suggestions($args, 'page');
$this
->assertEquals($result, $suggestions, 'Found expected page suggestions for paths containing hyphens.');
}
public function testListThemes() {
$this->container
->get('theme_installer')
->install([
'test_subtheme',
]);
$theme_handler = $this->container
->get('theme_handler');
$themes = $theme_handler
->listInfo();
$this
->assertSame(1, $themes['test_theme']->status, 'Installed theme detected');
$base_theme_list = [
'test_basetheme' => 'Theme test base theme',
];
$sub_theme_list = [
'test_subsubtheme' => 'Theme test subsubtheme',
'test_subtheme' => 'Theme test subtheme',
];
$this
->assertSame($sub_theme_list, $themes['test_basetheme']->sub_themes, 'Base theme\'s object includes list of subthemes.');
$this
->assertSame($base_theme_list, $themes['test_subtheme']->base_themes, 'Subtheme\'s object includes list of base themes.');
$this
->assertSame('twig', $themes['test_subtheme']->engine, 'Subtheme\'s object includes the theme engine.');
$this
->assertSame('twig', $themes['test_basetheme']->prefix, 'Base theme\'s object includes the theme engine prefix.');
$this
->assertSame('twig', $themes['test_subtheme']->prefix, 'Subtheme\'s object includes the theme engine prefix.');
}
public function testDrupalRenderChildren() {
$element = [
'#theme' => 'theme_test_render_element_children',
'child' => [
'#markup' => 'Foo',
],
];
$this
->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme recursion loop when rendering a render element.');
$element = [
'#theme_wrappers' => [
'theme_test_render_element_children',
],
'child' => [
'#markup' => 'Foo',
],
];
$this
->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme_wrappers recursion loop when rendering a render element.');
}
public function testFindThemeTemplates() {
$registry = $this->container
->get('theme.registry')
->get();
$templates = drupal_find_theme_templates($registry, '.html.twig', $this
->getThemePath('test_theme'));
$this
->assertEquals('node--1', $templates['node__1']['template'], 'Template node--1.html.twig was found in test_theme.');
}
}