You are here

public function SmartSqlMigrateExecutableTest::testSmartSqlMigrateExecutableCompatibility in Smart SQL ID Map 1.1.x

Tests Smart SQL ID map's compatibility with core's MigrateExecutable.

@dataProvider providerTestSmartSqlMigrateExecutableCompatibility

See also

https://drupal.org/i/3227549

https://drupal.org/i/3227660

File

tests/src/Kernel/SmartSqlMigrateExecutableTest.php, line 60

Class

SmartSqlMigrateExecutableTest
Tests Smart SQL ID map's compatibility with core's MigrateExecutable.

Namespace

Drupal\Tests\smart_sql_idmap\Kernel

Code

public function testSmartSqlMigrateExecutableCompatibility(array $source_records) {
  $manager = $this->container
    ->get('plugin.manager.migration');
  assert($manager instanceof MigrationPluginManagerInterface);
  $definition = NestedArray::mergeDeepArray([
    static::MIGRATION,
    [
      'source' => [
        'data_rows' => $source_records,
      ],
    ],
  ], FALSE);
  $migration = $manager
    ->createStubMigration($definition);

  // Populate the ID map plugin.
  $id_map = $migration
    ->getIdMap();
  assert($id_map instanceof SmartSql);
  $executable = new MigrateExecutable($migration);
  $source = $migration
    ->getSourcePlugin();
  $destination = $migration
    ->getDestinationPlugin();
  $destination_ids = $destination
    ->getIds();
  $source
    ->rewind();
  foreach ($source as $row) {
    assert($row instanceof Row);
    $executable
      ->processRow($row);
    $destination_values = $row
      ->getDestination();
    $destination_id_values = array_reduce(array_keys($destination_ids), function (array $carry, string $key) use ($destination_values) {
      $carry[$key] = $destination_values[$key] ?? NULL;
      return $carry;
    }, []);
    $id_map
      ->saveIdMapping($row, $destination_id_values, $row
      ->getSourceProperty('source_row_status') ?? MigrateIdMapInterface::STATUS_IMPORTED, $row
      ->getSourceProperty('rollback_action') ?? MigrateIdMapInterface::ROLLBACK_DELETE);
  }

  // Ensure that the test pre-filled the id map accordingly.
  $this
    ->assertCount(count($source_records), $this
    ->getRecordsOfSqlIdMap($id_map));
  $rollback_result = $executable
    ->rollback();
  $this
    ->assertEquals(MigrationInterface::RESULT_COMPLETED, $rollback_result);
  $this
    ->assertCount(0, $this
    ->getRecordsOfSqlIdMap($id_map));
}