You are here

public function LayoutBuilderTest::testLayoutBuilderUi in Drupal 10

Same name in this branch
  1. 10 core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderTest::testLayoutBuilderUi()
  2. 10 core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\LayoutBuilderTest::testLayoutBuilderUi()
Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderTest::testLayoutBuilderUi()
  2. 9 core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderTest::testLayoutBuilderUi()

File

core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php, line 281

Class

LayoutBuilderTest
Tests the Layout Builder UI.

Namespace

Drupal\Tests\layout_builder\Functional

Code

public function testLayoutBuilderUi() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'configure any layout',
    'administer node display',
    'administer node fields',
  ]));
  $this
    ->drupalGet('node/1');
  $assert_session
    ->elementNotExists('css', '.layout-builder-block');
  $assert_session
    ->pageTextContains('The first node body');
  $assert_session
    ->pageTextNotContains('Powered by Drupal');
  $assert_session
    ->linkNotExists('Layout');
  $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';

  // From the manage display page, go to manage the layout.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $assert_session
    ->linkNotExists('Manage layout');
  $assert_session
    ->fieldDisabled('layout[allow_custom]');
  $this
    ->submitForm([
    'layout[enabled]' => TRUE,
  ], 'Save');
  $assert_session
    ->linkExists('Manage layout');
  $this
    ->clickLink('Manage layout');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default/layout");
  $assert_session
    ->elementTextContains('css', '.layout-builder__message.layout-builder__message--defaults', 'You are editing the layout template for all Bundle with section field content items.');

  // The body field is only present once.
  $assert_session
    ->elementsCount('css', '.field--name-body', 1);

  // The extra field is only present once.
  $assert_session
    ->pageTextContainsOnce('Placeholder for the "Extra label" field');

  // Blocks have layout builder specific block class.
  $assert_session
    ->elementExists('css', '.layout-builder-block');

  // Save the defaults.
  $page
    ->pressButton('Save layout');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default");

  // Load the default layouts again after saving to confirm fields are only
  // added on new layouts.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $assert_session
    ->linkExists('Manage layout');
  $this
    ->clickLink('Manage layout');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default/layout");

  // The body field is only present once.
  $assert_session
    ->elementsCount('css', '.field--name-body', 1);

  // The extra field is only present once.
  $assert_session
    ->pageTextContainsOnce('Placeholder for the "Extra label" field');

  // Add a new block.
  $assert_session
    ->linkExists('Add block');
  $this
    ->clickLink('Add block');
  $assert_session
    ->linkExists('Powered by Drupal');
  $this
    ->clickLink('Powered by Drupal');
  $page
    ->fillField('settings[label]', 'This is the label');
  $page
    ->checkField('settings[label_display]');
  $page
    ->pressButton('Add block');
  $assert_session
    ->pageTextContains('Powered by Drupal');
  $assert_session
    ->pageTextContains('This is the label');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default/layout");

  // Save the defaults.
  $page
    ->pressButton('Save layout');
  $assert_session
    ->pageTextContains('The layout has been saved.');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default");

  // The node uses the defaults, no overrides available.
  $this
    ->drupalGet('node/1');
  $assert_session
    ->pageTextContains('The first node body');
  $assert_session
    ->pageTextContains('Powered by Drupal');
  $assert_session
    ->pageTextContains('Extra, Extra read all about it.');
  $assert_session
    ->pageTextNotContains('Placeholder for the "Extra label" field');
  $assert_session
    ->linkNotExists('Layout');

  // Enable overrides.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $this
    ->submitForm([
    'layout[allow_custom]' => TRUE,
  ], 'Save');
  $this
    ->drupalGet('node/1');

  // Remove the section from the defaults.
  $assert_session
    ->linkExists('Layout');
  $this
    ->clickLink('Layout');
  $assert_session
    ->pageTextContains('Placeholder for the "Extra label" field');
  $assert_session
    ->linkExists('Remove Section 1');
  $this
    ->clickLink('Remove Section 1');
  $page
    ->pressButton('Remove');

  // Add a new section.
  $this
    ->clickLink('Add section');
  $this
    ->assertCorrectLayouts();
  $assert_session
    ->linkExists('Two column');
  $this
    ->clickLink('Two column');
  $assert_session
    ->buttonExists('Add section');
  $page
    ->pressButton('Add section');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextNotContains('The first node body');
  $assert_session
    ->pageTextNotContains('Powered by Drupal');
  $assert_session
    ->pageTextNotContains('Extra, Extra read all about it.');
  $assert_session
    ->pageTextNotContains('Placeholder for the "Extra label" field');

  // Assert that overrides cannot be turned off while overrides exist.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $assert_session
    ->checkboxChecked('layout[allow_custom]');
  $assert_session
    ->fieldDisabled('layout[allow_custom]');

  // Alter the defaults.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default/layout");
  $assert_session
    ->linkExists('Add block');
  $this
    ->clickLink('Add block');
  $assert_session
    ->linkExists('Title');
  $this
    ->clickLink('Title');
  $page
    ->pressButton('Add block');

  // The title field is present.
  $assert_session
    ->elementExists('css', '.field--name-title');
  $page
    ->pressButton('Save layout');

  // View the other node, which is still using the defaults.
  $this
    ->drupalGet('node/2');
  $assert_session
    ->pageTextContains('The second node title');
  $assert_session
    ->pageTextContains('The second node body');
  $assert_session
    ->pageTextContains('Powered by Drupal');
  $assert_session
    ->pageTextContains('Extra, Extra read all about it.');
  $assert_session
    ->pageTextNotContains('Placeholder for the "Extra label" field');

  // The overridden node does not pick up the changes to defaults.
  $this
    ->drupalGet('node/1');
  $assert_session
    ->elementNotExists('css', '.field--name-title');
  $assert_session
    ->pageTextNotContains('The first node body');
  $assert_session
    ->pageTextNotContains('Powered by Drupal');
  $assert_session
    ->pageTextNotContains('Extra, Extra read all about it.');
  $assert_session
    ->pageTextNotContains('Placeholder for the "Extra label" field');
  $assert_session
    ->linkExists('Layout');

  // Reverting the override returns it to the defaults.
  $this
    ->clickLink('Layout');
  $assert_session
    ->linkExists('Add block');
  $this
    ->clickLink('Add block');
  $assert_session
    ->linkExists('ID');
  $this
    ->clickLink('ID');
  $page
    ->pressButton('Add block');

  // The title field is present.
  $assert_session
    ->elementExists('css', '.field--name-nid');
  $assert_session
    ->pageTextContains('ID');
  $assert_session
    ->pageTextContains('1');
  $page
    ->pressButton('Revert to defaults');
  $page
    ->pressButton('Revert');
  $assert_session
    ->addressEquals('node/1');
  $assert_session
    ->pageTextContains('The layout has been reverted back to defaults.');
  $assert_session
    ->elementExists('css', '.field--name-title');
  $assert_session
    ->elementNotExists('css', '.field--name-nid');
  $assert_session
    ->pageTextContains('The first node body');
  $assert_session
    ->pageTextContains('Powered by Drupal');
  $assert_session
    ->pageTextContains('Extra, Extra read all about it.');

  // Assert that overrides can be turned off now that all overrides are gone.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $this
    ->submitForm([
    'layout[allow_custom]' => FALSE,
  ], 'Save');
  $this
    ->drupalGet('node/1');
  $assert_session
    ->linkNotExists('Layout');

  // Add a new field.
  $edit = [
    'new_storage_type' => 'string',
    'label' => 'My text field',
    'field_name' => 'my_text',
  ];
  $this
    ->drupalGet("{$field_ui_prefix}/fields/add-field");
  $this
    ->submitForm($edit, 'Save and continue');
  $page
    ->pressButton('Save field settings');
  $page
    ->pressButton('Save settings');
  $this
    ->drupalGet("{$field_ui_prefix}/display/default/layout");
  $assert_session
    ->pageTextContains('My text field');
  $assert_session
    ->elementExists('css', '.field--name-field-my-text');

  // Delete the field.
  $this
    ->drupalGet("{$field_ui_prefix}/fields/node.bundle_with_section_field.field_my_text/delete");
  $this
    ->submitForm([], 'Delete');
  $this
    ->drupalGet("{$field_ui_prefix}/display/default/layout");
  $assert_session
    ->pageTextNotContains('My text field');
  $assert_session
    ->elementNotExists('css', '.field--name-field-my-text');
  $this
    ->clickLink('Add section');
  $this
    ->clickLink('One column');
  $page
    ->fillField('layout_settings[label]', 'My Cool Section');
  $page
    ->pressButton('Add section');
  $expected_labels = [
    'My Cool Section',
    'Content region in My Cool Section',
    'Section 2',
    'Content region in Section 2',
  ];
  $labels = [];
  foreach ($page
    ->findAll('css', '[role="group"]') as $element) {
    $labels[] = $element
      ->getAttribute('aria-label');
  }
  $this
    ->assertSame($expected_labels, $labels);
}