You are here

public function AccessTest::testDrupalEntityEditForm in Twig Tweak 8.2

Test callback.

File

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

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalEntityEditForm() {

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

  // -- Unprivileged user without access check.
  $build = $this->twigExtension
    ->drupalEntityForm('node', $this->node
    ->id(), 'default', [], FALSE);
  self::assertArrayHasKey('form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
      '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
    ->drupalEntityForm('node', $this->node
    ->id());
  self::assertArrayHasKey('#form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user',
      'user.permissions',
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
      '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
    ->drupalEntityForm('node', $this->node
    ->id(), 'default', [], FALSE);
  self::assertArrayHasKey('#form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
      'node:1',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);
}