3 namespace Drupal\Tests\layout_builder\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Tests the ability for opting in and out of Layout Builder.
10 * @group layout_builder
12 class LayoutBuilderOptInTest extends WebDriverTestBase {
17 protected static $modules = [
26 protected function setUp() {
29 // @todo The Layout Builder UI relies on local tasks; fix in
30 // https://www.drupal.org/project/drupal/issues/2917777.
31 $this->drupalPlaceBlock('local_tasks_block');
33 // Create one content type before installing Layout Builder and one after.
34 $this->createContentType(['type' => 'before']);
35 $this->container->get('module_installer')->install(['layout_builder']);
37 $this->createContentType(['type' => 'after']);
39 $this->drupalLogin($this->drupalCreateUser([
40 'configure any layout',
41 'administer node display',
46 * Tests the interaction between the two layout checkboxes.
48 public function testCheckboxLogic() {
49 $assert_session = $this->assertSession();
50 $page = $this->getSession()->getPage();
52 $this->drupalGet('admin/structure/types/manage/before/display/default');
53 // Both fields are unchecked and allow_custom is disabled and hidden.
54 $assert_session->checkboxNotChecked('layout[enabled]');
55 $assert_session->checkboxNotChecked('layout[allow_custom]');
56 $assert_session->fieldDisabled('layout[allow_custom]');
57 $this->assertFalse($page->findField('layout[allow_custom]')->isVisible());
59 // Checking is_enable will show allow_custom.
60 $page->checkField('layout[enabled]');
61 $assert_session->checkboxNotChecked('layout[allow_custom]');
62 $this->assertTrue($page->findField('layout[allow_custom]')->isVisible());
63 $page->pressButton('Save');
64 $assert_session->checkboxChecked('layout[enabled]');
65 $assert_session->checkboxNotChecked('layout[allow_custom]');
67 // Check and submit allow_custom.
68 $page->checkField('layout[allow_custom]');
69 $page->pressButton('Save');
70 $assert_session->checkboxChecked('layout[enabled]');
71 $assert_session->checkboxChecked('layout[allow_custom]');
73 // Reset the checkboxes.
74 $page->uncheckField('layout[enabled]');
75 $page->pressButton('Save');
76 $page->pressButton('Confirm');
77 $assert_session->checkboxNotChecked('layout[enabled]');
78 $assert_session->checkboxNotChecked('layout[allow_custom]');
80 // Check both at the same time.
81 $page->checkField('layout[enabled]');
82 $page->checkField('layout[allow_custom]');
83 $page->pressButton('Save');
84 $assert_session->checkboxChecked('layout[enabled]');
85 $assert_session->checkboxChecked('layout[allow_custom]');
89 * Tests the expected default values for enabling Layout Builder.
91 public function testDefaultValues() {
92 $assert_session = $this->assertSession();
93 $page = $this->getSession()->getPage();
95 // Both the content type created before and after Layout Builder was
96 // installed is still using the Field UI.
97 $this->drupalGet('admin/structure/types/manage/before/display/default');
98 $assert_session->checkboxNotChecked('layout[enabled]');
100 $field_ui_prefix = 'admin/structure/types/manage/after/display/default';
101 $this->drupalGet($field_ui_prefix);
102 $assert_session->checkboxNotChecked('layout[enabled]');
103 $page->checkField('layout[enabled]');
104 $page->pressButton('Save');
106 $layout_builder_ui = $this->getPathForFieldBlock('node', 'after', 'default', 'body');
108 $assert_session->linkExists('Manage layout');
109 $this->clickLink('Manage layout');
110 // Ensure the body appears once and only once.
111 $assert_session->elementsCount('css', '.field--name-body', 1);
113 // Change the body formatter to Trimmed.
114 $this->drupalGet($layout_builder_ui);
115 $assert_session->fieldValueEquals('settings[formatter][type]', 'text_default');
116 $page->selectFieldOption('settings[formatter][type]', 'text_trimmed');
117 $assert_session->assertWaitOnAjaxRequest();
118 $page->pressButton('Update');
119 $assert_session->linkExists('Save Layout');
120 $this->clickLink('Save Layout');
122 $this->drupalGet($layout_builder_ui);
123 $assert_session->fieldValueEquals('settings[formatter][type]', 'text_trimmed');
125 // Disable Layout Builder.
126 $this->drupalPostForm($field_ui_prefix, ['layout[enabled]' => FALSE], 'Save');
127 $page->pressButton('Confirm');
129 // The Layout Builder UI is no longer accessible.
130 $this->drupalGet($layout_builder_ui);
131 $assert_session->pageTextContains('You are not authorized to access this page.');
133 // The original body formatter is reflected in Field UI.
134 $this->drupalGet($field_ui_prefix);
135 $assert_session->fieldValueEquals('fields[body][type]', 'text_default');
137 // Change the body formatter to Summary.
138 $page->selectFieldOption('fields[body][type]', 'text_summary_or_trimmed');
139 $assert_session->assertWaitOnAjaxRequest();
140 $page->pressButton('Save');
141 $assert_session->fieldValueEquals('fields[body][type]', 'text_summary_or_trimmed');
143 // Reactivate Layout Builder.
144 $this->drupalPostForm($field_ui_prefix, ['layout[enabled]' => TRUE], 'Save');
145 $assert_session->linkExists('Manage layout');
146 $this->clickLink('Manage layout');
147 // Ensure the body appears once and only once.
148 $assert_session->elementsCount('css', '.field--name-body', 1);
150 // The changed body formatter is reflected in Layout Builder UI.
151 $this->drupalGet($this->getPathForFieldBlock('node', 'after', 'default', 'body'));
152 $assert_session->fieldValueEquals('settings[formatter][type]', 'text_summary_or_trimmed');
156 * Returns the path to update a field block in the UI.
158 * @param string $entity_type_id
159 * The entity type ID.
160 * @param string $bundle
162 * @param string $view_mode
164 * @param string $field_name
170 protected function getPathForFieldBlock($entity_type_id, $bundle, $view_mode, $field_name) {
172 /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display */
173 $display = $this->container->get('entity_type.manager')->getStorage('entity_view_display')->load("$entity_type_id.$bundle.$view_mode");
174 $body_component = NULL;
175 foreach ($display->getSection($delta)->getComponents() as $component) {
176 if ($component->getPluginId() === "field_block:$entity_type_id:$bundle:$field_name") {
177 $body_component = $component;
180 $this->assertNotNull($body_component);
181 return 'layout_builder/update/block/defaults/node.after.default/0/content/' . $body_component->getUuid();