You are here

public function EntityGenerateTest::testTransform in Migrate Plus 8.3

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

Tests generating an entity.

@dataProvider transformDataProvider

@covers ::transform

File

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

Class

EntityGenerateTest
Tests the migration plugin.

Namespace

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

Code

public function testTransform(array $definition, array $expected, array $preSeed = []) {

  // Pre seed some test data.
  foreach ($preSeed as $storageName => $values) {

    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage($storageName);
    $entity = $storage
      ->create($values);
    $entity
      ->save();
  }

  /** @var Migration $migration */
  $migration = $this->migrationPluginManager
    ->createStubMigration($definition);

  /** @var EntityStorageBase $storage */
  $storage = $this
    ->readAttribute($migration
    ->getDestinationPlugin(), 'storage');
  $migrationExecutable = new MigrateExecutable($migration, $this);
  $migrationExecutable
    ->import();
  foreach ($expected as $row) {
    $entity = $storage
      ->load($row['id']);
    $properties = array_diff_key($row, array_flip([
      'id',
    ]));
    foreach ($properties as $property => $value) {
      if (is_array($value)) {
        foreach ($value as $key => $expectedValue) {
          if (empty($expectedValue)) {
            $this
              ->assertEmpty($entity->{$property}
              ->getValue(), "Expected value is empty but field {$property} is not empty.");
          }
          elseif ($entity->{$property}
            ->getValue()) {
            $this
              ->assertEquals($expectedValue, $entity->{$property}[0]->entity->{$key}->value);
          }
          else {
            $this
              ->fail("Expected value: {$expectedValue} does not exist in {$property}.");
          }
        }
      }
      else {
        $this
          ->assertNotEmpty($entity, 'Entity with label ' . $row[$property] . ' is empty');
        $this
          ->assertEquals($row[$property], $entity
          ->label());
      }
    }
  }
}