View source
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class LayoutBuilderOptInTest extends WebDriverTestBase {
protected static $modules = [
'node',
'field_ui',
'block',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->createContentType([
'type' => 'before',
]);
$this->container
->get('module_installer')
->install([
'layout_builder',
]);
$this
->rebuildAll();
$this
->createContentType([
'type' => 'after',
]);
$this
->drupalLogin($this
->drupalCreateUser([
'configure any layout',
'administer node display',
]));
}
public function testCheckboxLogic() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('admin/structure/types/manage/before/display/default');
$assert_session
->checkboxNotChecked('layout[enabled]');
$assert_session
->checkboxNotChecked('layout[allow_custom]');
$assert_session
->fieldDisabled('layout[allow_custom]');
$this
->assertFalse($page
->findField('layout[allow_custom]')
->isVisible());
$page
->checkField('layout[enabled]');
$assert_session
->checkboxNotChecked('layout[allow_custom]');
$this
->assertTrue($page
->findField('layout[allow_custom]')
->isVisible());
$page
->pressButton('Save');
$assert_session
->checkboxChecked('layout[enabled]');
$assert_session
->checkboxNotChecked('layout[allow_custom]');
$page
->checkField('layout[allow_custom]');
$page
->pressButton('Save');
$assert_session
->checkboxChecked('layout[enabled]');
$assert_session
->checkboxChecked('layout[allow_custom]');
$page
->uncheckField('layout[enabled]');
$page
->pressButton('Save');
$page
->pressButton('Confirm');
$assert_session
->checkboxNotChecked('layout[enabled]');
$assert_session
->checkboxNotChecked('layout[allow_custom]');
$page
->checkField('layout[enabled]');
$page
->checkField('layout[allow_custom]');
$page
->pressButton('Save');
$assert_session
->checkboxChecked('layout[enabled]');
$assert_session
->checkboxChecked('layout[allow_custom]');
}
public function testDefaultValues() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('admin/structure/types/manage/before/display/default');
$assert_session
->checkboxNotChecked('layout[enabled]');
$field_ui_prefix = 'admin/structure/types/manage/after/display/default';
$this
->drupalGet($field_ui_prefix);
$assert_session
->checkboxNotChecked('layout[enabled]');
$page
->checkField('layout[enabled]');
$page
->pressButton('Save');
$layout_builder_ui = $this
->getPathForFieldBlock('node', 'after', 'default', 'body');
$assert_session
->linkExists('Manage layout');
$this
->clickLink('Manage layout');
$assert_session
->elementsCount('css', '.field--name-body', 1);
$this
->drupalGet($layout_builder_ui);
$assert_session
->fieldValueEquals('settings[formatter][type]', 'text_default');
$page
->selectFieldOption('settings[formatter][type]', 'text_trimmed');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Update');
$page
->pressButton('Save layout');
$this
->drupalGet($layout_builder_ui);
$assert_session
->fieldValueEquals('settings[formatter][type]', 'text_trimmed');
$this
->drupalGet($field_ui_prefix);
$this
->submitForm([
'layout[enabled]' => FALSE,
], 'Save');
$page
->pressButton('Confirm');
$this
->drupalGet($layout_builder_ui);
$assert_session
->pageTextContains('You are not authorized to access this page.');
$this
->drupalGet($field_ui_prefix);
$assert_session
->fieldValueEquals('fields[body][type]', 'text_default');
$page
->selectFieldOption('fields[body][type]', 'text_summary_or_trimmed');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->fieldValueEquals('fields[body][type]', 'text_summary_or_trimmed');
$this
->drupalGet($field_ui_prefix);
$this
->submitForm([
'layout[enabled]' => TRUE,
], 'Save');
$assert_session
->linkExists('Manage layout');
$this
->clickLink('Manage layout');
$assert_session
->elementsCount('css', '.field--name-body', 1);
$this
->drupalGet($this
->getPathForFieldBlock('node', 'after', 'default', 'body'));
$assert_session
->fieldValueEquals('settings[formatter][type]', 'text_summary_or_trimmed');
}
protected function getPathForFieldBlock($entity_type_id, $bundle, $view_mode, $field_name) {
$delta = 0;
$display = $this->container
->get('entity_type.manager')
->getStorage('entity_view_display')
->load("{$entity_type_id}.{$bundle}.{$view_mode}");
$body_component = NULL;
foreach ($display
->getSection($delta)
->getComponents() as $component) {
if ($component
->getPluginId() === "field_block:{$entity_type_id}:{$bundle}:{$field_name}") {
$body_component = $component;
}
}
$this
->assertNotNull($body_component);
return 'layout_builder/update/block/defaults/node.after.default/0/content/' . $body_component
->getUuid();
}
}