You are here

public function TwigSandboxTest::testEntitySafePrefixes in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php \Drupal\Tests\Core\Template\TwigSandboxTest::testEntitySafePrefixes()
  2. 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 80
Contains \Drupal\Tests\Core\Template\TwigSandboxTest.

Class

TwigSandboxTest
Tests the twig sandbox policy.

Namespace

Drupal\Tests\Core\Template

Code

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($result, 'test', 'Sandbox policy allows get* functions to be called.');
}