You are here

public function AccessTest::testDrupalEntity 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::testDrupalEntity()

Test callback.

File

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

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalEntity() {

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

  // -- Unprivileged user without access check.
  $build = $this->twigExtension
    ->drupalEntity('node', $this->node
    ->id(), NULL, NULL, FALSE);
  self::assertArrayHasKey('#node', $build);
  $expected_cache = [
    'tags' => [
      'node:1',
      'node_view',
    ],
    'contexts' => [],
    '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
    ->drupalEntity('node', $this->node
    ->id());
  self::assertArrayHasKey('#node', $build);
  $expected_cache = [
    'tags' => [
      'node:1',
      'node_view',
      'tag_from_twig_tweak_test_node_access',
    ],
    'contexts' => [
      'user',
      'user.permissions',
    ],
    'max-age' => 50,
  ];
  self::assertSame($expected_cache, $build['#cache']);

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