You are here

public function AccessTest::testDrupalEntityAddForm in Twig Tweak 8.2

Test callback.

File

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

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalEntityAddForm() {
  $node_values = [
    'type' => 'article',
  ];

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

  // -- Unprivileged user without access check.
  $build = $this->twigExtension
    ->drupalEntityForm('node', NULL, 'default', $node_values, FALSE);
  self::assertArrayHasKey('form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);

  // -- Privileged user with access check.
  $this
    ->setUpCurrentUser([
    'name' => 'User 2',
  ], [
    'access content',
    'create article content',
  ]);
  $build = $this->twigExtension
    ->drupalEntityForm('node', NULL, 'default', $node_values);
  self::assertArrayHasKey('form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user.permissions',
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);

  // -- Privileged user without access check.
  $build = $this->twigExtension
    ->drupalEntityForm('node', NULL, 'default', $node_values);
  self::assertArrayHasKey('form_id', $build);
  $expected_cache = [
    'contexts' => [
      'user.permissions',
      'user.roles:authenticated',
    ],
    'tags' => [
      'config:core.entity_form_display.node.article.default',
    ],
    'max-age' => Cache::PERMANENT,
  ];
  self::assertSame($expected_cache, $build['#cache']);
}