function DevelTwigExtensionTest::testDumpFunctions
Same name in other branches
- 4.x tests/src/Kernel/DevelTwigExtensionTest.php \Drupal\Tests\devel\Kernel\DevelTwigExtensionTest::testDumpFunctions()
- 5.x tests/src/Kernel/DevelTwigExtensionTest.php \Drupal\Tests\devel\Kernel\DevelTwigExtensionTest::testDumpFunctions()
Tests that the Twig extension's dump functions produce the expected output.
File
-
tests/
src/ Kernel/ DevelTwigExtensionTest.php, line 124
Class
- DevelTwigExtensionTest
- Tests Twig extensions.
Namespace
Drupal\Tests\devel\KernelCode
public function testDumpFunctions() {
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump() }}';
$expected_template_output = 'test-with-context context! first value second value';
$context = [
'twig_string' => 'context!',
'twig_array' => [
'first' => 'first value',
'second' => 'second value',
],
'twig_object' => new \stdClass(),
];
/** @var \Drupal\Core\Template\TwigEnvironment $environment */
$environment = \Drupal::service('twig');
// Ensures that the twig extension does nothing if the current
// user has not the adequate permission.
$this->assertTrue($environment->isDebug());
$this->assertEquals($environment->renderInline($template, $context), $expected_template_output);
\Drupal::currentUser()->setAccount($this->develUser);
// Ensures that if no argument is passed to the function the twig context is
// dumped.
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$this->assertContainsDump($output, $context, 'Twig context');
// Ensures that if an argument is passed to the function it is dumped.
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump(twig_array) }}';
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$this->assertContainsDump($output, $context['twig_array']);
// Ensures that if more than one argument is passed the function works
// properly and every argument is dumped separately.
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump(twig_string, twig_array.first, twig_array, twig_object) }}';
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$this->assertContainsDump($output, $context['twig_string']);
$this->assertContainsDump($output, $context['twig_array']['first']);
$this->assertContainsDump($output, $context['twig_array']);
$this->assertContainsDump($output, $context['twig_object']);
// Clear messages.
drupal_get_messages();
$retrieve_message = function ($messages, $index) {
return isset($messages['status'][$index]) ? (string) $messages['status'][$index] : NULL;
};
// Ensures that if no argument is passed to the function the twig context is
// dumped.
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message() }}';
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$messages = drupal_get_messages();
$this->assertDumpExportEquals($retrieve_message($messages, 0), $context, 'Twig context');
// Ensures that if an argument is passed to the function it is dumped.
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message(twig_array) }}';
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$messages = drupal_get_messages();
$this->assertDumpExportEquals($retrieve_message($messages, 0), $context['twig_array']);
// Ensures that if more than one argument is passed to the function works
// properly and every argument is dumped separately.
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message(twig_string, twig_array.first, twig_array, twig_object) }}';
$output = (string) $environment->renderInline($template, $context);
$this->assertContains($expected_template_output, $output);
$messages = drupal_get_messages();
$this->assertDumpExportEquals($retrieve_message($messages, 0), $context['twig_string']);
$this->assertDumpExportEquals($retrieve_message($messages, 1), $context['twig_array']['first']);
$this->assertDumpExportEquals($retrieve_message($messages, 2), $context['twig_array']);
$this->assertDumpExportEquals($retrieve_message($messages, 3), $context['twig_object']);
}