Unverified Commit d7f5dbc0 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3493287 by phenaproxima, alexpott, longwave, tim.plunkett: The default...

Issue #3493287 by phenaproxima, alexpott, longwave, tim.plunkett: The default content importer should handle Layout Builder section data

(cherry picked from commit a44cf1ef)
parent c2ff9ac2
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,9 @@ class SectionData extends TypedData {
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    if (is_array($value)) {
      $value = Section::fromArray($value);
    }
    if ($value && !$value instanceof Section) {
      throw new \InvalidArgumentException(sprintf('Value assigned to "%s" is not a valid section', $this->getName()));
    }
+40 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\layout_builder\Kernel;

use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\layout_builder\Section;

/**
 * @coversDefaultClass \Drupal\layout_builder\Plugin\DataType\SectionData
 *
 * @group layout_builder
 */
class SectionDataTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['layout_builder'];

  /**
   * @covers ::setValue
   */
  public function testSetArrayValue(): void {
    $definition = DataDefinition::create('layout_section');
    $data = $this->container->get(TypedDataManagerInterface::class)
      ->create($definition, name: 'test_section');

    // If an array is passed, it's converted to a Section object.
    $data->setValue([]);
    $this->assertInstanceOf(Section::class, $data->getValue());
    // Anything else should raise an exception.
    $this->expectExceptionMessage('Value assigned to "test_section" is not a valid section');
    $data->setValue('[]');
  }

}
+19 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
use Drupal\Core\DefaultContent\Importer;
use Drupal\Core\DefaultContent\ImportException;
use Drupal\Core\DefaultContent\InvalidEntityException;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\File\FileExists;
use Drupal\Core\Url;
@@ -22,6 +23,7 @@
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\layout_builder\Section;
use Drupal\media\MediaInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\node\NodeInterface;
@@ -57,6 +59,7 @@ class ContentImportTest extends BrowserTestBase {
    'block_content',
    'content_translation',
    'entity_test',
    'layout_builder',
    'media',
    'menu_link_content',
    'node',
@@ -108,6 +111,13 @@ protected function setUp(): void {

    $this->contentDir = $this->getDrupalRoot() . '/core/tests/fixtures/default_content';
    \Drupal::service('file_system')->copy($this->contentDir . '/file/druplicon_copy.png', $this->publicFilesDirectory . '/druplicon_copy.png', FileExists::Error);

    // Enable Layout Builder for the Page content type, with custom overrides.
    \Drupal::service(EntityDisplayRepositoryInterface::class)
      ->getViewDisplay('node', 'page')
      ->enableLayoutBuilder()
      ->setOverridable()
      ->save();
  }

  /**
@@ -259,6 +269,15 @@ private function assertContentWasImported(): void {
    $translation = $node->getTranslation('fr');
    $this->assertSame('Perdu en traduction', $translation->label());
    $this->assertSame("Içi c'est la version français.", $translation->body->value);

    // Layout data should be imported.
    $node = $entity_repository->loadEntityByUuid('node', '32650de8-9edd-48dc-80b8-8bda180ebbac');
    $this->assertInstanceOf(NodeInterface::class, $node);
    $section = $node->layout_builder__layout[0]->section;
    $this->assertInstanceOf(Section::class, $section);
    $this->assertCount(2, $section->getComponents());
    $this->assertSame('system_powered_by_block', $section->getComponent('03b45f14-cf74-469a-8398-edf3383ce7fa')->getPluginId());

  }

}
+60 −0
Original line number Diff line number Diff line
_meta:
  version: '1.0'
  entity_type: node
  uuid: 32650de8-9edd-48dc-80b8-8bda180ebbac
  bundle: page
  default_langcode: en
default:
  revision_uid:
    -
      target_id: 1
  status:
    -
      value: true
  uid:
    -
      target_id: 1
  title:
    -
      value: 'Layout override'
  created:
    -
      value: 1733930395
  promote:
    -
      value: true
  sticky:
    -
      value: false
  revision_translation_affected:
    -
      value: true
  layout_builder__layout:
    -
      section:
        layout_id: layout_onecol
        layout_settings:
          label: ''
        components:
          199f7eea-3e6e-4a91-bf5e-7b1d6f13fecd:
            uuid: 199f7eea-3e6e-4a91-bf5e-7b1d6f13fecd
            region: content
            configuration:
              id: 'extra_field_block:node:test:links'
              label_display: '0'
              context_mapping:
                entity: layout_builder.entity
            weight: 1
            additional: {  }
          03b45f14-cf74-469a-8398-edf3383ce7fa:
            uuid: 03b45f14-cf74-469a-8398-edf3383ce7fa
            region: content
            configuration:
              id: system_powered_by_block
              label: 'Powered by Drupal'
              label_display: 0
              provider: system
              context_mapping: {  }
            weight: 0
            additional: {  }
        third_party_settings: {  }