3 namespace Drupal\Tests\layout_builder\Kernel;
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;
12 * Tests Layout Builder's compatibility with existing systems.
14 abstract class LayoutBuilderCompatibilityTestBase extends EntityKernelTestBase {
19 public static $modules = [
24 * The entity view display.
26 * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
31 * The entity being rendered.
33 * @var \Drupal\Core\Entity\FieldableEntityInterface
40 protected function setUp() {
43 $this->installEntitySchema('entity_test_base_field_display');
44 $this->installConfig(['filter']);
45 $this->installSchema('system', ['key_value_expire']);
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']));
50 \Drupal::service('theme_handler')->install(['classy']);
51 $this->config('system.theme')->set('default', 'classy')->save();
53 $field_storage = FieldStorageConfig::create([
54 'entity_type' => 'entity_test_base_field_display',
55 'field_name' => 'test_field_display_configurable',
58 $field_storage->save();
60 'field_storage' => $field_storage,
61 'bundle' => 'entity_test_base_field_display',
62 'label' => 'FieldConfig with configurable display',
65 $this->display = EntityViewDisplay::create([
66 'targetEntityType' => 'entity_test_base_field_display',
67 'bundle' => 'entity_test_base_field_display',
72 ->setComponent('test_field_display_configurable', ['weight' => 5])
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', [
81 'langcode_default' => TRUE,
83 $this->entity->save();
87 * Installs the Layout Builder.
89 * Also configures and reloads the entity display.
91 protected function installLayoutBuilder() {
92 $this->container->get('module_installer')->install(['layout_builder']);
93 $this->refreshServices();
95 $this->display = $this->reloadEntity($this->display);
96 $this->display->enableLayoutBuilder()->save();
97 $this->entity = $this->reloadEntity($this->entity);
101 * Enables overrides for the display and reloads the entity.
103 protected function enableOverrides() {
104 $this->display->setOverridable()->save();
105 $this->entity = $this->reloadEntity($this->entity);
109 * Asserts that the rendered entity has the correct fields.
111 * @param \Drupal\Core\Entity\EntityInterface $entity
112 * The entity to render.
113 * @param array $attributes
114 * An array of field attributes to assert.
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);
121 $actual = array_map(function (\SimpleXMLElement $element) {
122 return (string) $element->attributes();
123 }, $this->cssSelect('.field'));
124 $this->assertSame($attributes, $actual);