You are here

public function AccessTest::testDrupalField in Twig Tweak 8.2

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

Test callback.

File

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

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalField() {

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

  // -- Unprivileged user without access check.
  $build = $this->twigExtension
    ->drupalField('title', 'node', $this->node
    ->id(), 'default', NULL, FALSE);
  self::assertArrayHasKey('#items', $build);
  $expected_cache = [
    'contexts' => [],
    'tags' => [
      'node:1',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);

  // -- Privileged user with access check.
  $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']);

  // -- Privileged user without access check.
  $build = $this->twigExtension
    ->drupalField('title', 'node', $this->node
    ->id(), 'default', NULL, FALSE);
  self::assertArrayHasKey('#items', $build);
  $expected_cache = [
    'contexts' => [],
    'tags' => [
      'node:1',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);
}