eff239507d2b111c7717474e483e4706ad58f071
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / SectionTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
5 use Drupal\layout_builder\Section;
6 use Drupal\layout_builder\SectionComponent;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\layout_builder\Section
11  * @group layout_builder
12  */
13 class SectionTest extends UnitTestCase {
14
15   /**
16    * The section object to test.
17    *
18    * @var \Drupal\layout_builder\Section
19    */
20   protected $section;
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
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),
32     ]);
33   }
34
35   /**
36    * @covers ::__construct
37    * @covers ::setComponent
38    * @covers ::getComponents
39    */
40   public function testGetComponents() {
41     $expected = [
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),
45     ];
46
47     $this->assertComponents($expected, $this->section);
48   }
49
50   /**
51    * @covers ::getComponent
52    */
53   public function testGetComponentInvalidUuid() {
54     $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID "invalid-uuid"');
55     $this->section->getComponent('invalid-uuid');
56   }
57
58   /**
59    * @covers ::getComponent
60    */
61   public function testGetComponent() {
62     $expected = new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']);
63
64     $this->assertEquals($expected, $this->section->getComponent('existing-uuid'));
65   }
66
67   /**
68    * @covers ::removeComponent
69    * @covers ::getComponentsByRegion
70    */
71   public function testRemoveComponent() {
72     $expected = [
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),
75     ];
76
77     $this->section->removeComponent('first-uuid');
78     $this->assertComponents($expected, $this->section);
79   }
80
81   /**
82    * @covers ::appendComponent
83    * @covers ::getNextHighestWeight
84    * @covers ::getComponentsByRegion
85    */
86   public function testAppendComponent() {
87     $expected = [
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),
92     ];
93
94     $this->section->appendComponent(new SectionComponent('new-uuid', 'some-region'));
95     $this->assertComponents($expected, $this->section);
96   }
97
98   /**
99    * @covers ::insertAfterComponent
100    */
101   public function testInsertAfterComponent() {
102     $expected = [
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),
107     ];
108
109     $this->section->insertAfterComponent('first-uuid', new SectionComponent('new-uuid', 'ordered-region'));
110     $this->assertComponents($expected, $this->section);
111   }
112
113   /**
114    * @covers ::insertAfterComponent
115    */
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'));
119   }
120
121   /**
122    * @covers ::insertAfterComponent
123    */
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'));
127   }
128
129   /**
130    * @covers ::insertComponent
131    * @covers ::getComponentsByRegion
132    */
133   public function testInsertComponent() {
134     $expected = [
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),
139     ];
140
141     $this->section->insertComponent(0, new SectionComponent('new-uuid', 'ordered-region'));
142     $this->assertComponents($expected, $this->section);
143   }
144
145   /**
146    * @covers ::insertComponent
147    */
148   public function testInsertComponentAppend() {
149     $expected = [
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),
154     ];
155
156     $this->section->insertComponent(2, new SectionComponent('new-uuid', 'ordered-region'));
157     $this->assertComponents($expected, $this->section);
158   }
159
160   /**
161    * @covers ::insertComponent
162    */
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'));
166   }
167
168   /**
169    * Asserts that the section has the expected components.
170    *
171    * @param \Drupal\layout_builder\SectionComponent[] $expected
172    *   The expected sections.
173    * @param \Drupal\layout_builder\Section $section
174    *   The section storage to check.
175    */
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));
180   }
181
182 }