function TwigSandboxTest::testEntitySafePrefixes

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php \Drupal\Tests\Core\Template\TwigSandboxTest::testEntitySafePrefixes()
  2. 8.9.x core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php \Drupal\Tests\Core\Template\TwigSandboxTest::testEntitySafePrefixes()
  3. 11.x 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 81

Class

TwigSandboxTest
Tests the twig sandbox policy.

Namespace

Drupal\Tests\Core\Template

Code

public function testEntitySafePrefixes() : void {
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.