View source
<?php
namespace Drupal\Tests\block_style_plugins\FunctionalJavascript;
use Drupal\block_content\Entity\BlockContent;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
class LayoutBuilderTest extends WebDriverTestBase {
use ContextualLinkClickTrait;
protected static $modules = [
'node',
'layout_builder',
'block_style_plugins',
'block_style_plugins_test',
'contextual',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$user = $this
->drupalCreateUser([
'configure any layout',
'administer node display',
'administer node fields',
'access contextual links',
]);
$user
->save();
$this
->drupalLogin($user);
$this
->createContentType([
'type' => 'bundle_with_section_field',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
$this
->drupalGet("{$field_ui_prefix}/display/default");
$this
->submitForm([
'layout[enabled]' => TRUE,
'layout[allow_custom]' => TRUE,
], 'Save');
$node = $this
->createNode([
'type' => 'bundle_with_section_field',
'body' => [
[
'value' => 'The node body',
],
],
]);
$node
->save();
}
public function testLayoutBuilderUi() {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$block_css_locator = '.block-system-powered-by-block';
$this
->drupalGet('node/1/layout');
$assert
->pageTextContains('The node body');
$this
->addNewBlock('Powered by Drupal');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$assert
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert
->waitForElementVisible('css', '#drupal-off-canvas'));
$dropdown = $assert
->waitForElementVisible('css', '[name="block_styles"]');
$dropdown
->selectOption('Simple Class');
$page
->pressButton('Add Styles');
$assert
->assertWaitOnAjaxRequest();
$page
->fillField('settings[simple_class]', 'test-class');
$page
->pressButton('Add Styles');
$assert
->assertWaitOnAjaxRequest();
$block_element = $assert
->waitForElementVisible('css', $block_css_locator);
$this
->assertNotEmpty($block_element);
$block_element
->hasClass('test-class');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$assert
->assertWaitOnAjaxRequest();
$this
->clickLink('Edit');
$assert
->assertWaitOnAjaxRequest();
$assert
->fieldValueEquals('settings[simple_class]', 'test-class');
$page
->fillField('settings[simple_class]', 'edited-class');
$page
->pressButton('Update');
$assert
->assertWaitOnAjaxRequest();
$page
->pressButton('Save layout');
$block_element = $assert
->waitForElementVisible('css', $block_css_locator);
$this
->assertNotEmpty($block_element);
$block_element
->hasClass('edited-class');
$this
->drupalGet('node/1/layout');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$assert
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert
->waitForElementVisible('css', '#drupal-off-canvas'));
$this
->clickLink('Delete');
$assert
->assertWaitOnAjaxRequest();
$page
->pressButton('Cancel');
$assert
->assertWaitOnAjaxRequest();
$this
->clickLink('Delete');
$assert
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert
->waitForElementVisible('css', '[value="Confirm"]'));
$page
->pressButton('Confirm');
$assert
->assertWaitOnAjaxRequest();
$assert
->responseNotContains('edited-class');
$page
->pressButton('Save layout');
$assert
->responseContains('The layout override has been saved');
$assert
->responseNotContains('edited-class');
}
public function testLayoutBuilderBlockVisibility() {
$assert = $this
->assertSession();
$block_css_locator = '.block-system-powered-by-block';
$this
->drupalGet('node/1/layout');
$this
->addNewBlock('Powered by Drupal');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Styles Created by Yaml');
$assert
->pageTextContains('Dropdown with Include');
$assert
->pageTextNotContains('Checkbox with Exclude');
}
public function testLayoutBuilderCustomBlockVisibility() {
$assert = $this
->assertSession();
$block = $this
->createBlockContent('Custom Block Test');
$block_css_locator = '.block-block-content' . $block
->uuid();
$this
->drupalGet('node/1/layout');
$this
->addNewBlock('Custom Block Test');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Simple Class');
$assert
->pageTextContains('Dropdown with Include');
$assert
->pageTextNotContains('Checkbox with Exclude');
$assert
->pageTextNotContains('Styles Created by Yaml');
}
public function testLayoutBuilderTemplateSet() {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$block_css_locator = '.block-system-powered-by-block';
$this
->drupalGet('node/1/layout');
$this
->addNewBlock('Powered by Drupal');
$this
->clickContextualLink($block_css_locator, 'Style settings');
$dropdown = $assert
->waitForElementVisible('css', '[name="block_styles"]');
$dropdown
->selectOption('Template Set by Yaml');
$page
->pressButton('Add Style');
$assert
->assertWaitOnAjaxRequest();
$page
->fillField('settings[test_field]', 'test-class');
$page
->pressButton('Add Styles');
$assert
->assertWaitOnAjaxRequest();
$assert
->responseContains('This is a custom template');
$page
->pressButton('Save layout');
$assert
->responseContains('This is a custom template');
}
public function testLayoutBuilderEmbedForm() {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$block_css_locator = '.block-system-breadcrumb-block';
$this
->drupalGet('node/1/layout');
$this
->clickLink('Add block');
$assert
->assertWaitOnAjaxRequest();
$this
->clickLink('Breadcrumbs');
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Embed on Layout Builder Form');
$assert
->pageTextNotContains('Simple Class');
$page
->fillField('third_party_settings[block_style_plugins][embed_on_layout_builder][test_embedded_field]', 'test-class');
$page
->pressButton('Add block');
$assert
->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert
->assertWaitOnAjaxRequest();
$block_element = $assert
->waitForElementVisible('css', $block_css_locator);
$block_element
->hasClass('test-class');
}
protected function addNewBlock($title) {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->clickLink('Add block');
$assert
->assertWaitOnAjaxRequest();
$this
->clickLink($title);
$assert
->assertWaitOnAjaxRequest();
$page
->pressButton('Add block');
$assert
->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert
->assertWaitOnAjaxRequest();
}
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = $title ?: $this
->randomMachineName();
$block_content = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
]);
if ($block_content && $save === TRUE) {
$block_content
->save();
}
return $block_content;
}
}