You are here

class TwigSandboxTest 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

Tests the twig sandbox policy.

@group Template

@coversDefaultClass \Drupal\Core\Template\TwigSandboxPolicy

Hierarchy

Expanded class hierarchy of TwigSandboxTest

File

core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php, line 21
Contains \Drupal\Tests\Core\Template\TwigSandboxTest.

Namespace

Drupal\Tests\Core\Template
View source
class TwigSandboxTest extends UnitTestCase {

  /**
   * The Twig environment loaded with the sandbox extension.
   *
   * @var \Twig_Environment
   */
  protected $twig;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * Tests that dangerous methods cannot be called in entity objects.
   *
   * @dataProvider getTwigEntityDangerousMethods
   * @expectedException \Twig_Sandbox_SecurityError
   */
  public function testEntityDangerousMethods($template) {
    $entity = $this
      ->getMock('Drupal\\Core\\Entity\\EntityInterface');
    $this->twig
      ->render($template, [
      'entity' => $entity,
    ]);
  }

  /**
   * Data provider for ::testEntityDangerousMethods.
   *
   * @return array
   */
  public function getTwigEntityDangerousMethods() {
    return [
      [
        '{{ entity.delete }}',
      ],
      [
        '{{ entity.save }}',
      ],
      [
        '{{ entity.create }}',
      ],
    ];
  }

  /**
   * Tests that prefixed methods can be called from within Twig templates.
   *
   * Currently "get", "has", and "is" are the only allowed prefixes.
   */
  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.');
  }

  /**
   * Tests that valid methods can be called from within Twig templates.
   *
   * Currently the following methods are whitelisted: id, label, bundle, and
   * get.
   */
  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
      ->getMock('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
      ->getMock('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
      ->getMock('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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwigSandboxTest::$twig protected property The Twig environment loaded with the sandbox extension.
TwigSandboxTest::getTwigEntityDangerousMethods public function Data provider for ::testEntityDangerousMethods.
TwigSandboxTest::setUp protected function Overrides UnitTestCase::setUp
TwigSandboxTest::testEntityDangerousMethods public function Tests that dangerous methods cannot be called in entity objects.
TwigSandboxTest::testEntitySafeMethods public function Tests that valid methods can be called from within Twig templates.
TwigSandboxTest::testEntitySafePrefixes public function Tests that prefixed methods can be called from within Twig templates.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.