From bb733ec667ec4fcf12fb3459bc02f91b0e0fa6e3 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Fri, 1 Mar 2024 12:12:51 +0000 Subject: [PATCH] Issue #3158916 by DanielVeza, tim.plunkett, mstrelan: Enable Layout Builder via code in tests when that UI is not being tested --- .../src/Functional/LayoutBuilderTest.php | 15 +++-- .../Functional/LayoutBuilderViewModeTest.php | 34 +++++----- .../src/Functional/LayoutDisplayTest.php | 19 +++--- .../LayoutBuilderOptInTest.php | 7 +- .../src/Traits/EnableLayoutBuilderTrait.php | 66 +++++++++++++++++++ 5 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 core/modules/layout_builder/tests/src/Traits/EnableLayoutBuilderTrait.php diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index 71c6674e2875..631159a8b3ef 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -4,8 +4,10 @@ namespace Drupal\Tests\layout_builder\Functional; +use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; use Drupal\layout_builder\Section; use Drupal\node\Entity\Node; +use Drupal\Tests\layout_builder\Traits\EnableLayoutBuilderTrait; /** * Tests the Layout Builder UI. @@ -15,6 +17,8 @@ */ class LayoutBuilderTest extends LayoutBuilderTestBase { + use EnableLayoutBuilderTrait; + /** * Tests the Layout Builder UI for an entity type without a bundle. */ @@ -547,9 +551,9 @@ public function testFormAlter() { ])); $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field'; + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->enableLayoutBuilder($display); $this->drupalGet("$field_ui_prefix/display/default"); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); $page->clickLink('Manage layout'); $page->clickLink('Add block'); @@ -579,11 +583,8 @@ public function testSectionLabels() { 'administer node display', ])); - $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display/default'); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); - $page->checkField('layout[allow_custom]'); - $page->pressButton('Save'); + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->enableLayoutBuilder($display); $this->drupalGet('node/1/layout'); $page->clickLink('Add section'); diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderViewModeTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderViewModeTest.php index 8d468f704052..95ac92066e72 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderViewModeTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderViewModeTest.php @@ -4,6 +4,9 @@ namespace Drupal\Tests\layout_builder\Functional; +use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; +use Drupal\Tests\layout_builder\Traits\EnableLayoutBuilderTrait; + /** * Tests the Layout Builder UI with view modes. * @@ -12,6 +15,8 @@ */ class LayoutBuilderViewModeTest extends LayoutBuilderTestBase { + use EnableLayoutBuilderTrait; + /** * Tests that a non-default view mode works as expected. */ @@ -26,12 +31,10 @@ public function testNonDefaultViewMode() { $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field'; // Allow overrides for the layout. - $this->drupalGet("$field_ui_prefix/display/default"); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); - $page->checkField('layout[allow_custom]'); - $page->pressButton('Save'); + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->enableLayoutBuilder($display); + $this->drupalGet("$field_ui_prefix/display/default"); $this->clickLink('Manage layout'); // Confirm the body field only is shown once. $assert_session->elementsCount('css', '.field--name-body', 1); @@ -43,8 +46,7 @@ public function testNonDefaultViewMode() { $assert_session->addressEquals("$field_ui_prefix/display/teaser"); $assert_session->elementNotExists('css', '#layout-builder__layout'); $assert_session->checkboxNotChecked('layout[enabled]'); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); + $this->enableLayoutBuilderFromUi('bundle_with_section_field', 'teaser', FALSE); $assert_session->linkExists('Manage layout'); $page->clickLink('Manage layout'); // Confirm the body field only is shown once. @@ -60,11 +62,10 @@ public function testNonDefaultViewMode() { $page->checkField('display_modes_custom[full]'); $page->pressButton('Save'); - $assert_session->linkExists('Full content'); - $page->clickLink('Full content'); - $assert_session->addressEquals("$field_ui_prefix/display/full"); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); + // Enable Layout Builder for the full view mode. + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.full'); + $this->enableLayoutBuilder($display); + $this->drupalGet("$field_ui_prefix/display/full"); $assert_session->linkExists('Manage layout'); $page->clickLink('Manage layout'); // Confirm the body field only is shown once. @@ -269,13 +270,10 @@ public function testFullViewModeMultipleBundles() { // Create another bundle without the full view mode enabled. $this->createContentType(['type' => 'default_bundle']); - $this->drupalGet('admin/structure/types/manage/default_bundle/display/default'); - // Enable Layout Builder for defaults and overrides. - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); - $page->checkField('layout[allow_custom]'); - $page->pressButton('Save'); + $display = LayoutBuilderEntityViewDisplay::load('node.default_bundle.default'); + $this->enableLayoutBuilder($display); + $this->drupalGet('admin/structure/types/manage/default_bundle/display/default'); $assert_session->checkboxChecked('layout[allow_custom]'); } diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php index 053011a75af5..1a2291821b7e 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php @@ -4,7 +4,9 @@ namespace Drupal\Tests\layout_builder\Functional; +use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\layout_builder\Traits\EnableLayoutBuilderTrait; /** * Tests functionality of the entity view display with regard to Layout Builder. @@ -13,6 +15,8 @@ */ class LayoutDisplayTest extends BrowserTestBase { + use EnableLayoutBuilderTrait; + /** * {@inheritdoc} */ @@ -50,11 +54,8 @@ public function testMultipleViewModes() { $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field/display'; // Enable Layout Builder for the default view modes, and overrides. - $this->drupalGet("$field_ui_prefix/default"); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); - $page->checkField('layout[allow_custom]'); - $page->pressButton('Save'); + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->enableLayoutBuilder($display); $this->drupalGet('node/1'); $assert_session->pageTextNotContains('Powered by Drupal'); @@ -80,12 +81,8 @@ public function testMultipleViewModes() { $page->pressButton('Save'); // Enable and disable Layout Builder for the new view mode. - $this->drupalGet("$field_ui_prefix/new"); - $page->checkField('layout[enabled]'); - $page->pressButton('Save'); - $page->uncheckField('layout[enabled]'); - $page->pressButton('Save'); - $page->pressButton('Confirm'); + $this->enableLayoutBuilderFromUi('bundle_with_section_field', 'new', FALSE); + $this->disableLayoutBuilderFromUi('bundle_with_section_field', 'new'); // The node using the default view mode still contains its overrides. $this->drupalGet('node/1'); diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderOptInTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderOptInTest.php index 0db8259c6d1e..d8a30accc29a 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderOptInTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderOptInTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\layout_builder\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; +use Drupal\Tests\layout_builder\Traits\EnableLayoutBuilderTrait; /** * Tests the ability for opting in and out of Layout Builder. @@ -13,6 +14,8 @@ */ class LayoutBuilderOptInTest extends WebDriverTestBase { + use EnableLayoutBuilderTrait; + /** * {@inheritdoc} */ @@ -74,9 +77,7 @@ public function testCheckboxLogic() { $assert_session->checkboxChecked('layout[allow_custom]'); // Reset the checkboxes. - $page->uncheckField('layout[enabled]'); - $page->pressButton('Save'); - $page->pressButton('Confirm'); + $this->disableLayoutBuilderFromUi('before', 'default'); $assert_session->checkboxNotChecked('layout[enabled]'); $assert_session->checkboxNotChecked('layout[allow_custom]'); diff --git a/core/modules/layout_builder/tests/src/Traits/EnableLayoutBuilderTrait.php b/core/modules/layout_builder/tests/src/Traits/EnableLayoutBuilderTrait.php new file mode 100644 index 000000000000..201b1f9aff29 --- /dev/null +++ b/core/modules/layout_builder/tests/src/Traits/EnableLayoutBuilderTrait.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\layout_builder\Traits; + +use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; + +/** + * Test trait to enable or disable Layout Builder on an entity view display. + */ +trait EnableLayoutBuilderTrait { + + /** + * Enables Layout Builder on an entity view display. + * + * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display + * The entity view display. + */ + protected function enableLayoutBuilder(LayoutEntityDisplayInterface $display): void { + $display + ->enableLayoutBuilder() + ->setOverridable() + ->save(); + } + + /** + * Enables Layout Builder using the UI. + * + * @param string $bundle + * The bundle that Layout Builder is being enabled on. + * @param string $viewMode + * The view mode that Layout Builder is being enabled on. + * @param bool $allowCustom + * Whether custom layouts per entity should be allowed. + */ + protected function enableLayoutBuilderFromUi(string $bundle, string $viewMode, bool $allowCustom = TRUE): void { + $path = sprintf('admin/structure/types/manage/%s/display/%s', $bundle, $viewMode); + $page = $this->getSession()->getPage(); + $this->drupalGet($path); + $page->checkField('layout[enabled]'); + $page->pressButton('Save'); + if ($allowCustom) { + $page->checkField('layout[allow_custom]'); + $page->pressButton('Save'); + } + } + + /** + * Disables Layout Builder using the UI. + * + * @param string $bundle + * The bundle that Layout Builder is being disabled on. + * @param string $viewMode + * The view mode that Layout Builder is being disabled on. + */ + protected function disableLayoutBuilderFromUi(string $bundle, string $viewMode): void { + $path = sprintf('admin/structure/types/manage/%s/display/%s', $bundle, $viewMode); + $page = $this->getSession()->getPage(); + $this->drupalGet($path); + $page->uncheckField('layout[enabled]'); + $page->pressButton('Save'); + $page->pressButton('Confirm'); + } + +} -- GitLab