You are here

public function BlockPlacementBlacklistTest::testBlockRestrictionStorage in Layout Builder Restrictions 8.2

Verify that both tempstore and config storage function correctly.

File

modules/layout_builder_restrictions_by_region/tests/src/FunctionalJavascript/BlockPlacementBlacklistTest.php, line 62

Class

BlockPlacementBlacklistTest
Demonstrate that blocks can be individually restricted.

Namespace

Drupal\Tests\layout_builder_restrictions_by_region\FunctionalJavascript

Code

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

  // Only allow two-column layout.
  $this
    ->navigateToManageDisplay();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-restricted"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layouts-layout-twocol-section"]');
  $element
    ->click();

  // Verify form behavior when restriction is applied to all regions.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary');
  $element
    ->click();
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-all');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="all_regions"]', 'All regions');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="all_regions"]', 'Unrestricted');

  // Verify form behavior when restriction is applied on a per-region basis.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-per-region"]');
  $element
    ->click();
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-per-region');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'First');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'Unrestricted');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Second');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Unrestricted');

  // Test temporary storage.
  // Add restriction to First region.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]//a');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->checkboxChecked('Allow all existing & new Content fields blocks.');
  $assert_session
    ->checkboxNotChecked('Allow specific Content fields blocks:');

  // Restrict all 'Content' fields from options.
  $element = $page
    ->find('xpath', '//*[starts-with(@id,"edit-allowed-blocks-content-fields-restriction-whitelisted--")]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[starts-with(@id,"edit-submit--")]');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Verify First region is 'Restricted' and Second region
  // remains 'Unrestricted'.
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'Restricted');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Unrestricted');

  // Reload First region allowed block form to verify temp storage.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]//a');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->checkboxNotChecked('Allow all existing & new Content fields blocks.');
  $assert_session
    ->checkboxChecked('Allow specific Content fields blocks:');
  $page
    ->pressButton('Close');

  // Load Second region allowed block form to verify temp storage.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]//a');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->checkboxChecked('Allow all existing & new Content fields blocks.');
  $assert_session
    ->checkboxNotChecked('Allow specific Content fields blocks:');
  $page
    ->pressButton('Close');

  // Verify All Regions unaffected.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-all"]');
  $element
    ->click();
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="all_regions"]', 'Unrestricted');
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="all_regions"]//a');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->checkboxChecked('Allow all existing & new Content fields blocks.');
  $assert_session
    ->checkboxNotChecked('Allow specific Content fields blocks:');
  $page
    ->pressButton('Close');

  // Switch back to Per-region restrictions prior to saving.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-per-region"]');
  $element
    ->click();

  // Allow one-column layout.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layouts-layout-onecol"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol"]/summary');
  $element
    ->click();
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol-table"]/tbody/tr[@data-region="all_regions"]', 'Unrestricted');

  // Save to config.
  $page
    ->pressButton('Save');

  // Verify no block restrictions bleed to other layouts/regions upon save
  // to database.
  $this
    ->navigateToManageDisplay();

  // Check two-column layout.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary');
  $element
    ->click();
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'Restricted');
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Unrestricted');

  // Verify All Regions unaffected.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-all"]');
  $element
    ->click();
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="all_regions"]', 'Unrestricted');

  // Check one-column layout.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol"]/summary');
  $element
    ->click();
  $assert_session
    ->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol-table"]/tbody/tr[@data-region="all_regions"]', 'Unrestricted');
}