You are here

public function MoveBlockRestrictionTest::testMovePluginBlock in Layout Builder Restrictions 8.2

Tests moving a plugin block.

File

tests/src/FunctionalJavascript/MoveBlockRestrictionTest.php, line 130

Class

MoveBlockRestrictionTest
Tests moving blocks via the form.

Namespace

Drupal\Tests\layout_builder_restrictions\FunctionalJavascript

Code

public function testMovePluginBlock() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->navigateToManageDisplay();
  $page
    ->clickLink('Manage layout');
  $expected_block_order = [
    '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    '.block-field-blocknodebundle-with-section-fieldbody',
  ];
  $this
    ->assertRegionBlocksOrder(0, 'content', $expected_block_order);

  // Add a top section using the Two column layout.
  $page
    ->clickLink('Add section');
  $assert_session
    ->waitForElementVisible('css', '#drupal-off-canvas');
  $page
    ->clickLink('Two column');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'input[value="Add section"]'));
  $page
    ->pressButton('Add section');
  $this
    ->assertRegionBlocksOrder(1, 'content', $expected_block_order);

  // Add a 'Powered by Drupal' block in the 'first' region of the new section.
  $first_region_block_locator = '[data-layout-delta="0"].layout--twocol-section [data-region="first"] [data-layout-block-uuid]';
  $assert_session
    ->elementNotExists('css', $first_region_block_locator);
  $assert_session
    ->elementExists('css', '[data-layout-delta="0"].layout--twocol-section [data-region="first"] .layout-builder__add-block')
    ->click();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', '#drupal-off-canvas a:contains("Powered by Drupal")'));
  $page
    ->clickLink('Powered by Drupal');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'input[value="Add block"]'));
  $page
    ->pressButton('Add block');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', $first_region_block_locator));

  // Ensure the request has completed before the test starts.
  $this
    ->waitForNoElement('#drupal-off-canvas');
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Add a block restriction after the fact to test basic restriction.
  // Restrict all 'Content' fields from options.
  $this
    ->navigateToManageDisplay();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all"]');
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all');
  $assert_session
    ->checkboxNotChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-whitelisted');
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-whitelisted"]');
  $element
    ->click();
  $page
    ->pressButton('Save');
  $page
    ->clickLink('Manage layout');
  $expected_block_order_1 = [
    '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    '.block-field-blocknodebundle-with-section-fieldbody',
  ];
  $this
    ->assertRegionBlocksOrder(1, 'content', $expected_block_order_1);

  // Attempt to reorder body field in current region.
  $this
    ->openMoveForm(1, 'content', 'block-field-blocknodebundle-with-section-fieldbody', [
    'Links',
    'Body (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Body (current)', [
    'Body (current)*',
    'Links',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Verify that a validation error is provided.
  $modal = $page
    ->find('css', '#drupal-off-canvas p');
  $this
    ->assertSame("There is a restriction on Body placement in the layout_onecol content region for bundle_with_section_field content.", trim($modal
    ->getText()));
  $dialog_div = $this
    ->assertSession()
    ->waitForElementVisible('css', 'div.ui-dialog');
  $close_button = $dialog_div
    ->findButton('Close');
  $this
    ->assertNotNull($close_button);
  $close_button
    ->press();
  $page
    ->pressButton('Save layout');
  $page
    ->clickLink('Manage layout');

  // The order should not have changed after save.
  $this
    ->assertRegionBlocksOrder(1, 'content', $expected_block_order_1);
  $this
    ->navigateToManageDisplay();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all"]');
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all');
  $assert_session
    ->checkboxNotChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-whitelisted');
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-whitelisted"]');
  $element
    ->click();
  $page
    ->pressButton('Save');
  $this
    ->navigateToManageDisplay();
  $page
    ->clickLink('Manage layout');

  // Move the body block into the first region above existing block.
  $this
    ->openMoveForm(1, 'content', 'block-field-blocknodebundle-with-section-fieldbody', [
    'Links',
    'Body (current)',
  ]);
  $page
    ->selectFieldOption('Region', '0:first');
  $this
    ->assertBlockTable([
    'Powered by Drupal',
    'Body (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Body', [
    'Body (current)*',
    'Powered by Drupal',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $modal = $page
    ->find('css', '#drupal-off-canvas p');

  // Content cannot be moved between sections if a restriction exists.
  $this
    ->assertSame("There is a restriction on Body placement in the layout_twocol_section first region for bundle_with_section_field content.", trim($modal
    ->getText()));
}