You are here

protected function MigrateUpgradeTestBase::assertMigrationResults in Drupal 8

Checks that migrations have been performed successfully.

Parameters

array $expected_counts: The expected counts of each entity type.

int $version: The Drupal version.

1 call to MigrateUpgradeTestBase::assertMigrationResults()
MigrateUpgradeExecuteTestBase::testMigrateUpgradeExecute in core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeExecuteTestBase.php
Executes all steps of migrations upgrade.

File

core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php, line 222

Class

MigrateUpgradeTestBase
Provides a base class for testing migration upgrades in the UI.

Namespace

Drupal\Tests\migrate_drupal_ui\Functional

Code

protected function assertMigrationResults(array $expected_counts, $version) {

  // Have to reset all the statics after migration to ensure entities are
  // loadable.
  $this
    ->resetAll();

  // Check that the expected number of entities is the same as the actual
  // number of entities.
  $entity_definitions = array_keys(\Drupal::entityTypeManager()
    ->getDefinitions());
  $expected_count_keys = array_keys($expected_counts);
  sort($entity_definitions);
  sort($expected_count_keys);
  $this
    ->assertSame($expected_count_keys, $entity_definitions);

  // Assert the correct number of entities exist.
  foreach ($entity_definitions as $entity_type) {
    $real_count = (int) \Drupal::entityQuery($entity_type)
      ->count()
      ->execute();
    $expected_count = $expected_counts[$entity_type];
    $this
      ->assertSame($expected_count, $real_count, "Found {$real_count} {$entity_type} entities, expected {$expected_count}.");
  }
  $plugin_manager = \Drupal::service('plugin.manager.migration');

  /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
  $all_migrations = $plugin_manager
    ->createInstancesByTag('Drupal ' . $version);
  foreach ($all_migrations as $migration) {
    $id_map = $migration
      ->getIdMap();
    foreach ($id_map as $source_id => $map) {

      // Convert $source_id into a keyless array so that
      // \Drupal\migrate\Plugin\migrate\id_map\Sql::getSourceHash() works as
      // expected.
      $source_id_values = array_values(unserialize($source_id));
      $row = $id_map
        ->getRowBySource($source_id_values);
      $destination = serialize($id_map
        ->currentDestination());
      $message = "Migration of {$source_id} to {$destination} as part of the {$migration->id()} migration. The source row status is " . $row['source_row_status'];

      // A completed migration should have maps with
      // MigrateIdMapInterface::STATUS_IGNORED or
      // MigrateIdMapInterface::STATUS_IMPORTED.
      $this
        ->assertNotSame(MigrateIdMapInterface::STATUS_FAILED, $row['source_row_status'], $message);
      $this
        ->assertNotSame(MigrateIdMapInterface::STATUS_NEEDS_UPDATE, $row['source_row_status'], $message);
    }
  }
}