Commit 51309354 authored by catch's avatar catch
Browse files

Issue #3335269 by Niklan, larowlan: Entity stubs doesn't follows fallback...

Issue #3335269 by Niklan, larowlan: Entity stubs doesn't follows fallback logic from entities and leads to a broken migration
parent 0f77dc4c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -342,9 +342,10 @@ protected function processStubRow(Row $row) {
      $row->setDestinationProperty($bundle_key, reset($this->bundles));
    }

    $bundle = $row->getDestinationProperty($bundle_key) ?? $this->storage->getEntityTypeId();
    // Populate any required fields not already populated.
    $fields = $this->entityFieldManager
      ->getFieldDefinitions($this->storage->getEntityTypeId(), $row->getDestinationProperty($bundle_key));
      ->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle);
    foreach ($fields as $field_name => $field_definition) {
      if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) {
        // Use the configured default value for this specific field, if any.
+48 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\migrate\Kernel;

use Drupal\Core\Entity\EntityFieldManager;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
@@ -10,6 +11,7 @@
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Tests\StubTestTrait;
use Drupal\migrate_entity_test\Entity\StringIdEntityTest;

/**
@@ -19,6 +21,8 @@
 */
class MigrateEntityContentBaseTest extends KernelTestBase {

  use StubTestTrait;

  /**
   * Modules to enable.
   *
@@ -324,4 +328,48 @@ public function testAccountSwitcherBackwardsCompatibility() {
    $this->assertInstanceOf(EntityContentBase::class, $destination);
  }

  /**
   * Tests bundle is properly provided for stubs without bundle support.
   *
   * @todo Remove this test in when native PHP type-hints will be added for
   *   EntityFieldManagerInterface::getFieldDefinitions(). See
   *   https://www.drupal.org/project/drupal/issues/3050720.
   */
  public function testBundleFallbackForStub(): void {
    $this->enableModules(['migrate_entity_test']);
    $this->installEntitySchema('migrate_string_id_entity_test');

    $entity_type_manager = $this->container->get('entity_type.manager');
    $entity_type_bundle_info = $this->container->get('entity_type.bundle.info');
    $entity_display_repository = $this
      ->container
      ->get('entity_display.repository');
    $typed_data_manager = $this->container->get('typed_data_manager');
    $language_manager = $this->container->get('language_manager');
    $keyvalue = $this->container->get('keyvalue');
    $module_handler = $this->container->get('module_handler');
    $cache_discovery = $this->container->get('cache.discovery');
    $entity_last_installed_schema_repository = $this
      ->container
      ->get('entity.last_installed_schema.repository');

    $decorated_entity_field_manager = new class ($entity_type_manager, $entity_type_bundle_info, $entity_display_repository, $typed_data_manager, $language_manager, $keyvalue, $module_handler, $cache_discovery, $entity_last_installed_schema_repository) extends EntityFieldManager {

      /**
       * {@inheritdoc}
       */
      public function getFieldDefinitions($entity_type_id, $bundle) {
        if (\is_null($bundle)) {
          throw new \Exception("Bundle value shouldn't be NULL.");
        }

        return parent::getFieldDefinitions($entity_type_id, $bundle);
      }

    };

    $this->container->set('entity_field.manager', $decorated_entity_field_manager);
    $this->createEntityStub('migrate_string_id_entity_test');
  }

}