You are here

public function LayoutBuilderAccessTest::testAccessWithBundles in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/Functional/LayoutBuilderAccessTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderAccessTest::testAccessWithBundles()

Tests Layout Builder access for an entity type that has bundles.

@dataProvider providerTestAccessWithBundles

Parameters

array $permissions: An array of permissions to grant to the user.

bool $default_access: Whether access is expected for the defaults.

bool $non_editable_access: Whether access is expected for a non-editable override.

bool $editable_access: Whether access is expected for an editable override.

array $permission_dependencies: An array of expected permission dependencies.

File

core/modules/layout_builder/tests/src/Functional/LayoutBuilderAccessTest.php, line 72

Class

LayoutBuilderAccessTest
Tests access to Layout Builder.

Namespace

Drupal\Tests\layout_builder\Functional

Code

public function testAccessWithBundles(array $permissions, $default_access, $non_editable_access, $editable_access, array $permission_dependencies) {
  $permissions[] = 'edit own bundle_with_section_field content';
  $permissions[] = 'access content';
  $user = $this
    ->drupalCreateUser($permissions);
  $this
    ->drupalLogin($user);
  $editable_node = $this
    ->createNode([
    'uid' => $user
      ->id(),
    'type' => 'bundle_with_section_field',
    'title' => 'The first node title',
    'body' => [
      [
        'value' => 'The first node body',
      ],
    ],
  ]);
  $non_editable_node = $this
    ->createNode([
    'uid' => 1,
    'type' => 'bundle_with_section_field',
    'title' => 'The second node title',
    'body' => [
      [
        'value' => 'The second node body',
      ],
    ],
  ]);
  $non_viewable_node = $this
    ->createNode([
    'uid' => $user
      ->id(),
    'status' => 0,
    'type' => 'bundle_with_section_field',
    'title' => 'Nobody can see this node.',
    'body' => [
      [
        'value' => 'Does it really exist?',
      ],
    ],
  ]);
  $this
    ->drupalGet($editable_node
    ->toUrl('edit-form'));
  $this
    ->assertExpectedAccess(TRUE);
  $this
    ->drupalGet($non_editable_node
    ->toUrl('edit-form'));
  $this
    ->assertExpectedAccess(FALSE);
  $this
    ->drupalGet('admin/structure/types/manage/bundle_with_section_field/display/default/layout');
  $this
    ->assertExpectedAccess($default_access);
  $this
    ->drupalGet('node/' . $editable_node
    ->id() . '/layout');
  $this
    ->assertExpectedAccess($editable_access);
  $this
    ->drupalGet('node/' . $non_editable_node
    ->id() . '/layout');
  $this
    ->assertExpectedAccess($non_editable_access);
  $this
    ->drupalGet($non_viewable_node
    ->toUrl());
  $this
    ->assertExpectedAccess(FALSE);
  $this
    ->drupalGet('node/' . $non_viewable_node
    ->id() . '/layout');
  $this
    ->assertExpectedAccess(FALSE);
  if (!empty($permission_dependencies)) {
    $permission_definitions = \Drupal::service('user.permissions')
      ->getPermissions();
    foreach ($permission_dependencies as $permission => $expected_dependencies) {
      $this
        ->assertSame($expected_dependencies, $permission_definitions[$permission]['dependencies']);
    }
  }
}