View source
<?php
namespace Drupal\Tests\components\Kernel;
class TwigExtensionTest extends ComponentsKernelTestBase {
protected static $modules = [
'system',
'components',
'components_twig_extension_test',
];
public function testTemplateFunction() {
$result = NULL;
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());
}
$expected = '<ul><li>first item</li><li>second item</li></ul>';
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($expected, $result);
}
else {
$this
->assertContains($expected, $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) {
$result = NULL;
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());
}
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($expected, $result);
}
else {
$this
->assertContains($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>',
],
'Uses deprecated "path" named argument' => [
'theme_hook' => 'components_twig_extension_test_set_filter_deprecated_named_arguments',
'expected' => '<div class="new-class"></div>',
],
'Uses deprecated "array" named argument' => [
'theme_hook' => 'components_twig_extension_test_set_filter_deprecated_named_argument',
'expected' => '<div id="the_element_id" class="new-class"></div>',
],
];
}
public function testAddFilter(string $theme_hook, string $expected) {
$result = NULL;
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());
}
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($expected, $result);
}
else {
$this
->assertContains($expected, $result);
}
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($expected, $result);
}
else {
$this
->assertContains($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>',
],
'Uses deprecated "path" named argument' => [
'theme_hook' => 'components_twig_extension_test_add_filter_deprecated_named_arguments',
'expected' => '<div class="original-container-class new-class"></div>',
],
];
}
}