Issue #3035174 by tim.plunkett, andypost, ankithashetty, jwwj, mrinalini9, mmbk, martin107, tatarbj, longwave, sandboxpl: Rename SectionStorageTrait to SectionListTrait
parent
2e7bd80439
commit
671433d1cc
|
@ -21,15 +21,15 @@ use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
|
|||
use Drupal\layout_builder\QuickEditIntegration;
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionComponent;
|
||||
use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
|
||||
use Drupal\layout_builder\SectionListTrait;
|
||||
|
||||
/**
|
||||
* Provides an entity view display entity that has a layout.
|
||||
*/
|
||||
class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements LayoutEntityDisplayInterface {
|
||||
|
||||
use SectionStorageTrait;
|
||||
use LayoutEntityHelperTrait;
|
||||
use SectionListTrait;
|
||||
|
||||
/**
|
||||
* The entity field manager.
|
||||
|
|
|
@ -8,7 +8,7 @@ use Drupal\Core\Field\FieldItemListInterface;
|
|||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionListInterface;
|
||||
use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
|
||||
use Drupal\layout_builder\SectionListTrait;
|
||||
|
||||
/**
|
||||
* Defines an item list class for layout section fields.
|
||||
|
@ -20,7 +20,7 @@ use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
|
|||
*/
|
||||
class LayoutSectionItemList extends FieldItemList implements SectionListInterface {
|
||||
|
||||
use SectionStorageTrait;
|
||||
use SectionListTrait;
|
||||
|
||||
/**
|
||||
* Numerically indexed array of field items.
|
||||
|
|
|
@ -8,8 +8,8 @@ use Drupal\Core\Layout\LayoutDefault;
|
|||
* Provides a layout plugin that produces no output.
|
||||
*
|
||||
* @see \Drupal\layout_builder\Field\LayoutSectionItemList::removeSection()
|
||||
* @see \Drupal\layout_builder\SectionStorage\SectionStorageTrait::addBlankSection()
|
||||
* @see \Drupal\layout_builder\SectionStorage\SectionStorageTrait::hasBlankSection()
|
||||
* @see \Drupal\layout_builder\SectionListTrait::addBlankSection()
|
||||
* @see \Drupal\layout_builder\SectionListTrait::hasBlankSection()
|
||||
*
|
||||
* @internal
|
||||
* This layout plugin is intended for internal use by Layout Builder only.
|
||||
|
|
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\layout_builder;
|
||||
|
||||
/**
|
||||
* Provides a trait for maintaining a list of sections.
|
||||
*
|
||||
* @see \Drupal\layout_builder\SectionListInterface
|
||||
*/
|
||||
trait SectionListTrait {
|
||||
|
||||
/**
|
||||
* Stores the information for all sections.
|
||||
*
|
||||
* Implementations of this method are expected to call array_values() to rekey
|
||||
* the list of sections.
|
||||
*
|
||||
* @param \Drupal\layout_builder\Section[] $sections
|
||||
* An array of section objects.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
abstract protected function setSections(array $sections);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function count() {
|
||||
if ($this->hasBlankSection()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return count($this->getSections());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSection($delta) {
|
||||
if (!$this->hasSection($delta)) {
|
||||
throw new \OutOfBoundsException(sprintf('Invalid delta "%s"', $delta));
|
||||
}
|
||||
|
||||
return $this->getSections()[$delta];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the section for the given delta on the display.
|
||||
*
|
||||
* @param int $delta
|
||||
* The delta of the section.
|
||||
* @param \Drupal\layout_builder\Section $section
|
||||
* The layout section.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setSection($delta, Section $section) {
|
||||
$sections = $this->getSections();
|
||||
$sections[$delta] = $section;
|
||||
$this->setSections($sections);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function appendSection(Section $section) {
|
||||
$delta = $this->count();
|
||||
|
||||
$this->setSection($delta, $section);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function insertSection($delta, Section $section) {
|
||||
// Clear the section list if there is currently a blank section.
|
||||
if ($this->hasBlankSection()) {
|
||||
$this->removeAllSections();
|
||||
}
|
||||
|
||||
if ($this->hasSection($delta)) {
|
||||
// @todo Use https://www.drupal.org/node/66183 once resolved.
|
||||
$start = array_slice($this->getSections(), 0, $delta);
|
||||
$end = array_slice($this->getSections(), $delta);
|
||||
$this->setSections(array_merge($start, [$section], $end));
|
||||
}
|
||||
else {
|
||||
$this->appendSection($section);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a blank section to the list.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see \Drupal\layout_builder\Plugin\Layout\BlankLayout
|
||||
*/
|
||||
protected function addBlankSection() {
|
||||
if ($this->hasSection(0)) {
|
||||
throw new \Exception('A blank section must only be added to an empty list');
|
||||
}
|
||||
|
||||
$this->appendSection(new Section('layout_builder_blank'));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this section list contains a blank section.
|
||||
*
|
||||
* A blank section is used to differentiate the difference between a layout
|
||||
* that has never been instantiated and one that has purposefully had all
|
||||
* sections removed.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the section list contains a blank section, FALSE otherwise.
|
||||
*
|
||||
* @see \Drupal\layout_builder\Plugin\Layout\BlankLayout
|
||||
*/
|
||||
protected function hasBlankSection() {
|
||||
// A blank section will only ever exist when the delta is 0, as added by
|
||||
// ::removeSection().
|
||||
return $this->hasSection(0) && $this->getSection(0)->getLayoutId() === 'layout_builder_blank';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeSection($delta) {
|
||||
// Clear the section list if there is currently a blank section.
|
||||
if ($this->hasBlankSection()) {
|
||||
$this->removeAllSections();
|
||||
}
|
||||
|
||||
$sections = $this->getSections();
|
||||
unset($sections[$delta]);
|
||||
$this->setSections($sections);
|
||||
// Add a blank section when the last section is removed.
|
||||
if (empty($sections)) {
|
||||
$this->addBlankSection();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeAllSections($set_blank = FALSE) {
|
||||
$this->setSections([]);
|
||||
if ($set_blank) {
|
||||
$this->addBlankSection();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if there is a section at the specified delta.
|
||||
*
|
||||
* @param int $delta
|
||||
* The delta of the section.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if there is a section for this delta, FALSE otherwise.
|
||||
*/
|
||||
protected function hasSection($delta) {
|
||||
return isset($this->getSections()[$delta]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method: Implements a deep clone.
|
||||
*/
|
||||
public function __clone() {
|
||||
$sections = $this->getSections();
|
||||
|
||||
foreach ($sections as $delta => $item) {
|
||||
$sections[$delta] = clone $item;
|
||||
}
|
||||
|
||||
$this->setSections($sections);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,184 +2,20 @@
|
|||
|
||||
namespace Drupal\layout_builder\SectionStorage;
|
||||
|
||||
use Drupal\layout_builder\Section;
|
||||
@trigger_error(__NAMESPACE__ . '\SectionStorageTrait is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\layout_builder\SectionListTrait instead. See https://www.drupal.org/node/3091432', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\layout_builder\SectionListTrait;
|
||||
|
||||
/**
|
||||
* Provides a trait for storing sections on an object.
|
||||
*
|
||||
* @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use
|
||||
* \Drupal\layout_builder\SectionListTrait instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3091432
|
||||
*/
|
||||
trait SectionStorageTrait {
|
||||
|
||||
/**
|
||||
* Stores the information for all sections.
|
||||
*
|
||||
* Implementations of this method are expected to call array_values() to rekey
|
||||
* the list of sections.
|
||||
*
|
||||
* @param \Drupal\layout_builder\Section[] $sections
|
||||
* An array of section objects.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
abstract protected function setSections(array $sections);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function count() {
|
||||
if ($this->hasBlankSection()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return count($this->getSections());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSection($delta) {
|
||||
if (!$this->hasSection($delta)) {
|
||||
throw new \OutOfBoundsException(sprintf('Invalid delta "%s"', $delta));
|
||||
}
|
||||
|
||||
return $this->getSections()[$delta];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the section for the given delta on the display.
|
||||
*
|
||||
* @param int $delta
|
||||
* The delta of the section.
|
||||
* @param \Drupal\layout_builder\Section $section
|
||||
* The layout section.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setSection($delta, Section $section) {
|
||||
$sections = $this->getSections();
|
||||
$sections[$delta] = $section;
|
||||
$this->setSections($sections);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function appendSection(Section $section) {
|
||||
$delta = $this->count();
|
||||
|
||||
$this->setSection($delta, $section);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function insertSection($delta, Section $section) {
|
||||
// Clear the section list if there is currently a blank section.
|
||||
if ($this->hasBlankSection()) {
|
||||
$this->removeAllSections();
|
||||
}
|
||||
|
||||
if ($this->hasSection($delta)) {
|
||||
// @todo Use https://www.drupal.org/node/66183 once resolved.
|
||||
$start = array_slice($this->getSections(), 0, $delta);
|
||||
$end = array_slice($this->getSections(), $delta);
|
||||
$this->setSections(array_merge($start, [$section], $end));
|
||||
}
|
||||
else {
|
||||
$this->appendSection($section);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a blank section to the list.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see \Drupal\layout_builder\Plugin\Layout\BlankLayout
|
||||
*/
|
||||
protected function addBlankSection() {
|
||||
if ($this->hasSection(0)) {
|
||||
throw new \Exception('A blank section must only be added to an empty list');
|
||||
}
|
||||
|
||||
$this->appendSection(new Section('layout_builder_blank'));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this section list contains a blank section.
|
||||
*
|
||||
* A blank section is used to differentiate the difference between a layout
|
||||
* that has never been instantiated and one that has purposefully had all
|
||||
* sections removed.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the section list contains a blank section, FALSE otherwise.
|
||||
*
|
||||
* @see \Drupal\layout_builder\Plugin\Layout\BlankLayout
|
||||
*/
|
||||
protected function hasBlankSection() {
|
||||
// A blank section will only ever exist when the delta is 0, as added by
|
||||
// ::removeSection().
|
||||
return $this->hasSection(0) && $this->getSection(0)->getLayoutId() === 'layout_builder_blank';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeSection($delta) {
|
||||
// Clear the section list if there is currently a blank section.
|
||||
if ($this->hasBlankSection()) {
|
||||
$this->removeAllSections();
|
||||
}
|
||||
|
||||
$sections = $this->getSections();
|
||||
unset($sections[$delta]);
|
||||
$this->setSections($sections);
|
||||
// Add a blank section when the last section is removed.
|
||||
if (empty($sections)) {
|
||||
$this->addBlankSection();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeAllSections($set_blank = FALSE) {
|
||||
$this->setSections([]);
|
||||
if ($set_blank) {
|
||||
$this->addBlankSection();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if there is a section at the specified delta.
|
||||
*
|
||||
* @param int $delta
|
||||
* The delta of the section.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if there is a section for this delta, FALSE otherwise.
|
||||
*/
|
||||
protected function hasSection($delta) {
|
||||
return isset($this->getSections()[$delta]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method: Implements a deep clone.
|
||||
*/
|
||||
public function __clone() {
|
||||
$sections = $this->getSections();
|
||||
|
||||
foreach ($sections as $delta => $item) {
|
||||
$sections[$delta] = clone $item;
|
||||
}
|
||||
|
||||
$this->setSections($sections);
|
||||
}
|
||||
use SectionListTrait;
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use Drupal\Core\Url;
|
|||
use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageLocalTaskProviderInterface;
|
||||
use Drupal\layout_builder\Routing\LayoutBuilderRoutesTrait;
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
|
||||
use Drupal\layout_builder\SectionListTrait;
|
||||
use Drupal\layout_builder\SectionStorageInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
@ -34,7 +34,7 @@ class SimpleConfigSectionStorage extends PluginBase implements SectionStorageInt
|
|||
|
||||
use ContextAwarePluginTrait;
|
||||
use LayoutBuilderRoutesTrait;
|
||||
use SectionStorageTrait;
|
||||
use SectionListTrait;
|
||||
|
||||
/**
|
||||
* The config factory.
|
||||
|
|
|
@ -10,12 +10,12 @@ use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
|
|||
*
|
||||
* @group layout_builder
|
||||
*/
|
||||
class LayoutBuilderEntityViewDisplayTest extends SectionStorageTestBase {
|
||||
class LayoutBuilderEntityViewDisplayTest extends SectionListTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSectionStorage(array $section_data) {
|
||||
protected function getSectionList(array $section_data) {
|
||||
$display = LayoutBuilderEntityViewDisplay::create([
|
||||
'targetEntityType' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
|
@ -37,8 +37,8 @@ class LayoutBuilderEntityViewDisplayTest extends SectionStorageTestBase {
|
|||
*/
|
||||
public function testInvalidConfiguration() {
|
||||
$this->expectException(SchemaIncompleteException::class);
|
||||
$this->sectionStorage->getSection(0)->getComponent('first-uuid')->setConfiguration(['id' => 'foo', 'bar' => 'baz']);
|
||||
$this->sectionStorage->save();
|
||||
$this->sectionList->getSection(0)->getComponent('first-uuid')->setConfiguration(['id' => 'foo', 'bar' => 'baz']);
|
||||
$this->sectionList->save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,15 +79,15 @@ class LayoutBuilderEntityViewDisplayTest extends SectionStorageTestBase {
|
|||
*/
|
||||
public function testSetOverridable() {
|
||||
// Disable Layout Builder.
|
||||
$this->sectionStorage->disableLayoutBuilder();
|
||||
$this->sectionList->disableLayoutBuilder();
|
||||
|
||||
// Set Overridable to TRUE and ensure Layout Builder is enabled.
|
||||
$this->sectionStorage->setOverridable();
|
||||
$this->assertTrue($this->sectionStorage->isLayoutBuilderEnabled());
|
||||
$this->sectionList->setOverridable();
|
||||
$this->assertTrue($this->sectionList->isLayoutBuilderEnabled());
|
||||
|
||||
// Ensure Layout Builder is still enabled after setting Overridable to FALSE.
|
||||
$this->sectionStorage->setOverridable(FALSE);
|
||||
$this->assertTrue($this->sectionStorage->isLayoutBuilderEnabled());
|
||||
$this->sectionList->setOverridable(FALSE);
|
||||
$this->assertTrue($this->sectionList->isLayoutBuilderEnabled());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
|
|||
*
|
||||
* @group layout_builder
|
||||
*/
|
||||
class LayoutSectionItemListTest extends SectionStorageTestBase {
|
||||
class LayoutSectionItemListTest extends SectionListTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -27,7 +27,7 @@ class LayoutSectionItemListTest extends SectionStorageTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSectionStorage(array $section_data) {
|
||||
protected function getSectionList(array $section_data) {
|
||||
$this->installEntitySchema('entity_test_base_field_display');
|
||||
LayoutBuilderEntityViewDisplay::create([
|
||||
'targetEntityType' => 'entity_test_base_field_display',
|
||||
|
@ -54,13 +54,13 @@ class LayoutSectionItemListTest extends SectionStorageTestBase {
|
|||
* @covers ::equals
|
||||
*/
|
||||
public function testEquals() {
|
||||
$this->sectionStorage->getSection(0)->setLayoutSettings(['foo' => 1]);
|
||||
$this->sectionList->getSection(0)->setLayoutSettings(['foo' => 1]);
|
||||
|
||||
$second_section_storage = clone $this->sectionStorage;
|
||||
$this->assertTrue($this->sectionStorage->equals($second_section_storage));
|
||||
$second_section_storage = clone $this->sectionList;
|
||||
$this->assertTrue($this->sectionList->equals($second_section_storage));
|
||||
|
||||
$second_section_storage->getSection(0)->setLayoutSettings(['foo' => '1']);
|
||||
$this->assertFalse($this->sectionStorage->equals($second_section_storage));
|
||||
$this->assertFalse($this->sectionList->equals($second_section_storage));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,7 +68,7 @@ class LayoutSectionItemListTest extends SectionStorageTestBase {
|
|||
*/
|
||||
public function testEqualsNonSection() {
|
||||
$list = $this->prophesize(FieldItemListInterface::class);
|
||||
$this->assertFalse($this->sectionStorage->equals($list->reveal()));
|
||||
$this->assertFalse($this->sectionList->equals($list->reveal()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,201 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\layout_builder\Kernel;
|
||||
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionComponent;
|
||||
|
||||
/**
|
||||
* Provides a base class for testing implementations of a section list.
|
||||
*/
|
||||
abstract class SectionListTestBase extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'layout_builder',
|
||||
'layout_discovery',
|
||||
'layout_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* The section list implementation.
|
||||
*
|
||||
* @var \Drupal\layout_builder\SectionListInterface
|
||||
*/
|
||||
protected $sectionList;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the section list.
|
||||
*
|
||||
* @param array $section_data
|
||||
* An array of section data.
|
||||
*
|
||||
* @return \Drupal\layout_builder\SectionListInterface
|
||||
* The section list.
|
||||
*/
|
||||
abstract protected function getSectionList(array $section_data);
|
||||
|
||||
/**
|
||||
* Tests ::getSections().
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getSection
|
||||
*/
|
||||
public function testGetSection() {
|
||||
$this->assertInstanceOf(Section::class, $this->sectionList->getSection(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getSection
|
||||
*/
|
||||
public function testGetSectionInvalidDelta() {
|
||||
$this->expectException(\OutOfBoundsException::class);
|
||||
$this->expectExceptionMessage('Invalid delta "2"');
|
||||
$this->sectionList->getSection(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::insertSection
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::appendSection
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeAllSections
|
||||
*
|
||||
* @dataProvider providerTestRemoveAllSections
|
||||
*/
|
||||
public function testRemoveAllSections($set_blank, $expected) {
|
||||
if ($set_blank === NULL) {
|
||||
$this->sectionList->removeAllSections();
|
||||
}
|
||||
else {
|
||||
$this->sectionList->removeAllSections($set_blank);
|
||||
}
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for ::testRemoveAllSections().
|
||||
*/
|
||||
public function providerTestRemoveAllSections() {
|
||||
$data = [];
|
||||
$data[] = [NULL, []];
|
||||
$data[] = [FALSE, []];
|
||||
$data[] = [TRUE, [new Section('layout_builder_blank')]];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeSection
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeSection
|
||||
*/
|
||||
public function testRemoveMultipleSections() {
|
||||
$expected = [
|
||||
new Section('layout_builder_blank'),
|
||||
];
|
||||
|
||||
$this->sectionList->removeSection(0);
|
||||
$this->sectionList->removeSection(0);
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests __clone().
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the field list has the expected sections.
|
||||
*
|
||||
* @param \Drupal\layout_builder\Section[] $expected
|
||||
* The expected sections.
|
||||
*/
|
||||
protected function assertSections(array $expected) {
|
||||
$result = $this->sectionList->getSections();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertSame(array_keys($expected), array_keys($result));
|
||||
}
|
||||
|
||||
}
|
|
@ -4,19 +4,19 @@ namespace Drupal\Tests\layout_builder\Kernel;
|
|||
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionListInterface;
|
||||
use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
|
||||
use Drupal\layout_builder\SectionListTrait;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\layout_builder\SectionStorage\SectionStorageTrait
|
||||
* @coversDefaultClass \Drupal\layout_builder\SectionListTrait
|
||||
*
|
||||
* @group layout_builder
|
||||
*/
|
||||
class SectionListTraitTest extends SectionStorageTestBase {
|
||||
class SectionListTraitTest extends SectionListTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSectionStorage(array $section_data) {
|
||||
protected function getSectionList(array $section_data) {
|
||||
return new TestSectionList($section_data);
|
||||
}
|
||||
|
||||
|
@ -26,14 +26,14 @@ class SectionListTraitTest extends SectionStorageTestBase {
|
|||
public function testAddBlankSection() {
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('A blank section must only be added to an empty list');
|
||||
$this->sectionStorage->addBlankSection();
|
||||
$this->sectionList->addBlankSection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestSectionList implements SectionListInterface {
|
||||
|
||||
use SectionStorageTrait {
|
||||
use SectionListTrait {
|
||||
addBlankSection as public;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,200 +2,52 @@
|
|||
|
||||
namespace Drupal\Tests\layout_builder\Kernel;
|
||||
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\layout_builder\Section;
|
||||
use Drupal\layout_builder\SectionComponent;
|
||||
@trigger_error(__NAMESPACE__ . '\SectionStorageTestBase is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Tests\layout_builder\Kernel\SectionListTestBase instead. See https://www.drupal.org/node/3091432', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Provides a base class for testing implementations of section storage.
|
||||
*
|
||||
* @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use
|
||||
* \Drupal\Tests\layout_builder\Kernel\SectionListTestBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3091432
|
||||
*/
|
||||
abstract class SectionStorageTestBase extends EntityKernelTestBase {
|
||||
abstract class SectionStorageTestBase extends SectionListTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'layout_builder',
|
||||
'layout_discovery',
|
||||
'layout_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* The section storage implementation.
|
||||
* The section list implementation.
|
||||
*
|
||||
* @var \Drupal\layout_builder\SectionStorageInterface
|
||||
* @var \Drupal\layout_builder\SectionListInterface
|
||||
*/
|
||||
protected $sectionStorage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
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->sectionStorage = $this->getSectionStorage($section_data);
|
||||
// Provide backwards compatibility.
|
||||
$this->sectionStorage = $this->sectionList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the section storage entity.
|
||||
* Sets up the section list.
|
||||
*
|
||||
* @param array $section_data
|
||||
* An array of section data.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityInterface
|
||||
* The entity.
|
||||
* @return \Drupal\layout_builder\SectionListInterface
|
||||
* The section list.
|
||||
*/
|
||||
abstract protected function getSectionStorage(array $section_data);
|
||||
|
||||
/**
|
||||
* Tests ::getSections().
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getSection
|
||||
*/
|
||||
public function testGetSection() {
|
||||
$this->assertInstanceOf(Section::class, $this->sectionStorage->getSection(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getSection
|
||||
*/
|
||||
public function testGetSectionInvalidDelta() {
|
||||
$this->expectException(\OutOfBoundsException::class);
|
||||
$this->expectExceptionMessage('Invalid delta "2"');
|
||||
$this->sectionStorage->getSection(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::insertSection
|
||||
*/
|
||||
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->sectionStorage->insertSection(1, new Section('layout_onecol'));
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::appendSection
|
||||
*/
|
||||
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->sectionStorage->appendSection(new Section('layout_onecol'));
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeAllSections
|
||||
*
|
||||
* @dataProvider providerTestRemoveAllSections
|
||||
*/
|
||||
public function testRemoveAllSections($set_blank, $expected) {
|
||||
if ($set_blank === NULL) {
|
||||
$this->sectionStorage->removeAllSections();
|
||||
}
|
||||
else {
|
||||
$this->sectionStorage->removeAllSections($set_blank);
|
||||
}
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for ::testRemoveAllSections().
|
||||
*/
|
||||
public function providerTestRemoveAllSections() {
|
||||
$data = [];
|
||||
$data[] = [NULL, []];
|
||||
$data[] = [FALSE, []];
|
||||
$data[] = [TRUE, [new Section('layout_builder_blank')]];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeSection
|
||||
*/
|
||||
public function testRemoveSection() {
|
||||
$expected = [
|
||||
new Section('layout_test_plugin', ['setting_1' => 'bar'], [
|
||||
'second-uuid' => new SectionComponent('second-uuid', 'content', ['id' => 'foo']),
|
||||
]),
|
||||
];
|
||||
|
||||
$this->sectionStorage->removeSection(0);
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::removeSection
|
||||
*/
|
||||
public function testRemoveMultipleSections() {
|
||||
$expected = [
|
||||
new Section('layout_builder_blank'),
|
||||
];
|
||||
|
||||
$this->sectionStorage->removeSection(0);
|
||||
$this->sectionStorage->removeSection(0);
|
||||
$this->assertSections($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests __clone().
|
||||
*/
|
||||
public function testClone() {
|
||||
$this->assertSame(['setting_1' => 'Default'], $this->sectionStorage->getSection(0)->getLayoutSettings());
|
||||
|
||||
$new_section_storage = clone $this->sectionStorage;
|
||||
$new_section_storage->getSection(0)->setLayoutSettings(['asdf' => 'qwer']);
|
||||
$this->assertSame(['setting_1' => 'Default'], $this->sectionStorage->getSection(0)->getLayoutSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the field list has the expected sections.
|
||||
*
|
||||
* @param \Drupal\layout_builder\Section[] $expected
|
||||
* The expected sections.
|
||||
*/
|
||||
protected function assertSections(array $expected) {
|
||||
$result = $this->sectionStorage->getSections();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertSame(array_keys($expected), array_keys($result));
|
||||
protected function getSectionList(array $section_data) {
|
||||
// Provide backwards compatibility.
|
||||
return $this->getSectionStorage($section_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use Drupal\layout_builder_test\Plugin\SectionStorage\SimpleConfigSectionStorage;
|
|||
*
|
||||
* @group layout_builder
|
||||
*/
|
||||
class SimpleConfigSectionStorageTest extends SectionStorageTestBase {
|
||||
class SimpleConfigSectionListTest extends SectionListTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -27,7 +27,7 @@ class SimpleConfigSectionStorageTest extends SectionStorageTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSectionStorage(array $section_data) {
|
||||
protected function getSectionList(array $section_data) {
|
||||
$config = $this->container->get('config.factory')->getEditable('layout_builder_test.test_simple_config.foobar');
|
||||
$section_data = array_map(function (Section $section) {
|
||||
return $section->toArray();
|
Loading…
Reference in New Issue