Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / tests / src / Kernel / SectionStorageTestBase.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Kernel;
4
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\layout_builder\Section;
7 use Drupal\layout_builder\SectionComponent;
8
9 /**
10  * Provides a base class for testing implementations of section storage.
11  */
12 abstract class SectionStorageTestBase extends EntityKernelTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = [
18     'layout_builder',
19     'layout_discovery',
20     'layout_test',
21   ];
22
23   /**
24    * The section storage implementation.
25    *
26    * @var \Drupal\layout_builder\SectionStorageInterface
27    */
28   protected $sectionStorage;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->installSchema('system', ['key_value_expire']);
37
38     $section_data = [
39       new Section('layout_test_plugin', [], [
40         'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
41       ]),
42       new Section('layout_test_plugin', ['setting_1' => 'bar'], [
43         'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
44       ]),
45     ];
46     $this->sectionStorage = $this->getSectionStorage($section_data);
47   }
48
49   /**
50    * Sets up the section storage entity.
51    *
52    * @param array $section_data
53    *   An array of section data.
54    *
55    * @return \Drupal\Core\Entity\EntityInterface
56    *   The entity.
57    */
58   abstract protected function getSectionStorage(array $section_data);
59
60   /**
61    * @covers ::getSections
62    */
63   public function testGetSections() {
64     $expected = [
65       new Section('layout_test_plugin', [], [
66         'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
67       ]),
68       new Section('layout_test_plugin', ['setting_1' => 'bar'], [
69         'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
70       ]),
71     ];
72     $this->assertSections($expected);
73   }
74
75   /**
76    * @covers ::getSection
77    */
78   public function testGetSection() {
79     $this->assertInstanceOf(Section::class, $this->sectionStorage->getSection(0));
80   }
81
82   /**
83    * @covers ::getSection
84    */
85   public function testGetSectionInvalidDelta() {
86     $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta "2"');
87     $this->sectionStorage->getSection(2);
88   }
89
90   /**
91    * @covers ::insertSection
92    */
93   public function testInsertSection() {
94     $expected = [
95       new Section('layout_test_plugin', [], [
96         'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
97       ]),
98       new Section('setting_1'),
99       new Section('layout_test_plugin', ['setting_1' => 'bar'], [
100         'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
101       ]),
102     ];
103
104     $this->sectionStorage->insertSection(1, new Section('setting_1'));
105     $this->assertSections($expected);
106   }
107
108   /**
109    * @covers ::appendSection
110    */
111   public function testAppendSection() {
112     $expected = [
113       new Section('layout_test_plugin', [], [
114         'first-uuid' => new SectionComponent('first-uuid', 'content', ['id' => 'foo']),
115       ]),
116       new Section('layout_test_plugin', ['setting_1' => 'bar'], [
117         'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
118       ]),
119       new Section('foo'),
120     ];
121
122     $this->sectionStorage->appendSection(new Section('foo'));
123     $this->assertSections($expected);
124   }
125
126   /**
127    * @covers ::removeSection
128    */
129   public function testRemoveSection() {
130     $expected = [
131       new Section('layout_test_plugin', ['setting_1' => 'bar'], [
132         'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
133       ]),
134     ];
135
136     $this->sectionStorage->removeSection(0);
137     $this->assertSections($expected);
138   }
139
140   /**
141    * Asserts that the field list has the expected sections.
142    *
143    * @param \Drupal\layout_builder\Section[] $expected
144    *   The expected sections.
145    */
146   protected function assertSections(array $expected) {
147     $result = $this->sectionStorage->getSections();
148     $this->assertEquals($expected, $result);
149     $this->assertSame(array_keys($expected), array_keys($result));
150   }
151
152 }