View source
<?php
namespace Drupal\Tests\Core\Template;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\TwigSandboxPolicy;
use Drupal\Core\Template\Loader\StringLoader;
use Drupal\Tests\UnitTestCase;
class TwigSandboxTest extends UnitTestCase {
protected $twig;
protected function setUp() {
parent::setUp();
$loader = new StringLoader();
$this->twig = new \Twig_Environment($loader);
$policy = new TwigSandboxPolicy();
$sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
$this->twig
->addExtension($sandbox);
}
public function testEntityDangerousMethods($template) {
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$this
->expectException(\Twig_Sandbox_SecurityError::class);
$this->twig
->render($template, [
'entity' => $entity,
]);
}
public function getTwigEntityDangerousMethods() {
return [
[
'{{ entity.delete }}',
],
[
'{{ entity.save }}',
],
[
'{{ entity.create }}',
],
];
}
public function testExtendedClass() {
$this
->assertEquals(' class="kitten"', $this->twig
->render('{{ attribute.addClass("kitten") }}', [
'attribute' => new TestAttribute(),
]));
}
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.');
}
public function testEntitySafeMethods() {
$entity = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->getMock();
$entity
->expects($this
->atLeastOnce())
->method('get')
->with('title')
->willReturn('test');
$result = $this->twig
->render('{{ entity.get("title") }}', [
'entity' => $entity,
]);
$this
->assertEquals($result, 'test', 'Sandbox policy allows get() to be called.');
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('id')
->willReturn('1234');
$result = $this->twig
->render('{{ entity.id }}', [
'entity' => $entity,
]);
$this
->assertEquals($result, '1234', 'Sandbox policy allows get() to be called.');
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('label')
->willReturn('testing');
$result = $this->twig
->render('{{ entity.label }}', [
'entity' => $entity,
]);
$this
->assertEquals($result, 'testing', 'Sandbox policy allows get() to be called.');
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->atLeastOnce())
->method('bundle')
->willReturn('testing');
$result = $this->twig
->render('{{ entity.bundle }}', [
'entity' => $entity,
]);
$this
->assertEquals($result, 'testing', 'Sandbox policy allows get() to be called.');
}
public function testUrlSafeMethods() {
$url = $this
->getMockBuilder('Drupal\\Core\\Url')
->disableOriginalConstructor()
->getMock();
$url
->expects($this
->once())
->method('toString')
->willReturn('http://kittens.cat/are/cute');
$result = $this->twig
->render('{{ url.toString }}', [
'url' => $url,
]);
$this
->assertEquals($result, 'http://kittens.cat/are/cute', 'Sandbox policy allows toString() to be called.');
}
}
class TestAttribute extends Attribute {
}