You are here

public function AccessTest::testDrupalField in Twig Tweak 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/AccessTest.php \Drupal\Tests\twig_tweak\Kernel\AccessTest::testDrupalField()

Test callback.

File

tests/src/Kernel/AccessTest.php, line 113

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalField() {

  // -- Unprivileged user.
  $this
    ->setUpCurrentUser([
    'name' => 'User 1',
  ]);
  $build = $this->twigExtension
    ->drupalField('title', 'node', $this->node
    ->id());
  self::assertNull($build);

  // -- Privileged user.
  $this
    ->setUpCurrentUser([
    'name' => 'User 2',
  ], [
    'access content',
  ]);
  $build = $this->twigExtension
    ->drupalField('title', 'node', $this->node
    ->id());
  self::assertArrayHasKey('#items', $build);
  $expected_cache = [
    'contexts' => [
      'user',
      'user.permissions',
    ],
    'tags' => [
      'node:1',
      'tag_from_twig_tweak_test_node_access',
    ],
    'max-age' => 50,
  ];
  self::assertSame($expected_cache, $build['#cache']);
}