View source
<?php
namespace Drupal\Tests\devel\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\devel\Twig\Extension\Debug;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class DevelTwigExtensionTest extends KernelTestBase {
use DevelDumperTestTrait;
use MessengerTrait;
protected $develUser;
public static $modules = [
'devel',
'user',
'system',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', 'sequences');
$devel_role = Role::create([
'id' => 'admin',
'permissions' => [
'access devel information',
],
]);
$devel_role
->save();
$this->develUser = User::create([
'name' => $this
->randomMachineName(),
'roles' => [
$devel_role
->id(),
],
]);
$this->develUser
->save();
}
public function register(ContainerBuilder $container) {
parent::register($container);
$parameters = $container
->getParameter('twig.config');
$parameters['debug'] = TRUE;
$container
->setParameter('twig.config', $parameters);
}
public function testTwigExtensionLoaded() {
$twig_service = \Drupal::service('twig');
$extension = $twig_service
->getExtension(Debug::class);
$this
->assertEquals(get_class($extension), Debug::class, 'Debug Extension loaded successfully.');
}
public function testDumpFunctionsRegistered() {
$functions = \Drupal::service('twig')
->getFunctions();
$dump_functions = [
'devel_dump',
'kpr',
];
$message_functions = [
'devel_message',
'dpm',
'dsm',
];
$registered_functions = $dump_functions + $message_functions;
foreach ($registered_functions as $name) {
$function = $functions[$name];
$this
->assertTrue($function instanceof \Twig_SimpleFunction);
$this
->assertEquals($function
->getName(), $name);
$this
->assertTrue($function
->needsContext());
$this
->assertTrue($function
->needsEnvironment());
$this
->assertTrue($function
->isVariadic());
is_callable($function
->getCallable(), TRUE, $callable);
if (in_array($name, $dump_functions)) {
$this
->assertEquals($callable, 'Drupal\\devel\\Twig\\Extension\\Debug::dump');
}
else {
$this
->assertEquals($callable, 'Drupal\\devel\\Twig\\Extension\\Debug::message');
}
}
}
public function testXdebugIntegrationFunctionsRegistered() {
$function = \Drupal::service('twig')
->getFunction('devel_breakpoint');
$this
->assertTrue($function instanceof \Twig_SimpleFunction);
$this
->assertEquals($function
->getName(), 'devel_breakpoint');
$this
->assertTrue($function
->needsContext());
$this
->assertTrue($function
->needsEnvironment());
$this
->assertTrue($function
->isVariadic());
is_callable($function
->getCallable(), TRUE, $callable);
$this
->assertEquals($callable, 'Drupal\\devel\\Twig\\Extension\\Debug::breakpoint');
}
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(),
];
$environment = \Drupal::service('twig');
$this
->assertTrue($environment
->isDebug());
$this
->assertEquals($environment
->renderInline($template, $context), $expected_template_output);
\Drupal::currentUser()
->setAccount($this->develUser);
$output = (string) $environment
->renderInline($template, $context);
$this
->assertStringContainsString($expected_template_output, $output, 'When no argument passed');
$this
->assertContainsDump($output, $context, 'Twig context');
$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']);
$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']);
$this
->messenger()
->deleteAll();
$retrieve_message = function ($messages, $index) {
return isset($messages['status'][$index]) ? (string) $messages['status'][$index] : NULL;
};
$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');
$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']);
$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']);
}
}