Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Functional / LayoutBuilderTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests the Layout Builder UI.
9  *
10  * @group layout_builder
11  */
12 class LayoutBuilderTest extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = [
18     'layout_builder',
19     'layout_test',
20     'block',
21     'node',
22   ];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     // @todo The Layout Builder UI relies on local tasks; fix in
31     //   https://www.drupal.org/project/drupal/issues/2917777.
32     $this->drupalPlaceBlock('local_tasks_block');
33
34     // Create two nodes.
35     $this->createContentType(['type' => 'bundle_with_section_field']);
36     $this->createNode([
37       'type' => 'bundle_with_section_field',
38       'title' => 'The first node title',
39       'body' => [
40         [
41           'value' => 'The first node body',
42         ],
43       ],
44     ]);
45     $this->createNode([
46       'type' => 'bundle_with_section_field',
47       'title' => 'The second node title',
48       'body' => [
49         [
50           'value' => 'The second node body',
51         ],
52       ],
53     ]);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function testLayoutBuilderUi() {
60     $assert_session = $this->assertSession();
61     $page = $this->getSession()->getPage();
62
63     $this->drupalLogin($this->drupalCreateUser([
64       'configure any layout',
65       'administer node display',
66       'administer node fields',
67     ]));
68
69     $this->drupalGet('node/1');
70     $assert_session->pageTextContains('The first node body');
71     $assert_session->pageTextNotContains('Powered by Drupal');
72     $assert_session->linkNotExists('Layout');
73
74     $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
75
76     // From the manage display page, go to manage the layout.
77     $this->drupalGet("$field_ui_prefix/display/default");
78     $assert_session->linkExists('Manage layout');
79     $this->clickLink('Manage layout');
80     $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
81     // The body field is present.
82     $assert_session->elementExists('css', '.field--name-body');
83
84     // Add a new block.
85     $assert_session->linkExists('Add Block');
86     $this->clickLink('Add Block');
87     $assert_session->linkExists('Powered by Drupal');
88     $this->clickLink('Powered by Drupal');
89     $page->fillField('settings[label]', 'This is the label');
90     $page->checkField('settings[label_display]');
91     $page->pressButton('Add Block');
92     $assert_session->pageTextContains('Powered by Drupal');
93     $assert_session->pageTextContains('This is the label');
94     $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
95
96     // Save the defaults.
97     $assert_session->linkExists('Save Layout');
98     $this->clickLink('Save Layout');
99     $assert_session->addressEquals("$field_ui_prefix/display/default");
100
101     // The node uses the defaults, no overrides available.
102     $this->drupalGet('node/1');
103     $assert_session->pageTextContains('The first node body');
104     $assert_session->pageTextContains('Powered by Drupal');
105     $assert_session->linkNotExists('Layout');
106
107     // Enable overrides.
108     $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');
109     $this->drupalGet('node/1');
110
111     // Remove the section from the defaults.
112     $assert_session->linkExists('Layout');
113     $this->clickLink('Layout');
114     $assert_session->linkExists('Remove section');
115     $this->clickLink('Remove section');
116     $page->pressButton('Remove');
117
118     // Add a new section.
119     $this->clickLink('Add Section');
120     $assert_session->linkExists('Two column');
121     $this->clickLink('Two column');
122     $assert_session->linkExists('Save Layout');
123     $this->clickLink('Save Layout');
124     $assert_session->pageTextNotContains('The first node body');
125     $assert_session->pageTextNotContains('Powered by Drupal');
126
127     // Assert that overrides cannot be turned off while overrides exist.
128     $this->drupalGet("$field_ui_prefix/display/default");
129     $assert_session->fieldDisabled('layout[allow_custom]');
130
131     // Alter the defaults.
132     $this->drupalGet("$field_ui_prefix/display-layout/default");
133     $assert_session->linkExists('Add Block');
134     $this->clickLink('Add Block');
135     $assert_session->linkExists('Title');
136     $this->clickLink('Title');
137     $page->pressButton('Add Block');
138     // The title field is present.
139     $assert_session->elementExists('css', '.field--name-title');
140     $this->clickLink('Save Layout');
141
142     // View the other node, which is still using the defaults.
143     $this->drupalGet('node/2');
144     $assert_session->pageTextContains('The second node title');
145     $assert_session->pageTextContains('The second node body');
146     $assert_session->pageTextContains('Powered by Drupal');
147
148     // The overridden node does not pick up the changes to defaults.
149     $this->drupalGet('node/1');
150     $assert_session->elementNotExists('css', '.field--name-title');
151     $assert_session->pageTextNotContains('The first node body');
152     $assert_session->pageTextNotContains('Powered by Drupal');
153     $assert_session->linkExists('Layout');
154
155     // Reverting the override returns it to the defaults.
156     $this->clickLink('Layout');
157     $assert_session->linkExists('Revert to defaults');
158     $this->clickLink('Revert to defaults');
159     $page->pressButton('Revert');
160     $assert_session->pageTextContains('The layout has been reverted back to defaults.');
161     $assert_session->elementExists('css', '.field--name-title');
162     $assert_session->pageTextContains('The first node body');
163     $assert_session->pageTextContains('Powered by Drupal');
164
165     // Assert that overrides can be turned off now that all overrides are gone.
166     $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => FALSE], 'Save');
167     $this->drupalGet('node/1');
168     $assert_session->linkNotExists('Layout');
169
170     // Add a new field.
171     $edit = [
172       'new_storage_type' => 'string',
173       'label' => 'My text field',
174       'field_name' => 'my_text',
175     ];
176     $this->drupalPostForm("$field_ui_prefix/fields/add-field", $edit, 'Save and continue');
177     $page->pressButton('Save field settings');
178     $page->pressButton('Save settings');
179     $this->drupalGet("$field_ui_prefix/display-layout/default");
180     $assert_session->pageTextContains('My text field');
181     $assert_session->elementExists('css', '.field--name-field-my-text');
182
183     // Delete the field.
184     $this->drupalPostForm("$field_ui_prefix/fields/node.bundle_with_section_field.field_my_text/delete", [], 'Delete');
185     $this->drupalGet("$field_ui_prefix/display-layout/default");
186     $assert_session->pageTextNotContains('My text field');
187     $assert_session->elementNotExists('css', '.field--name-field-my-text');
188   }
189
190   /**
191    * Tests that component's dependencies are respected during removal.
192    */
193   public function testPluginDependencies() {
194     $assert_session = $this->assertSession();
195     $page = $this->getSession()->getPage();
196
197     $this->container->get('module_installer')->install(['menu_ui']);
198     $this->drupalLogin($this->drupalCreateUser([
199       'configure any layout',
200       'administer node display',
201       'administer menu',
202     ]));
203
204     // Create a new menu.
205     $this->drupalGet('admin/structure/menu/add');
206     $page->fillField('label', 'My Menu');
207     $page->fillField('id', 'mymenu');
208     $page->pressButton('Save');
209     $this->drupalGet('admin/structure/menu/add');
210     $page->fillField('label', 'My Menu');
211     $page->fillField('id', 'myothermenu');
212     $page->pressButton('Save');
213
214     $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display-layout/default');
215     $assert_session->linkExists('Add Section');
216     $this->clickLink('Add Section');
217     $assert_session->linkExists('Layout plugin (with dependencies)');
218     $this->clickLink('Layout plugin (with dependencies)');
219     $assert_session->elementExists('css', '.layout--layout-test-dependencies-plugin');
220     $assert_session->elementExists('css', '.field--name-body');
221     $assert_session->linkExists('Save Layout');
222     $this->clickLink('Save Layout');
223     $this->drupalPostForm('admin/structure/menu/manage/myothermenu/delete', [], 'Delete');
224     $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display-layout/default');
225     $assert_session->elementNotExists('css', '.layout--layout-test-dependencies-plugin');
226     $assert_session->elementExists('css', '.field--name-body');
227
228     // Add a menu block.
229     $assert_session->linkExists('Add Block');
230     $this->clickLink('Add Block');
231     $assert_session->linkExists('My Menu');
232     $this->clickLink('My Menu');
233     $page->pressButton('Add Block');
234
235     // Add another block alongside the menu.
236     $assert_session->linkExists('Add Block');
237     $this->clickLink('Add Block');
238     $assert_session->linkExists('Powered by Drupal');
239     $this->clickLink('Powered by Drupal');
240     $page->pressButton('Add Block');
241
242     // Assert that the blocks are visible, and save the layout.
243     $assert_session->pageTextContains('Powered by Drupal');
244     $assert_session->pageTextContains('My Menu');
245     $assert_session->elementExists('css', '.block.menu--mymenu');
246     $assert_session->linkExists('Save Layout');
247     $this->clickLink('Save Layout');
248
249     // Delete the menu.
250     $this->drupalPostForm('admin/structure/menu/manage/mymenu/delete', [], 'Delete');
251
252     // Ensure that the menu block is gone, but that the other block remains.
253     $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display-layout/default');
254     $assert_session->pageTextContains('Powered by Drupal');
255     $assert_session->pageTextNotContains('My Menu');
256     $assert_session->elementNotExists('css', '.block.menu--mymenu');
257   }
258
259 }