3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\layout_builder\Section;
6 use Drupal\layout_builder\SectionComponent;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\layout_builder\Section
11 * @group layout_builder
13 class SectionTest extends UnitTestCase {
16 * The section object to test.
18 * @var \Drupal\layout_builder\Section
25 protected function setUp() {
28 $this->section = new Section('layout_onecol', [], [
29 new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']),
30 (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
31 (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
36 * @covers ::__construct
37 * @covers ::setComponent
38 * @covers ::getComponents
40 public function testGetComponents() {
42 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
43 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
44 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
47 $this->assertComponents($expected, $this->section);
51 * @covers ::getComponent
53 public function testGetComponentInvalidUuid() {
54 $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID "invalid-uuid"');
55 $this->section->getComponent('invalid-uuid');
59 * @covers ::getComponent
61 public function testGetComponent() {
62 $expected = new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']);
64 $this->assertEquals($expected, $this->section->getComponent('existing-uuid'));
68 * @covers ::removeComponent
69 * @covers ::getComponentsByRegion
71 public function testRemoveComponent() {
73 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
74 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
77 $this->section->removeComponent('first-uuid');
78 $this->assertComponents($expected, $this->section);
82 * @covers ::appendComponent
83 * @covers ::getNextHighestWeight
84 * @covers ::getComponentsByRegion
86 public function testAppendComponent() {
88 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
89 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
90 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
91 'new-uuid' => (new SectionComponent('new-uuid', 'some-region', []))->setWeight(1),
94 $this->section->appendComponent(new SectionComponent('new-uuid', 'some-region'));
95 $this->assertComponents($expected, $this->section);
99 * @covers ::insertAfterComponent
101 public function testInsertAfterComponent() {
103 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
104 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(4),
105 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
106 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(3),
109 $this->section->insertAfterComponent('first-uuid', new SectionComponent('new-uuid', 'ordered-region'));
110 $this->assertComponents($expected, $this->section);
114 * @covers ::insertAfterComponent
116 public function testInsertAfterComponentValidUuidRegionMismatch() {
117 $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID "existing-uuid"');
118 $this->section->insertAfterComponent('existing-uuid', new SectionComponent('new-uuid', 'ordered-region'));
122 * @covers ::insertAfterComponent
124 public function testInsertAfterComponentInvalidUuid() {
125 $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID "invalid-uuid"');
126 $this->section->insertAfterComponent('invalid-uuid', new SectionComponent('new-uuid', 'ordered-region'));
130 * @covers ::insertComponent
131 * @covers ::getComponentsByRegion
133 public function testInsertComponent() {
135 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
136 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(4),
137 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(3),
138 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(2),
141 $this->section->insertComponent(0, new SectionComponent('new-uuid', 'ordered-region'));
142 $this->assertComponents($expected, $this->section);
146 * @covers ::insertComponent
148 public function testInsertComponentAppend() {
150 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0),
151 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
152 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
153 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(4),
156 $this->section->insertComponent(2, new SectionComponent('new-uuid', 'ordered-region'));
157 $this->assertComponents($expected, $this->section);
161 * @covers ::insertComponent
163 public function testInsertComponentInvalidDelta() {
164 $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta "7" for the "new-uuid" component');
165 $this->section->insertComponent(7, new SectionComponent('new-uuid', 'ordered-region'));
169 * Asserts that the section has the expected components.
171 * @param \Drupal\layout_builder\SectionComponent[] $expected
172 * The expected sections.
173 * @param \Drupal\layout_builder\Section $section
174 * The section storage to check.
176 protected function assertComponents(array $expected, Section $section) {
177 $result = $section->getComponents();
178 $this->assertEquals($expected, $result);
179 $this->assertSame(array_keys($expected), array_keys($result));