You are here

public function MigrateSqlIdMapTest::testGetRowsNeedingUpdate in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testGetRowsNeedingUpdate()
  2. 9 core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testGetRowsNeedingUpdate()

Tests the getRowsNeedingUpdate method for rows that need an update.

File

core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php, line 223

Class

MigrateSqlIdMapTest
Tests the SQL ID map plugin.

Namespace

Drupal\Tests\migrate\Unit

Code

public function testGetRowsNeedingUpdate() {
  $id_map = $this
    ->getIdMap();
  $row_statuses = [
    MigrateIdMapInterface::STATUS_IMPORTED,
    MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
    MigrateIdMapInterface::STATUS_IGNORED,
    MigrateIdMapInterface::STATUS_FAILED,
  ];

  // Create a mapping row for each STATUS constant.
  foreach ($row_statuses as $status) {
    $source = [
      'source_id_property' => 'source_value_' . $status,
    ];
    $row = new Row($source, [
      'source_id_property' => [],
    ]);
    $destination = [
      'destination_id_property' => 'destination_value_' . $status,
    ];
    $id_map
      ->saveIdMapping($row, $destination, $status);
    $expected_results[] = [
      'sourceid1' => 'source_value_' . $status,
      'source_ids_hash' => $this
        ->getIdMap()
        ->getSourceIdsHash($source),
      'destid1' => 'destination_value_' . $status,
      'source_row_status' => $status,
      'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
      'hash' => '',
    ];

    // Assert zero rows need an update.
    if ($status == MigrateIdMapInterface::STATUS_IMPORTED) {
      $rows_needing_update = $id_map
        ->getRowsNeedingUpdate(1);
      $this
        ->assertCount(0, $rows_needing_update);
    }
  }

  // Assert that test values exist.
  $this
    ->queryResultTest($this
    ->getIdMapContents(), $expected_results);

  // Assert a single row needs an update.
  $row_needing_update = $id_map
    ->getRowsNeedingUpdate(1);
  $this
    ->assertCount(1, $row_needing_update);

  // Assert the row matches its original source.
  $source_id = $expected_results[MigrateIdMapInterface::STATUS_NEEDS_UPDATE]['sourceid1'];
  $test_row = $id_map
    ->getRowBySource([
    'source_id_property' => $source_id,
  ]);

  // $row_needing_update is an array of objects returned from the database,
  // but $test_row is an array, so the cast is necessary.
  $this
    ->assertSame($test_row, (array) $row_needing_update[0]);

  // Add additional row that needs an update.
  $source = [
    'source_id_property' => 'source_value_multiple',
  ];
  $row = new Row($source, [
    'source_id_property' => [],
  ]);
  $destination = [
    'destination_id_property' => 'destination_value_multiple',
  ];
  $id_map
    ->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);

  // Assert multiple rows need an update.
  $rows_needing_update = $id_map
    ->getRowsNeedingUpdate(2);
  $this
    ->assertCount(2, $rows_needing_update);
}