You are here

public function EntityGenerateTest::testNonReferenceField in Migrate Plus 8.5

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php \Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process\EntityGenerateTest::testNonReferenceField()

Test lookup without a reference field.

File

tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php, line 227

Class

EntityGenerateTest
Tests the migration plugin.

Namespace

Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process

Code

public function testNonReferenceField() {
  $values = [
    'name' => 'Apples',
    'vid' => $this->vocabulary,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ];
  $this
    ->createTestData('taxonomy_term', $values);

  // Not enough context is provided for a non reference field, so error out.
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'id' => 1,
          'title' => 'content item 1',
          'term' => 'Apples',
        ],
      ],
      'ids' => [
        'id' => [
          'type' => 'integer',
        ],
      ],
    ],
    'process' => [
      'id' => 'id',
      'type' => [
        'plugin' => 'default_value',
        'default_value' => $this->bundle,
      ],
      'title' => 'title',
      'field_integer' => [
        'plugin' => 'entity_generate',
        'source' => 'term',
      ],
    ],
    'destination' => [
      'plugin' => 'entity:node',
    ],
  ];

  /** @var \Drupal\migrate\Plugin\Migration $migration */
  $migration = $this->migrationPluginManager
    ->createStubMigration($definition);
  $migrationExecutable = new MigrateExecutable($migration, $this);
  $migrationExecutable
    ->import();
  $this
    ->assertEquals('Destination field type integer is not a recognized reference type.', $migration
    ->getIdMap()
    ->getMessages()
    ->fetch()->message);
  $this
    ->assertSame(1, $migration
    ->getIdMap()
    ->messageCount());

  // Enough context is provided so this should work.
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'id' => 1,
          'title' => 'content item 1',
          'term' => 'Apples',
        ],
      ],
      'ids' => [
        'id' => [
          'type' => 'integer',
        ],
      ],
    ],
    'process' => [
      'id' => 'id',
      'type' => [
        'plugin' => 'default_value',
        'default_value' => $this->bundle,
      ],
      'title' => 'title',
      'field_integer' => [
        'plugin' => 'entity_generate',
        'source' => 'term',
        'value_key' => 'name',
        'bundle_key' => 'vid',
        'bundle' => $this->vocabulary,
        'entity_type' => 'taxonomy_term',
      ],
    ],
    'destination' => [
      'plugin' => 'entity:node',
    ],
  ];

  /** @var \Drupal\migrate\Plugin\Migration $migration */
  $migration = $this->migrationPluginManager
    ->createStubMigration($definition);
  $migrationExecutable = new MigrateExecutable($migration, $this);
  $migrationExecutable
    ->import();
  $this
    ->assertEmpty($migration
    ->getIdMap()
    ->messageCount());
  $term = Term::load(1);
  $this
    ->assertEquals('Apples', $term
    ->label());
}