function DevelTwigExtensionTest::testDumpFunctions

Tests that the Twig extension's dump functions produce the expected output.

File

tests/src/Kernel/DevelTwigExtensionTest.php, line 126

Class

DevelTwigExtensionTest
Tests Twig extensions.

Namespace

Drupal\Tests\devel\Kernel

Code

public function testDumpFunctions() : void {
  $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->assertStringContainsString($expected_template_output, $output, 'When no argument passed');
  $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->assertStringContainsString($expected_template_output, $output, 'When one argument is passed');
  $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->assertStringContainsString($expected_template_output, $output, 'When multiple arguments are passed');
  $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.
  $this->messenger()
    ->deleteAll();
  $retrieve_message = static fn($messages, $index): ?string => 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->assertStringContainsString($expected_template_output, $output, 'When no argument passed');
  $messages = \Drupal::messenger()->deleteAll();
  $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->assertStringContainsString($expected_template_output, $output, 'When one argument is passed');
  $messages = $this->messenger()
    ->deleteAll();
  $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->assertStringContainsString($expected_template_output, $output, 'When multiple arguments are passed');
  $messages = $this->messenger()
    ->deleteAll();
  $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']);
}