You are here

public function MigrateEntityContentBaseTest::testEmptyDestinations in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php \Drupal\Tests\migrate\Kernel\MigrateEntityContentBaseTest::testEmptyDestinations()

Tests empty destinations.

File

core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php, line 216

Class

MigrateEntityContentBaseTest
Tests the EntityContentBase destination.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testEmptyDestinations() {
  $this
    ->enableModules([
    'migrate_entity_test',
  ]);
  $this
    ->installEntitySchema('migrate_string_id_entity_test');
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'id' => 123,
          'version' => 'foo',
        ],
        // This integer needs an 'int' schema with 'big' size. If 'destid1'
        // is not correctly taking the definition from the destination entity
        // type, the import will fail with an SQL exception.
        [
          'id' => 123456789012,
          'version' => 'bar',
        ],
      ],
      'ids' => [
        'id' => [
          'type' => 'integer',
          'size' => 'big',
        ],
        'version' => [
          'type' => 'string',
        ],
      ],
      'constants' => [
        'null' => NULL,
      ],
    ],
    'process' => [
      'id' => 'id',
      'version' => 'version',
    ],
    'destination' => [
      'plugin' => 'entity:migrate_string_id_entity_test',
    ],
  ];
  $migration = \Drupal::service('plugin.manager.migration')
    ->createStubMigration($definition);
  $executable = new MigrateExecutable($migration);
  $executable
    ->import();

  /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
  $entity = StringIdEntityTest::load('123');
  $this
    ->assertSame('foo', $entity->version->value);
  $entity = StringIdEntityTest::load('123456789012');
  $this
    ->assertSame('bar', $entity->version->value);

  // Rerun the migration forcing the version to NULL.
  $definition['process'] = [
    'id' => 'id',
    'version' => 'constants/null',
  ];
  $migration = \Drupal::service('plugin.manager.migration')
    ->createStubMigration($definition);
  $executable = new MigrateExecutable($migration);
  $executable
    ->import();

  /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
  $entity = StringIdEntityTest::load('123');
  $this
    ->assertNull($entity->version->value);
  $entity = StringIdEntityTest::load('123456789012');
  $this
    ->assertNull($entity->version->value);
}