View source
<?php
namespace Drupal\Tests\layout_builder\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
abstract class SectionListTestBase extends EntityKernelTestBase {
protected static $modules = [
'layout_builder',
'layout_discovery',
'layout_test',
];
protected $sectionList;
protected function setUp() : void {
parent::setUp();
$section_data = [
new Section('layout_test_plugin', [], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'foo',
]),
]),
new Section('layout_test_plugin', [
'setting_1' => 'bar',
], [
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo',
]),
]),
];
$this->sectionList = $this
->getSectionList($section_data);
}
protected abstract function getSectionList(array $section_data);
public function testGetSections() {
$expected = [
new Section('layout_test_plugin', [
'setting_1' => 'Default',
], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'foo',
]),
]),
new Section('layout_test_plugin', [
'setting_1' => 'bar',
], [
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo',
]),
]),
];
$this
->assertSections($expected);
}
public function testGetSection() {
$this
->assertInstanceOf(Section::class, $this->sectionList
->getSection(0));
}
public function testGetSectionInvalidDelta() {
$this
->expectException(\OutOfBoundsException::class);
$this
->expectExceptionMessage('Invalid delta "2"');
$this->sectionList
->getSection(2);
}
public function testInsertSection() {
$expected = [
new Section('layout_test_plugin', [
'setting_1' => 'Default',
], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'foo',
]),
]),
new Section('layout_onecol'),
new Section('layout_test_plugin', [
'setting_1' => 'bar',
], [
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo',
]),
]),
];
$this->sectionList
->insertSection(1, new Section('layout_onecol'));
$this
->assertSections($expected);
}
public function testAppendSection() {
$expected = [
new Section('layout_test_plugin', [
'setting_1' => 'Default',
], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'foo',
]),
]),
new Section('layout_test_plugin', [
'setting_1' => 'bar',
], [
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo',
]),
]),
new Section('layout_onecol'),
];
$this->sectionList
->appendSection(new Section('layout_onecol'));
$this
->assertSections($expected);
}
public function testRemoveAllSections($set_blank, $expected) {
if ($set_blank === NULL) {
$this->sectionList
->removeAllSections();
}
else {
$this->sectionList
->removeAllSections($set_blank);
}
$this
->assertSections($expected);
}
public function providerTestRemoveAllSections() {
$data = [];
$data[] = [
NULL,
[],
];
$data[] = [
FALSE,
[],
];
$data[] = [
TRUE,
[
new Section('layout_builder_blank'),
],
];
return $data;
}
public function testRemoveSection() {
$expected = [
new Section('layout_test_plugin', [
'setting_1' => 'bar',
], [
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo',
]),
]),
];
$this->sectionList
->removeSection(0);
$this
->assertSections($expected);
}
public function testRemoveMultipleSections() {
$expected = [
new Section('layout_builder_blank'),
];
$this->sectionList
->removeSection(0);
$this->sectionList
->removeSection(0);
$this
->assertSections($expected);
}
public function testClone() {
$this
->assertSame([
'setting_1' => 'Default',
], $this->sectionList
->getSection(0)
->getLayoutSettings());
$new_section_storage = clone $this->sectionList;
$new_section_storage
->getSection(0)
->setLayoutSettings([
'asdf' => 'qwer',
]);
$this
->assertSame([
'setting_1' => 'Default',
], $this->sectionList
->getSection(0)
->getLayoutSettings());
}
protected function assertSections(array $expected) {
$result = $this->sectionList
->getSections();
$this
->assertEquals($expected, $result);
$this
->assertSame(array_keys($expected), array_keys($result));
}
}