diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index c0288297f6974ed5ec6425d6eab20e33d5980cb4..f872e251fd2522b5f37a2bbe53be8dcf07bbb5af 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -21,15 +21,15 @@ 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. diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php index 28b17399abdc06a134bef814be17159e9f966638..9870bc41b39ef8f5c1573f5e2a906fcde44e1c9b 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php @@ -8,7 +8,7 @@ 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 @@ */ class LayoutSectionItemList extends FieldItemList implements SectionListInterface { - use SectionStorageTrait; + use SectionListTrait; /** * Numerically indexed array of field items. diff --git a/core/modules/layout_builder/src/Plugin/Layout/BlankLayout.php b/core/modules/layout_builder/src/Plugin/Layout/BlankLayout.php index def5fe12238357115e30dcbdb1399bf06980c470..a3f71b0ef9c60c123ad4bbbe4e75a9239088ad63 100644 --- a/core/modules/layout_builder/src/Plugin/Layout/BlankLayout.php +++ b/core/modules/layout_builder/src/Plugin/Layout/BlankLayout.php @@ -8,8 +8,8 @@ * 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. diff --git a/core/modules/layout_builder/src/SectionListTrait.php b/core/modules/layout_builder/src/SectionListTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..3cb04e0c964705cecbe83b0c2e77caa04c80a6c8 --- /dev/null +++ b/core/modules/layout_builder/src/SectionListTrait.php @@ -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); + } + +} diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php index 410e406c50ced45faf7a4ab71e2ff8642e2904eb..719aa2c8419466439b7bea521651596d7e0e72b6 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php @@ -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; } diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php index f7316ec105178fada28e998c158a1038a2c75a1e..f687bb265b222f6c4f1fe0cca185da9b0be474cb 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php @@ -15,7 +15,7 @@ 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. diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderEntityViewDisplayTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderEntityViewDisplayTest.php index e806c0a58151d4a17b18ba447f3e622af888f909..3d4d2052ec06e7c9064ea866bab04d166d2581d7 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderEntityViewDisplayTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderEntityViewDisplayTest.php @@ -10,12 +10,12 @@ * * @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 @@ protected function getSectionStorage(array $section_data) { */ 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 @@ public function providerTestIsLayoutBuilderEnabled() { */ 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()); } } diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php index 9a5ce86a52b2fc7fccc52f164d4538f1c69e203e..ac25dc0796430b634f834deb33c99b4f7ecac841 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php @@ -14,7 +14,7 @@ * * @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 @@ protected function getSectionStorage(array $section_data) { * @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 @@ public function testEquals() { */ public function testEqualsNonSection() { $list = $this->prophesize(FieldItemListInterface::class); - $this->assertFalse($this->sectionStorage->equals($list->reveal())); + $this->assertFalse($this->sectionList->equals($list->reveal())); } } diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionListTestBase.php b/core/modules/layout_builder/tests/src/Kernel/SectionListTestBase.php new file mode 100644 index 0000000000000000000000000000000000000000..1421375f220e6cf27764e49eb604b91904354884 --- /dev/null +++ b/core/modules/layout_builder/tests/src/Kernel/SectionListTestBase.php @@ -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)); + } + +} diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionListTraitTest.php b/core/modules/layout_builder/tests/src/Kernel/SectionListTraitTest.php index 02be9fefcf152d7d6c92cb4803800c121afff231..723947c5a22bb22361d04162ae4d71bde105872c 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SectionListTraitTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/SectionListTraitTest.php @@ -4,19 +4,19 @@ 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 @@ protected function getSectionStorage(array $section_data) { 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; } diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php index 154e4e13a156da59fc5eb738896219e0025c58b1..3c1f41420d13db48b3ed78bbd8d6ec1c3644b15d 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php +++ b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php @@ -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(). - */ - 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. + * {@inheritdoc} */ - 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); } } diff --git a/core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionStorageTest.php b/core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionListTest.php similarity index 90% rename from core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionStorageTest.php rename to core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionListTest.php index a3f2b8bd8d4e7f3421bd8060273aa33bcbbd0733..6d4c46488da738e681d0205413391c353fcb12a8 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionListTest.php @@ -15,7 +15,7 @@ * * @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();