You are here

public function TwigSandboxTest::testEntitySafePrefixes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 72
Contains \Drupal\Tests\Core\Template\TwigSandboxTest.

Class

TwigSandboxTest
Tests the twig sandbox policy.

Namespace

Drupal\Tests\Core\Template

Code

public function testEntitySafePrefixes() {
  $entity = $this
    ->getMock('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
    ->getMock('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
    ->getMock('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.');
}