You are here

public function EntityLookupTest::testLookupEntityWithoutBundles in Migrate Plus 8.5

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

Lookup an entity without bundles on destination key.

Using user entity as destination entity without bundles as example for testing.

@covers ::transform

File

tests/src/Kernel/Plugin/migrate/process/EntityLookupTest.php, line 49

Class

EntityLookupTest
Tests the entity_lookup plugin.

Namespace

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

Code

public function testLookupEntityWithoutBundles() : void {

  // Create a user.
  $known_user = $this
    ->createUser([], 'lucuma');

  // Setup test migration objects.
  $migration_prophecy = $this
    ->prophesize(MigrationInterface::class);
  $migrate_destination_prophecy = $this
    ->prophesize(MigrateDestinationInterface::class);
  $migrate_destination_prophecy
    ->getPluginId()
    ->willReturn('user');
  $migrate_destination = $migrate_destination_prophecy
    ->reveal();
  $migration_prophecy
    ->getDestinationPlugin()
    ->willReturn($migrate_destination);
  $migration_prophecy
    ->getProcess()
    ->willReturn([]);
  $migration = $migration_prophecy
    ->reveal();
  $configuration = [
    'entity_type' => 'user',
    'value_key' => 'name',
  ];
  $plugin = \Drupal::service('plugin.manager.migrate.process')
    ->createInstance('entity_lookup', $configuration, $migration);
  $executable = $this
    ->prophesize(MigrateExecutableInterface::class)
    ->reveal();
  $row = new Row();

  // Check the known user is found.
  $value = $plugin
    ->transform('lucuma', $executable, $row, 'name');
  $this
    ->assertSame($known_user
    ->id(), $value);

  // Check an unknown user is not found.
  $value = $plugin
    ->transform('orange', $executable, $row, 'name');
  $this
    ->assertNull($value);
}