View source
<?php
namespace Drupal\Tests\components\Kernel;
class TwigExtensionTest extends ComponentsKernelTestBase {
protected static $modules = [
'system',
'components',
'components_twig_extension_test',
];
public function testTemplateFunction() {
try {
$element = [
'#theme' => 'components_twig_extension_test_template_function',
'#items' => [
'first item',
'second item',
],
];
$result = $this
->render($element);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertStringContainsString('<ul><li>first item</li><li>second item</li></ul>', $result);
}
public function testRecursiveMergeFilter(string $theme_hook, string $expected) {
try {
$element = [
'#theme' => $theme_hook,
'list' => [
'#theme' => 'item_list',
'#items' => [
[
'#type' => 'container',
'#attributes' => [
'id' => 'the_element_id',
'class' => [
'original-container-class',
],
],
],
],
],
];
$result = $this
->render($element);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertStringContainsString($expected, $result);
}
public function providerTestRecursiveMergeFilter() : array {
return [
'Uses positional arguments' => [
'theme_hook' => 'components_twig_extension_test_recursive_merge_filter',
'expected' => '<div id="the_element_id" class="new-class"></div>',
],
'Uses named arguments' => [
'theme_hook' => 'components_twig_extension_test_recursive_merge_filter_named_arguments',
'expected' => '<div id="the_element_id" class="new-class"></div>',
],
];
}
public function testSetFilter(string $theme_hook, string $expected) {
try {
$element = [
'#theme' => $theme_hook,
'list' => [
'#theme' => 'item_list',
'#items' => [
[
'#type' => 'container',
'#attributes' => [
'id' => 'the_element_id',
'class' => [
'original-container-class',
],
],
],
],
],
];
$result = $this
->render($element);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertStringContainsString($expected, $result);
}
public function providerTestSetFilter() : array {
return [
'Uses positional arguments' => [
'theme_hook' => 'components_twig_extension_test_set_filter',
'expected' => '<div class="new-class"></div>',
],
'Uses named arguments' => [
'theme_hook' => 'components_twig_extension_test_set_filter_named_arguments',
'expected' => '<div class="new-class"></div>',
],
];
}
public function testAddFilter(string $theme_hook, string $expected) {
try {
$element = [
'#theme' => $theme_hook,
'list' => [
'#theme' => 'item_list',
'#items' => [
[
'#type' => 'container',
'#attributes' => [
'class' => [
'original-container-class',
],
],
],
],
],
];
$result = $this
->render($element);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertStringContainsString($expected, $result);
}
public function providerTestAddFilter() : array {
return [
'Uses positional arguments' => [
'theme_hook' => 'components_twig_extension_test_add_filter',
'expected' => '<div class="original-container-class new-class"></div>',
],
'Uses named arguments' => [
'theme_hook' => 'components_twig_extension_test_add_filter_named_arguments',
'expected' => '<div class="original-container-class new-class"></div>',
],
'Uses "values" named argument' => [
'theme_hook' => 'components_twig_extension_test_add_filter_plural_named_arguments',
'expected' => '<div class="original-container-class new-class-1 new-class-2"></div>',
],
];
}
}