3 namespace Drupal\Tests\layout_builder\Kernel;
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\layout_builder\Section;
7 use Drupal\layout_builder\SectionComponent;
10 * Provides a base class for testing implementations of section storage.
12 abstract class SectionStorageTestBase extends EntityKernelTestBase {
17 public static $modules = [
24 * The section storage implementation.
26 * @var \Drupal\layout_builder\SectionStorageInterface
28 protected $sectionStorage;
33 protected function setUp() {
36 $this->installSchema('system', ['key_value_expire']);
39 new Section('layout_test_plugin', [], [
40 'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
42 new Section('layout_test_plugin', ['setting_1' => 'bar'], [
43 'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
46 $this->sectionStorage = $this->getSectionStorage($section_data);
50 * Sets up the section storage entity.
52 * @param array $section_data
53 * An array of section data.
55 * @return \Drupal\Core\Entity\EntityInterface
58 abstract protected function getSectionStorage(array $section_data);
61 * @covers ::getSections
63 public function testGetSections() {
65 new Section('layout_test_plugin', [], [
66 'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
68 new Section('layout_test_plugin', ['setting_1' => 'bar'], [
69 'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
72 $this->assertSections($expected);
76 * @covers ::getSection
78 public function testGetSection() {
79 $this->assertInstanceOf(Section::class, $this->sectionStorage->getSection(0));
83 * @covers ::getSection
85 public function testGetSectionInvalidDelta() {
86 $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta "2"');
87 $this->sectionStorage->getSection(2);
91 * @covers ::insertSection
93 public function testInsertSection() {
95 new Section('layout_test_plugin', [], [
96 'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
98 new Section('setting_1'),
99 new Section('layout_test_plugin', ['setting_1' => 'bar'], [
100 'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
104 $this->sectionStorage->insertSection(1, new Section('setting_1'));
105 $this->assertSections($expected);
109 * @covers ::appendSection
111 public function testAppendSection() {
113 new Section('layout_test_plugin', [], [
114 'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
116 new Section('layout_test_plugin', ['setting_1' => 'bar'], [
117 'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
122 $this->sectionStorage->appendSection(new Section('foo'));
123 $this->assertSections($expected);
127 * @covers ::removeSection
129 public function testRemoveSection() {
131 new Section('layout_test_plugin', ['setting_1' => 'bar'], [
132 'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
136 $this->sectionStorage->removeSection(0);
137 $this->assertSections($expected);
143 public function testClone() {
144 $this->assertSame([], $this->sectionStorage->getSection(0)->getLayoutSettings());
146 $new_section_storage = clone $this->sectionStorage;
147 $new_section_storage->getSection(0)->setLayoutSettings(['asdf' => 'qwer']);
148 $this->assertSame([], $this->sectionStorage->getSection(0)->getLayoutSettings());
152 * Asserts that the field list has the expected sections.
154 * @param \Drupal\layout_builder\Section[] $expected
155 * The expected sections.
157 protected function assertSections(array $expected) {
158 $result = $this->sectionStorage->getSections();
159 $this->assertEquals($expected, $result);
160 $this->assertSame(array_keys($expected), array_keys($result));