diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 146b9fd68e..1f6ef85990 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -304,8 +304,10 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin // entities created. if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) { $group = array_rand($referenceable); - $values['target_id'] = array_rand($referenceable[$group]); - return $values; + if (!empty($referenceable[$group])) { + $values['target_id'] = array_rand($referenceable[$group]); + return $values; + } } // Attempt to create a sample entity, avoiding recursion. diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php index dbf68d6179..e40536ab2e 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php @@ -110,18 +110,23 @@ protected function getLatestBlockEntityId() { /** * Removes an entity block from the layout but does not save the layout. */ - protected function removeInlineBlockFromLayout() { + protected function removeInlineBlockFromLayout($selector = NULL) { + $selector = $selector ?? static::INLINE_BLOCK_LOCATOR; $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); - $block_text = $page->find('css', static::INLINE_BLOCK_LOCATOR)->getText(); + $block_text = $page->find('css', $selector)->getText(); $this->assertNotEmpty($block_text); $assert_session->pageTextContains($block_text); - $this->clickContextualLink(static::INLINE_BLOCK_LOCATOR, 'Remove block'); + $this->clickContextualLink($selector, 'Remove block'); $assert_session->waitForElement('css', "#drupal-off-canvas input[value='Remove']"); $assert_session->assertWaitOnAjaxRequest(); + + // Output the new HTML. + $this->htmlOutput($page->getHtml()); + $page->find('css', '#drupal-off-canvas')->pressButton('Remove'); $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); - $assert_session->assertNoElementAfterWait('css', static::INLINE_BLOCK_LOCATOR); + $assert_session->assertNoElementAfterWait('css', $selector); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextNotContains($block_text); } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/WorkspacesBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/WorkspacesBlockTest.php new file mode 100644 index 0000000000..070c4ea7a0 --- /dev/null +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/WorkspacesBlockTest.php @@ -0,0 +1,173 @@ +drupalLogin($this->drupalCreateUser([ + 'access contextual links', + 'configure any layout', + 'administer node display', + 'administer node fields', + 'create and edit custom blocks', + 'administer blocks', + 'administer content types', + 'administer workspaces', + 'view any workspace', + 'administer site configuration', + 'administer nodes', + 'bypass node access', + ])); + $this->setupWorkspaceSwitcherBlock(); + + // Enable layout builder. + $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default'); + $this->submitForm([ + 'layout[enabled]' => TRUE, + 'layout[allow_custom]' => TRUE, + ], 'Save'); + $this->clickLink('Manage layout'); + $this->assertSession()->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout'); + // Add a basic block with the body field set. + $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body'); + $this->assertSaveLayout(); + } + + /** + * Tests changing a layout/blocks inside a workspace. + */ + public function testBlocksInWorkspaces() { + $assert_session = $this->assertSession(); + $this->drupalGet('node/1'); + $assert_session->pageTextContains('The DEFAULT block body'); + $this->drupalGet('node/2'); + $assert_session->pageTextContains('The DEFAULT block body'); + + $stage = Workspace::load('stage'); + $this->switchToWorkspace($stage); + + // Confirm the block can be edited. + $this->drupalGet('node/1/layout'); + $new_block_body = 'The NEW block body'; + $this->configureInlineBlock('The DEFAULT block body', $new_block_body); + $this->assertSaveLayout(); + + $this->drupalGet('node/1'); + $assert_session->pageTextContains($new_block_body); + $assert_session->pageTextNotContains('The DEFAULT block body'); + $this->drupalGet('node/2'); + // Node 2 should use default layout. + $assert_session->pageTextContains('The DEFAULT block body'); + $assert_session->pageTextNotContains($new_block_body); + + // Switch back to the live workspace and verify that the changes are not + // visible there. + $this->switchToLive(); + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains($new_block_body); + $assert_session->pageTextContains('The DEFAULT block body'); + + $this->switchToWorkspace($stage); + // Add a basic block with the body field set. + $this->drupalGet('node/1/layout'); + $second_block_body = 'The 2nd block body'; + $this->addInlineBlockToLayout('2nd Block title', $second_block_body); + $this->assertSaveLayout(); + $this->drupalGet('node/1'); + $assert_session->pageTextContains($second_block_body); + $this->drupalGet('node/2'); + // Node 2 should use default layout. + $assert_session->pageTextContains('The DEFAULT block body'); + $assert_session->pageTextNotContains($new_block_body); + $assert_session->pageTextNotContains($second_block_body); + + // Switch back to the live workspace and verify that the new added block is + // not visible there. + $this->switchToLive(); + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains($second_block_body); + $assert_session->pageTextContains('The DEFAULT block body'); + + $stage->publish(); + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains('The DEFAULT block body'); + $assert_session->pageTextContains($new_block_body); + $assert_session->pageTextContains($second_block_body); + } + + /** + * Tests that blocks can be deleted inside workspaces. + */ + public function testBlockDeletionInWorkspaces() { + $assert_session = $this->assertSession(); + + $stage = Workspace::load('stage'); + $this->switchToWorkspace($stage); + + $this->drupalGet('node/1/layout'); + $workspace_block_content = 'The WORKSPACE block body'; + $this->addInlineBlockToLayout('Workspace block title', $workspace_block_content); + $this->assertSaveLayout(); + + $this->drupalGet('node/1'); + $assert_session->pageTextContains('The DEFAULT block body'); + $assert_session->pageTextContains($workspace_block_content); + + $this->switchToLive(); + $assert_session->pageTextNotContains($workspace_block_content); + + $this->switchToWorkspace($stage); + $this->drupalGet('node/1/layout'); + $this->removeInlineBlockFromLayout(static::INLINE_BLOCK_LOCATOR . ' ~ ' . static::INLINE_BLOCK_LOCATOR); + $this->assertSaveLayout(); + $this->drupalGet('node/1'); + $assert_session->pageTextContains('The DEFAULT block body'); + $assert_session->pageTextNotContains($workspace_block_content); + + $this->drupalGet('node/1/layout'); + $this->removeInlineBlockFromLayout(); + $this->assertSaveLayout(); + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains('The DEFAULT block body'); + $assert_session->pageTextNotContains($workspace_block_content); + + $this->switchToLive(); + $this->drupalGet('node/1'); + $assert_session->pageTextContains('The DEFAULT block body'); + $stage->publish(); + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains('The DEFAULT block body'); + $assert_session->pageTextNotContains($workspace_block_content); + } + +}