View source
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
class LayoutBuilderUiTest extends WebDriverTestBase {
use ContextualLinkClickTrait;
const FIELD_UI_PREFIX = 'admin/structure/types/manage/bundle_with_section_field';
protected static $modules = [
'layout_builder',
'block',
'field_ui',
'node',
'block_content',
'contextual',
'views',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->createContentType([
'type' => 'bundle_with_section_field',
]);
$this
->drupalLogin($this
->drupalCreateUser([
'configure any layout',
'create and edit custom blocks',
'administer node display',
'administer node fields',
'access contextual links',
]));
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
$this
->submitForm([
'layout[enabled]' => TRUE,
], 'Save');
}
public function testReloadWithNoSections() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$page
->clickLink('Remove Section 1');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Remove');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextNotContains('Remove Section 1');
$assert_session
->pageTextNotContains('Add block');
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$assert_session
->pageTextNotContains('Remove Section 1');
$assert_session
->pageTextNotContains('Add block');
}
public function testUnsavedChangesMessage() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertModifiedLayout(static::FIELD_UI_PREFIX . '/display/default/layout');
$page
->pressButton('Discard changes');
$page
->pressButton('Confirm');
$assert_session
->pageTextNotContains('You have unsaved changes.');
$this
->assertModifiedLayout(static::FIELD_UI_PREFIX . '/display/default/layout');
$page
->pressButton('Save layout');
$assert_session
->pageTextNotContains('You have unsaved changes.');
}
protected function assertModifiedLayout(string $path) : void {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet($path);
$page
->clickLink('Add section');
$assert_session
->waitForElementVisible('named', [
'link',
'One column',
]);
$assert_session
->pageTextNotContains('You have unsaved changes.');
$page
->clickLink('One column');
$assert_session
->waitForElementVisible('named', [
'button',
'Add section',
]);
$page
->pressButton('Add section');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContainsOnce('You have unsaved changes.');
$this
->drupalGet($path);
$assert_session
->pageTextContainsOnce('You have unsaved changes.');
}
public function testAddHighlights() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$bundle = BlockContentType::create([
'id' => 'basic',
'label' => 'Basic block',
'revision' => 1,
]);
$bundle
->save();
block_content_add_body_field($bundle
->id());
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$assert_session
->elementsCount('css', '.layout-builder__add-section', 2);
$assert_session
->elementNotExists('css', '.is-layout-builder-highlighted');
$page
->clickLink('Add section');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '#drupal-off-canvas .item-list'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="section-0"]');
$page
->clickLink('Two column');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas input[type="submit"][value="Add section"]'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="section-0"]');
$page
->pressButton("Add section");
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightNotExists();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '[data-layout-delta="1"]'));
$assert_session
->elementsCount('css', '.layout-builder__add-block', 3);
$page
->clickLink('Add block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'a:contains("Create custom block")'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="block-0-first"]');
$page
->clickLink('Create custom block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas input[value="Add block"]'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="block-0-first"]');
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
$page
->clickLink('Add block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'a:contains("Recent content")'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="block-0-first"]');
$page
->clickLink('Recent content');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas input[value="Add block"]'));
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="block-0-first"]');
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
$page
->clickLink('Configure Section 1');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="section-update-0"]');
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
$page
->clickLink('Remove Section 1');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('[data-layout-builder-highlight-id="section-update-0"]');
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
$this
->clickContextualLink('.block-field-blocknodebundle-with-section-fieldbody', 'Configure');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('.block-field-blocknodebundle-with-section-fieldbody');
$this
->toggleContextualTriggerVisibility('.block-field-blocknodebundle-with-section-fieldbody');
$active_section = $page
->find('css', '.block-field-blocknodebundle-with-section-fieldbody');
$active_section
->pressButton('Open configuration options');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.block-field-blocknodebundle-with-section-fieldbody .contextual.open'));
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
$this
->getSession()
->reload();
$this
->clickContextualLink('.block-field-blocknodebundle-with-section-fieldbody', 'Remove block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHighlightedElement('.block-field-blocknodebundle-with-section-fieldbody');
$page
->pressButton('Close');
$this
->assertHighlightNotExists();
}
public function testNewExtraField() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
\Drupal::service('module_installer')
->install([
'layout_builder_extra_field_test',
]);
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$assert_session
->pageTextContains('New Extra Field');
$this
->clickContextualLink('.block-extra-field-blocknodebundle-with-section-fieldlayout-builder-extra-field-test', 'Remove block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Are you sure you want to remove');
$page
->pressButton('Remove');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextNotContains('New Extra Field');
}
private function assertHighlightedElement(string $selector) : void {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$assert_session
->elementsCount('css', '.is-layout-builder-highlighted', 1);
$this
->assertTrue($page
->find('css', $selector)
->hasClass('is-layout-builder-highlighted'));
}
private function assertHighlightNotExists() : void {
$this
->markTestSkipped("Skipped temporarily for random fails.");
$assert_session = $this
->assertSession();
$assert_session
->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert_session
->assertNoElementAfterWait('css', '.is-layout-builder-highlighted');
}
}