You are here

public function LayoutBuilderLockTest::testMultipleSections in Layout Builder Lock 8

Tests with at least 3 sections.

Throws

\Behat\Mink\Exception\ExpectationException

File

tests/src/Functional/LayoutBuilderLockTest.php, line 320

Class

LayoutBuilderLockTest
Tests Layout Builder Lock.

Namespace

Drupal\Tests\layout_builder_lock\Functional

Code

public function testMultipleSections() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Ensure the lock settings are added to the correct section when adding a
  // new section before an existing section.
  // We expect that:
  //  - we can't add lock settings for new sections,
  //    @see https://www.drupal.org/project/layout_builder_lock/issues/3129009
  //  - the settings are stored on the correct section
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/0/layout_onecol');
  $assert_session
    ->responseContains('Locks can be configured when the section has been added.');

  // Add a new section above default
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/0/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'section above default',
  ], 'Add section');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/0');
  $this
    ->drupalPostForm(NULL, [
    'layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']' => TRUE,
  ], 'Update');

  // Add a new section between previous created and default
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/1/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'new section in between',
  ], 'Add section');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/1');
  $this
    ->drupalPostForm(NULL, [
    'layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_BEFORE . ']' => TRUE,
  ], 'Update');
  $page
    ->pressButton('Save layout');

  // we expect that the first section (first added) has the `locked section configure` checkbox checked
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/0');
  $assert_session
    ->checkboxChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']');
  $assert_session
    ->checkboxNotChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_BEFORE . ']');

  // we expect that the second section (last added) has the `locked section before` checkbox checked
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/1');
  $assert_session
    ->checkboxChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_BEFORE . ']');
  $assert_session
    ->checkboxNotChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']');

  // we expect that the third section (default) has no checkboxes checked
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/2');
  $assert_session
    ->checkboxNotChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']');
  $assert_session
    ->checkboxNotChecked('layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_BEFORE . ']');

  // Create a node.
  $node = $this
    ->drupalCreateNode([
    'type' => 'landing_page',
    'title' => 'Homepage',
  ]);

  // Simply login as an editor. Should not throw any PHP error
  // @see https://www.drupal.org/project/layout_builder_lock/issues/3121250
  $this
    ->drupalLogin($this->editor);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/layout');
  $assert_session
    ->linkByHrefExists('/layout_builder/choose/section/overrides/node.' . $node
    ->id() . '/0');
  $assert_session
    ->linkByHrefNotExists('/layout_builder/choose/section/overrides/node.' . $node
    ->id() . '/1');
  $assert_session
    ->linkByHrefExists('/layout_builder/choose/section/overrides/node.' . $node
    ->id() . '/2');
  $assert_session
    ->linkExists('Configure new section in between');
  $assert_session
    ->linkExists('Configure Section 3');

  // Ensure sections with empty lock config don't mess up the subsequent
  // 'configure section' links.
  $this
    ->drupalLogin($this->adminUser);

  // Add a section without config so it has a section delta > 1.
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/2/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'section without any lock config',
  ], 'Add section');

  // Add extra sections that have section configuration locked
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/3/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'section with locked section configuration  1',
  ], 'Add section');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/3');
  $this
    ->drupalPostForm(NULL, [
    'layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']' => TRUE,
    'layout_settings[label]' => 'section with locked section configuration  1',
  ], 'Update');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/5/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'section with locked section configuration  2',
  ], 'Add section');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/5');
  $this
    ->drupalPostForm(NULL, [
    'layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']' => TRUE,
    'layout_settings[label]' => 'section with locked section configuration  2',
  ], 'Update');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/6/layout_onecol');
  $this
    ->drupalPostForm(NULL, [
    'layout_settings[label]' => 'section with locked section configuration  3',
  ], 'Add section');
  $this
    ->drupalGet('layout_builder/configure/section/defaults/node.landing_page.default/6');
  $this
    ->drupalPostForm(NULL, [
    'layout_builder_lock[' . LayoutBuilderLock::LOCKED_SECTION_CONFIGURE . ']' => TRUE,
    'layout_settings[label]' => 'section with locked section configuration 3',
  ], 'Update');
  $page
    ->pressButton('Save layout');

  // Create a node.
  $node = $this
    ->drupalCreateNode([
    'type' => 'landing_page',
    'title' => 'Homepage',
  ]);
  $this
    ->drupalLogin($this->editor);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/layout');
  $assert_session
    ->linkNotExists('Configure section above default');
  $assert_session
    ->linkExists('Configure new section in between');
  $assert_session
    ->linkExists('Configure section without any lock config');
  $assert_session
    ->linkNotExists('Configure section with locked configure 1');
  $assert_session
    ->linkExists('Configure Section 5');
  $assert_session
    ->linkNotExists('Configure section with locked configure 2');
  $assert_session
    ->linkNotExists('Configure section with locked configure 3');
}