d75636564fecb58f3fec40d598f77661a7386962
[yaffs-website] / web / core / modules / layout_builder / tests / src / FunctionalJavascript / LayoutBuilderOptInTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\FunctionalJavascript;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Tests the ability for opting in and out of Layout Builder.
9  *
10  * @group layout_builder
11  */
12 class LayoutBuilderOptInTest extends WebDriverTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected static $modules = [
18     'node',
19     'field_ui',
20     'block',
21   ];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
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');
32
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']);
36     $this->rebuildAll();
37     $this->createContentType(['type' => 'after']);
38
39     $this->drupalLogin($this->drupalCreateUser([
40       'configure any layout',
41       'administer node display',
42     ]));
43   }
44
45   /**
46    * Tests the interaction between the two layout checkboxes.
47    */
48   public function testCheckboxLogic() {
49     $assert_session = $this->assertSession();
50     $page = $this->getSession()->getPage();
51
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());
58
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]');
66
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]');
72
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]');
79
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]');
86   }
87
88   /**
89    * Tests the expected default values for enabling Layout Builder.
90    */
91   public function testDefaultValues() {
92     $assert_session = $this->assertSession();
93     $page = $this->getSession()->getPage();
94
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]');
99
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');
105
106     $layout_builder_ui = $this->getPathForFieldBlock('node', 'after', 'default', 'body');
107
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);
112
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');
121
122     $this->drupalGet($layout_builder_ui);
123     $assert_session->fieldValueEquals('settings[formatter][type]', 'text_trimmed');
124
125     // Disable Layout Builder.
126     $this->drupalPostForm($field_ui_prefix, ['layout[enabled]' => FALSE], 'Save');
127     $page->pressButton('Confirm');
128
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.');
132
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');
136
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');
142
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);
149
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');
153   }
154
155   /**
156    * Returns the path to update a field block in the UI.
157    *
158    * @param string $entity_type_id
159    *   The entity type ID.
160    * @param string $bundle
161    *   The bundle.
162    * @param string $view_mode
163    *   The view mode.
164    * @param string $field_name
165    *   The field name.
166    *
167    * @return string
168    *   The path.
169    */
170   protected function getPathForFieldBlock($entity_type_id, $bundle, $view_mode, $field_name) {
171     $delta = 0;
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;
178       }
179     }
180     $this->assertNotNull($body_component);
181     return 'layout_builder/update/block/defaults/node.after.default/0/content/' . $body_component->getUuid();
182   }
183
184 }