3 namespace Drupal\Tests\devel\Kernel;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\devel\Twig\Extension\Debug;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\Entity\User;
12 * Tests Twig extensions.
16 class DevelTwigExtensionTest extends KernelTestBase {
18 use DevelDumperTestTrait;
21 * The user used in test.
23 * @var \Drupal\user\UserInterface
32 public static $modules = ['devel', 'user', 'system'];
38 protected function setUp() {
41 $this->installEntitySchema('user');
42 $this->installSchema('system', 'sequences');
44 $devel_role = Role::create([
46 'permissions' => ['access devel information'],
50 $this->develUser = User::create([
51 'name' => $this->randomMachineName(),
52 'roles' => [$devel_role->id()],
54 $this->develUser->save();
60 public function register(ContainerBuilder $container) {
61 parent::register($container);
63 $parameters = $container->getParameter('twig.config');
64 $parameters['debug'] = TRUE;
65 $container->setParameter('twig.config', $parameters);
69 * Tests that Twig extension loads appropriately.
71 public function testTwigExtensionLoaded() {
72 $twig_service = \Drupal::service('twig');
73 $extension = $twig_service->getExtension('devel_debug');
74 $this->assertEquals(get_class($extension), Debug::class, 'Debug Extension loaded successfully.');
78 * Tests that the Twig dump functions are registered properly.
80 public function testDumpFunctionsRegistered() {
81 /** @var \Twig_SimpleFunction[] $functions */
82 $functions = \Drupal::service('twig')->getFunctions();
84 $dump_functions = ['devel_dump', 'kpr'];
85 $message_functions = ['devel_message', 'dpm', 'dsm'];
86 $registered_functions = $dump_functions + $message_functions;
88 foreach ($registered_functions as $name) {
89 $function = $functions[$name];
90 $this->assertTrue($function instanceof \Twig_SimpleFunction);
91 $this->assertEquals($function->getName(), $name);
92 $this->assertTrue($function->needsContext());
93 $this->assertTrue($function->needsEnvironment());
94 $this->assertTrue($function->isVariadic());
96 is_callable($function->getCallable(), TRUE, $callable);
97 if (in_array($name, $dump_functions)) {
98 $this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::dump');
101 $this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::message');
107 * Tests that the Twig function for XDebug integration is registered properly.
109 public function testXDebugIntegrationFunctionsRegistered() {
110 /** @var \Twig_SimpleFunction $function */
111 $function = \Drupal::service('twig')->getFunction('devel_breakpoint');
112 $this->assertTrue($function instanceof \Twig_SimpleFunction);
113 $this->assertEquals($function->getName(), 'devel_breakpoint');
114 $this->assertTrue($function->needsContext());
115 $this->assertTrue($function->needsEnvironment());
116 $this->assertTrue($function->isVariadic());
117 is_callable($function->getCallable(), TRUE, $callable);
118 $this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::breakpoint');
122 * Tests that the Twig extension's dump functions produce the expected output.
124 public function testDumpFunctions() {
125 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump() }}';
126 $expected_template_output = 'test-with-context context! first value second value';
129 'twig_string' => 'context!',
131 'first' => 'first value',
132 'second' => 'second value',
134 'twig_object' => new \stdClass(),
137 /** @var \Drupal\Core\Template\TwigEnvironment $environment */
138 $environment = \Drupal::service('twig');
140 // Ensures that the twig extension does nothing if the current
141 // user has not the adequate permission.
142 $this->assertTrue($environment->isDebug());
143 $this->assertEquals($environment->renderInline($template, $context), $expected_template_output);
145 \Drupal::currentUser()->setAccount($this->develUser);
147 // Ensures that if no argument is passed to the function the twig context is
149 $output = (string) $environment->renderInline($template, $context);
150 $this->assertContains($expected_template_output, $output);
151 $this->assertContainsDump($output, $context, 'Twig context');
153 // Ensures that if an argument is passed to the function it is dumped.
154 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump(twig_array) }}';
155 $output = (string) $environment->renderInline($template, $context);
156 $this->assertContains($expected_template_output, $output);
157 $this->assertContainsDump($output, $context['twig_array']);
159 // Ensures that if more than one argument is passed the function works
160 // properly and every argument is dumped separately.
161 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump(twig_string, twig_array.first, twig_array, twig_object) }}';
162 $output = (string) $environment->renderInline($template, $context);
163 $this->assertContains($expected_template_output, $output);
164 $this->assertContainsDump($output, $context['twig_string']);
165 $this->assertContainsDump($output, $context['twig_array']['first']);
166 $this->assertContainsDump($output, $context['twig_array']);
167 $this->assertContainsDump($output, $context['twig_object']);
170 drupal_get_messages();
172 $retrieve_message = function ($messages, $index) {
173 return isset($messages['status'][$index]) ? (string) $messages['status'][$index] : NULL;
176 // Ensures that if no argument is passed to the function the twig context is
178 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message() }}';
179 $output = (string) $environment->renderInline($template, $context);
180 $this->assertContains($expected_template_output, $output);
181 $messages = drupal_get_messages();
182 $this->assertDumpExportEquals($retrieve_message($messages, 0), $context, 'Twig context');
184 // Ensures that if an argument is passed to the function it is dumped.
185 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message(twig_array) }}';
186 $output = (string) $environment->renderInline($template, $context);
187 $this->assertContains($expected_template_output, $output);
188 $messages = drupal_get_messages();
189 $this->assertDumpExportEquals($retrieve_message($messages, 0), $context['twig_array']);
191 // Ensures that if more than one argument is passed to the function works
192 // properly and every argument is dumped separately.
193 $template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_message(twig_string, twig_array.first, twig_array, twig_object) }}';
194 $output = (string) $environment->renderInline($template, $context);
195 $this->assertContains($expected_template_output, $output);
196 $messages = drupal_get_messages();
197 $this->assertDumpExportEquals($retrieve_message($messages, 0), $context['twig_string']);
198 $this->assertDumpExportEquals($retrieve_message($messages, 1), $context['twig_array']['first']);
199 $this->assertDumpExportEquals($retrieve_message($messages, 2), $context['twig_array']);
200 $this->assertDumpExportEquals($retrieve_message($messages, 3), $context['twig_object']);