Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / layout_builder / tests / src / Kernel / LayoutBuilderInstallTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Kernel;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
8 use Drupal\layout_builder\Section;
9
10 /**
11  * Ensures that Layout Builder and core EntityViewDisplays are compatible.
12  *
13  * @group layout_builder
14  */
15 class LayoutBuilderInstallTest extends LayoutBuilderCompatibilityTestBase {
16
17   /**
18    * Tests the compatibility of Layout Builder with existing entity displays.
19    */
20   public function testCompatibility() {
21     // Ensure that the fields are shown.
22     $expected_fields = [
23       'field field--name-name field--type-string field--label-hidden field__item',
24       'field field--name-test-field-display-configurable field--type-boolean field--label-above',
25       'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
26       'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
27       'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
28     ];
29     $this->assertFieldAttributes($this->entity, $expected_fields);
30
31     $this->installLayoutBuilder();
32
33     // Without using Layout Builder for an override, the result has not changed.
34     $this->assertFieldAttributes($this->entity, $expected_fields);
35
36     // Add a layout override.
37     $this->enableOverrides();
38     $this->entity = $this->reloadEntity($this->entity);
39     $this->entity->get(OverridesSectionStorage::FIELD_NAME)->appendSection(new Section('layout_onecol'));
40     $this->entity->save();
41
42     // The rendered entity has now changed. The non-configurable field is shown
43     // outside the layout, the configurable field is not shown at all, and the
44     // layout itself is rendered (but empty).
45     $new_expected_fields = [
46       'field field--name-name field--type-string field--label-hidden field__item',
47       'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
48       'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
49     ];
50     $this->assertFieldAttributes($this->entity, $new_expected_fields);
51     $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
52
53     // Removing the layout restores the original rendering of the entity.
54     $this->entity->get(OverridesSectionStorage::FIELD_NAME)->removeSection(0);
55     $this->entity->save();
56     $this->assertFieldAttributes($this->entity, $expected_fields);
57
58     // Test that adding a new field after Layout Builder has been installed will
59     // add the new field to the default region of the first section.
60     $field_storage = FieldStorageConfig::create([
61       'entity_type' => 'entity_test_base_field_display',
62       'field_name' => 'test_field_display_post_install',
63       'type' => 'text',
64     ]);
65     $field_storage->save();
66     FieldConfig::create([
67       'field_storage' => $field_storage,
68       'bundle' => 'entity_test_base_field_display',
69       'label' => 'FieldConfig with configurable display',
70     ])->save();
71
72     $this->entity = $this->reloadEntity($this->entity);
73     $this->entity->test_field_display_post_install = 'Test string';
74     $this->entity->save();
75
76     $this->display = $this->reloadEntity($this->display);
77     $this->display
78       ->setComponent('test_field_display_post_install', ['weight' => 50])
79       ->save();
80     $new_expected_fields = [
81       'field field--name-name field--type-string field--label-hidden field__item',
82       'field field--name-test-field-display-configurable field--type-boolean field--label-above',
83       'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
84       'clearfix text-formatted field field--name-test-field-display-post-install field--type-text field--label-above',
85       'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
86       'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
87     ];
88     $this->assertFieldAttributes($this->entity, $new_expected_fields);
89     $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
90     $this->assertText('Test string');
91   }
92
93 }