function OverridesSectionStorageTest::testAccess

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Kernel\OverridesSectionStorageTest::testAccess()
  2. 10 core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Kernel\OverridesSectionStorageTest::testAccess()
  3. 11.x core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Kernel\OverridesSectionStorageTest::testAccess()

@covers ::access @dataProvider providerTestAccess

Parameters

bool $expected: The expected outcome of ::access().

bool $is_enabled: Whether Layout Builder is enabled for this display.

array $section_data: Data to store as the sections value for Layout Builder.

string[] $permissions: An array of permissions to grant to the user.

File

core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php, line 75

Class

OverridesSectionStorageTest
@coversDefaultClass \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testAccess($expected, $is_enabled, array $section_data, array $permissions) {
    $display = LayoutBuilderEntityViewDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'default',
        'status' => TRUE,
    ]);
    if ($is_enabled) {
        $display->enableLayoutBuilder();
    }
    $display->setOverridable()
        ->save();
    $entity = EntityTest::create([
        OverridesSectionStorage::FIELD_NAME => $section_data,
    ]);
    $entity->save();
    $account = $this->setUpCurrentUser([], $permissions);
    $this->plugin
        ->setContext('entity', EntityContext::fromEntity($entity));
    $this->plugin
        ->setContext('view_mode', new Context(new ContextDefinition('string'), 'default'));
    // Check access with both the global current user as well as passing one in.
    $result = $this->plugin
        ->access('view');
    $this->assertSame($expected, $result);
    $result = $this->plugin
        ->access('view', $account);
    $this->assertSame($expected, $result);
    // Create a translation.
    ConfigurableLanguage::createFromLangcode('es')->save();
    $entity = EntityTest::load($entity->id());
    $translation = $entity->addTranslation('es');
    $translation->save();
    $this->plugin
        ->setContext('entity', EntityContext::fromEntity($translation));
    // Perform the same checks again but with a non default translation which
    // should always deny access.
    $result = $this->plugin
        ->access('view');
    $this->assertSame(FALSE, $result);
    $result = $this->plugin
        ->access('view', $account);
    $this->assertSame(FALSE, $result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.