Interim commit.
[yaffs-website] / web / modules / contrib / devel / tests / src / Kernel / DevelTwigExtensionTest.php
1 <?php
2
3 namespace Drupal\Tests\devel\Kernel;
4
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;
10
11 /**
12  * Tests Twig extensions.
13  *
14  * @group devel
15  */
16 class DevelTwigExtensionTest extends KernelTestBase {
17
18   use DevelDumperTestTrait;
19
20   /**
21    * The user used in test.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $develUser;
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = ['devel', 'user', 'system'];
33
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40
41     $this->installEntitySchema('user');
42     $this->installSchema('system', 'sequences');
43
44     $devel_role = Role::create([
45       'id' => 'admin',
46       'permissions' => ['access devel information'],
47     ]);
48     $devel_role->save();
49
50     $this->develUser = User::create([
51       'name' => $this->randomMachineName(),
52       'roles' => [$devel_role->id()],
53     ]);
54     $this->develUser->save();
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function register(ContainerBuilder $container) {
61     parent::register($container);
62
63     $parameters = $container->getParameter('twig.config');
64     $parameters['debug'] = TRUE;
65     $container->setParameter('twig.config', $parameters);
66   }
67
68   /**
69    * Tests that Twig extension loads appropriately.
70    */
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.');
75   }
76
77   /**
78    * Tests that the Twig dump functions are registered properly.
79    */
80   public function testDumpFunctionsRegistered() {
81     /** @var \Twig_SimpleFunction[] $functions */
82     $functions = \Drupal::service('twig')->getFunctions();
83
84     $dump_functions = ['devel_dump', 'kpr'];
85     $message_functions = ['devel_message', 'dpm', 'dsm'];
86     $registered_functions = $dump_functions + $message_functions;
87
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());
95
96       is_callable($function->getCallable(), TRUE, $callable);
97       if (in_array($name, $dump_functions)) {
98         $this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::dump');
99       }
100       else {
101         $this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::message');
102       }
103     }
104   }
105
106   /**
107    * Tests that the Twig function for XDebug integration is registered properly.
108    */
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');
119   }
120
121   /**
122    * Tests that the Twig extension's dump functions produce the expected output.
123    */
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';
127
128     $context = [
129       'twig_string' => 'context!',
130       'twig_array' => [
131         'first' => 'first value',
132         'second' => 'second value',
133       ],
134       'twig_object' => new \stdClass(),
135     ];
136
137     /** @var \Drupal\Core\Template\TwigEnvironment $environment */
138     $environment = \Drupal::service('twig');
139
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);
144
145     \Drupal::currentUser()->setAccount($this->develUser);
146
147     // Ensures that if no argument is passed to the function the twig context is
148     // dumped.
149     $output = (string) $environment->renderInline($template, $context);
150     $this->assertContains($expected_template_output,  $output);
151     $this->assertContainsDump($output, $context, 'Twig context');
152
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']);
158
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']);
168
169     // Clear messages.
170     drupal_get_messages();
171
172     $retrieve_message = function ($messages, $index) {
173       return isset($messages['status'][$index]) ? (string) $messages['status'][$index] : NULL;
174     };
175
176     // Ensures that if no argument is passed to the function the twig context is
177     // dumped.
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');
183
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']);
190
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']);
201   }
202
203 }