function OverridesSectionStorageTest::testAccess

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Kernel\OverridesSectionStorageTest::testAccess()
  2. 8.9.x core/modules/layout_builder/tests/src/Kernel/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Kernel\OverridesSectionStorageTest::testAccess()
  3. 10 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 76

Class

OverridesSectionStorageTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21layout_builder%21src%21Plugin%21SectionStorage%21OverridesSectionStorage.php/class/OverridesSectionStorage/11.x" title="Defines the &#039;overrides&#039; section storage type." class="local">\Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage</a>

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testAccess($expected, $is_enabled, array $section_data, array $permissions) : void {
    $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->assertFalse($result);
    $result = $this->plugin
        ->access('view', $account);
    $this->assertFalse($result);
}

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