public function TwigSandboxTest::testEntitySafePrefixes in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php \Drupal\Tests\Core\Template\TwigSandboxTest::testEntitySafePrefixes()
- 10 core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php \Drupal\Tests\Core\Template\TwigSandboxTest::testEntitySafePrefixes()
Tests that prefixed methods can be called from within Twig templates.
Currently "get", "has", and "is" are the only allowed prefixes.
File
- core/
tests/ Drupal/ Tests/ Core/ Template/ TwigSandboxTest.php, line 83 - Contains \Drupal\Tests\Core\Template\TwigSandboxTest.
Class
- TwigSandboxTest
- Tests the twig sandbox policy.
Namespace
Drupal\Tests\Core\TemplateCode
public function testEntitySafePrefixes() {
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('hasLinkTemplate')
->with('test')
->willReturn(TRUE);
$result = $this->twig
->render('{{ entity.hasLinkTemplate("test") }}', [
'entity' => $entity,
]);
$this
->assertTrue((bool) $result, 'Sandbox policy allows has* functions to be called.');
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('isNew')
->willReturn(TRUE);
$result = $this->twig
->render('{{ entity.isNew }}', [
'entity' => $entity,
]);
$this
->assertTrue((bool) $result, 'Sandbox policy allows is* functions to be called.');
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('getEntityType')
->willReturn('test');
$result = $this->twig
->render('{{ entity.getEntityType }}', [
'entity' => $entity,
]);
$this
->assertEquals('test', $result, 'Sandbox policy allows get* functions to be called.');
}