Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / layout_builder / tests / src / Kernel / LayoutBuilderCompatibilityTestBase.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Kernel;
4
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
10
11 /**
12  * Tests Layout Builder's compatibility with existing systems.
13  */
14 abstract class LayoutBuilderCompatibilityTestBase extends EntityKernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'layout_discovery',
21   ];
22
23   /**
24    * The entity view display.
25    *
26    * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
27    */
28   protected $display;
29
30   /**
31    * The entity being rendered.
32    *
33    * @var \Drupal\Core\Entity\FieldableEntityInterface
34    */
35   protected $entity;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     $this->installEntitySchema('entity_test_base_field_display');
44     $this->installConfig(['filter']);
45     $this->installSchema('system', ['key_value_expire']);
46
47     // Set up a non-admin user that is allowed to view test entities.
48     \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['view test entity']));
49
50     \Drupal::service('theme_handler')->install(['classy']);
51     $this->config('system.theme')->set('default', 'classy')->save();
52
53     $field_storage = FieldStorageConfig::create([
54       'entity_type' => 'entity_test_base_field_display',
55       'field_name' => 'test_field_display_configurable',
56       'type' => 'boolean',
57     ]);
58     $field_storage->save();
59     FieldConfig::create([
60       'field_storage' => $field_storage,
61       'bundle' => 'entity_test_base_field_display',
62       'label' => 'FieldConfig with configurable display',
63     ])->save();
64
65     $this->display = EntityViewDisplay::create([
66       'targetEntityType' => 'entity_test_base_field_display',
67       'bundle' => 'entity_test_base_field_display',
68       'mode' => 'default',
69       'status' => TRUE,
70     ]);
71     $this->display
72       ->setComponent('test_field_display_configurable', ['weight' => 5])
73       ->save();
74
75     // Create an entity with fields that are configurable and non-configurable.
76     $entity_storage = $this->container->get('entity_type.manager')->getStorage('entity_test_base_field_display');
77     // @todo Remove langcode workarounds after resolving
78     //   https://www.drupal.org/node/2915034.
79     $this->entity = $entity_storage->createWithSampleValues('entity_test_base_field_display', [
80       'langcode' => 'en',
81       'langcode_default' => TRUE,
82     ]);
83     $this->entity->save();
84   }
85
86   /**
87    * Installs the Layout Builder.
88    *
89    * Also configures and reloads the entity display.
90    */
91   protected function installLayoutBuilder() {
92     $this->container->get('module_installer')->install(['layout_builder']);
93     $this->refreshServices();
94
95     $this->display = $this->reloadEntity($this->display);
96     $this->display->enableLayoutBuilder()->save();
97     $this->entity = $this->reloadEntity($this->entity);
98   }
99
100   /**
101    * Enables overrides for the display and reloads the entity.
102    */
103   protected function enableOverrides() {
104     $this->display->setOverridable()->save();
105     $this->entity = $this->reloadEntity($this->entity);
106   }
107
108   /**
109    * Asserts that the rendered entity has the correct fields.
110    *
111    * @param \Drupal\Core\Entity\EntityInterface $entity
112    *   The entity to render.
113    * @param array $attributes
114    *   An array of field attributes to assert.
115    */
116   protected function assertFieldAttributes(EntityInterface $entity, array $attributes) {
117     $view_builder = $this->container->get('entity_type.manager')->getViewBuilder($entity->getEntityTypeId());
118     $build = $view_builder->view($entity);
119     $this->render($build);
120
121     $actual = array_map(function (\SimpleXMLElement $element) {
122       return (string) $element->attributes();
123     }, $this->cssSelect('.field'));
124     $this->assertSame($attributes, $actual);
125   }
126
127 }