You are here

public function MoveBlockRestrictionTest::testMoveContentBlock in Layout Builder Restrictions 8.2

Tests moving a content block.

File

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

Class

MoveBlockRestrictionTest
Tests moving blocks via the form.

Namespace

Drupal\Tests\layout_builder_restrictions\FunctionalJavascript

Code

public function testMoveContentBlock() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $blocks = $this
    ->generateTestBlocks();
  $node_id = $this
    ->generateTestNode();
  $this
    ->navigateToManageDisplay();
  $page
    ->clickLink('Manage layout');

  // 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');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Add Basic Block 1 to the 'first' region.
  $assert_session
    ->elementExists('css', '[data-layout-delta="0"].layout--twocol-section [data-region="first"] .layout-builder__add-block')
    ->click();
  $this
    ->assertNotEmpty($assert_session
    ->waitForText('Basic Block 1'));
  $page
    ->clickLink('Basic Block 1');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Add block');
  $this
    ->waitForNoElement('#drupal-off-canvas');

  // Add Alternate Block 1 to the 'first' region.
  $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("Alternate Block 1")'));
  $page
    ->clickLink('Alternate Block 1');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Add block');
  $this
    ->waitForNoElement('#drupal-off-canvas');
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Restrict all Custom blocks.
  $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-custom-blocks-restriction-all"]');
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-custom-blocks-restriction-all');
  $assert_session
    ->checkboxNotChecked('edit-layout-builder-restrictions-allowed-blocks-custom-blocks-restriction-whitelisted');
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-blocks-restriction-whitelisted"]');
  $element
    ->click();
  $page
    ->pressButton('Save');
  $page
    ->clickLink('Manage layout');
  $expected_block_order = [
    '.block-block-content' . $blocks['Basic Block 1'],
    '.block-block-content' . $blocks['Alternate Block 1'],
  ];
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order);
  $this
    ->navigateToManageDisplay();
  $page
    ->clickLink('Manage layout');

  // Attempt to reorder Alternate Block 1.
  $this
    ->openMoveForm(0, 'first', 'block-block-content' . $blocks['Alternate Block 1'], [
    'Basic Block 1',
    'Alternate Block 1 (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Alternate Block 1', [
    'Alternate Block 1 (current)*',
    'Basic Block 1',
  ]);
  $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 Alternate Block 1 placement in the layout_twocol_section first 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();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save layout');
  $page
    ->clickLink('Manage layout');

  // The order should not have changed after save.
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order);

  // Allow Alternate Block, but not Basic block.
  $this
    ->navigateToManageDisplay();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary');
  $element
    ->click();

  // Do not apply individual block level restrictions.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-blocks-restriction-all"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-block-types-restriction-whitelisted"]');
  $element
    ->click();

  // Whitelist all "Alternate" block types.
  $page
    ->checkField('layout_builder_restrictions[allowed_blocks][Custom block types][available_blocks][alternate]');
  $page
    ->pressButton('Save');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Reorder Alternate block.
  $page
    ->clickLink('Manage layout');
  $expected_block_order_moved = [
    '.block-block-content' . $blocks['Alternate Block 1'],
    '.block-block-content' . $blocks['Basic Block 1'],
  ];
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order);
  $this
    ->openMoveForm(0, 'first', 'block-block-content' . $blocks['Alternate Block 1'], [
    'Basic Block 1',
    'Alternate Block 1 (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Alternate Block 1', [
    'Alternate Block 1 (current)*',
    'Basic Block 1',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order_moved);

  // Demonstrate that Basic block types are still restricted.
  $this
    ->openMoveForm(0, 'first', 'block-block-content' . $blocks['Basic Block 1'], [
    'Alternate Block 1',
    'Basic Block 1 (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Basic Block 1', [
    'Basic Block 1 (current)*',
    'Alternate Block 1',
  ]);
  $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 Basic Block 1 placement in the layout_twocol_section first 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();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save layout');
  $page
    ->clickLink('Manage layout');

  // Allow all Custom block types.
  $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-custom-block-types-restriction-all"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-blocks-restriction-all"]');
  $element
    ->click();
  $page
    ->pressButton('Save');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Reorder both Alternate & Basic block block.
  $page
    ->clickLink('Manage layout');
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order_moved);
  $this
    ->openMoveForm(0, 'first', 'block-block-content' . $blocks['Basic Block 1'], [
    'Alternate Block 1',
    'Basic Block 1 (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Basic Block 1', [
    'Basic Block 1 (current)*',
    'Alternate Block 1',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $modal = $page
    ->find('css', '#drupal-off-canvas p');
  $this
    ->assertNull($modal);
  $page
    ->pressButton('Save layout');

  // Reorder Alternate block.
  $page
    ->clickLink('Manage layout');
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order);
  $this
    ->openMoveForm(0, 'first', 'block-block-content' . $blocks['Alternate Block 1'], [
    'Basic Block 1',
    'Alternate Block 1 (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Alternate Block 1', [
    'Alternate Block 1 (current)*',
    'Basic Block 1',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save layout');
  $page
    ->clickLink('Manage layout');
  $this
    ->assertRegionBlocksOrder(0, 'first', $expected_block_order_moved);
}