You are here

public function MigrateUpgradeCommerce1Test::testMigrateUpgrade in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/tests/src/Functional/commerce1/MigrateUpgradeCommerce1Test.php \Drupal\Tests\commerce_migrate_commerce\Functional\commerce1\MigrateUpgradeCommerce1Test::testMigrateUpgrade()
  2. 3.1.x modules/commerce/tests/src/Functional/commerce1/MigrateUpgradeCommerce1Test.php \Drupal\Tests\commerce_migrate_commerce\Functional\commerce1\MigrateUpgradeCommerce1Test::testMigrateUpgrade()

Executes all steps of migrations upgrade.

File

modules/commerce/tests/src/Functional/commerce1/MigrateUpgradeCommerce1Test.php, line 94

Class

MigrateUpgradeCommerce1Test
Tests Commerce 1 upgrade using the migrate UI.

Namespace

Drupal\Tests\commerce_migrate_commerce\Functional\commerce1

Code

public function testMigrateUpgrade() {
  $connection_options = $this->sourceDatabase
    ->getConnectionOptions();
  $session = $this
    ->assertSession();
  $driver = $connection_options['driver'];
  $connection_options['prefix'] = $connection_options['prefix']['default'];

  // Use the driver connection form to get the correct options out of the
  // database settings. This supports all of the databases we test against.
  $drivers = drupal_get_database_types();
  $form = $drivers[$driver]
    ->getFormOptions($connection_options);
  $connection_options = array_intersect_key($connection_options, $form + $form['advanced_options']);
  $version = $this
    ->getLegacyDrupalVersion($this->sourceDatabase);
  $edit = [
    $driver => $connection_options,
    'source_private_file_path' => $this
      ->getSourceBasePath(),
    'version' => $version,
  ];
  if ($version == 6) {
    $edit['d6_source_base_path'] = $this
      ->getSourceBasePath();
  }
  else {
    $edit['source_base_path'] = $this
      ->getSourceBasePath();
  }
  if (count($drivers) !== 1) {
    $edit['driver'] = $driver;
  }
  $edits = $this
    ->translatePostValues($edit);

  // Start the upgrade process.
  $this
    ->drupalGet('/upgrade');
  $this
    ->drupalPostForm(NULL, [], 'Continue');
  $session
    ->pageTextContains('Provide credentials for the database of the Drupal site you want to upgrade.');
  $session
    ->fieldExists('mysql[host]');
  $this
    ->drupalPostForm(NULL, $edits, 'Review upgrade');
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [], 'Perform upgrade');

  // Have to reset all the statics after migration to ensure entities are
  // loadable.
  $this
    ->resetAll();
  $expected_counts = $this
    ->getEntityCounts();
  foreach (array_keys(\Drupal::entityTypeManager()
    ->getDefinitions()) as $entity_type) {
    $real_count = \Drupal::entityQuery($entity_type)
      ->count()
      ->execute();
    $expected_count = isset($expected_counts[$entity_type]) ? $expected_counts[$entity_type] : 0;
    self::assertEquals($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.
      if ($row['source_row_status'] == MigrateIdMapInterface::STATUS_FAILED || $row['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE) {
        $this
          ->fail($message);
      }
      else {
        self::assertNotEmpty($message);
      }
    }
  }
}