You are here

public function RowTest::testHashing in Drupal 8

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

Tests hashing.

File

core/modules/migrate/tests/src/Unit/RowTest.php, line 151

Class

RowTest
@coversDefaultClass \Drupal\migrate\Row @group migrate

Namespace

Drupal\Tests\migrate\Unit

Code

public function testHashing() {
  $row = new Row($this->testValues, $this->testSourceIds);
  $this
    ->assertSame('', $row
    ->getHash(), 'No hash at creation');
  $row
    ->rehash();
  $this
    ->assertSame($this->testHash, $row
    ->getHash(), 'Correct hash.');
  $row
    ->rehash();
  $this
    ->assertSame($this->testHash, $row
    ->getHash(), 'Correct hash even doing it twice.');

  // Set the map to needs update.
  $test_id_map = [
    'original_hash' => '',
    'hash' => '',
    'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
  ];
  $row
    ->setIdMap($test_id_map);
  $this
    ->assertTrue($row
    ->needsUpdate());
  $row
    ->rehash();
  $this
    ->assertSame($this->testHash, $row
    ->getHash(), 'Correct hash even if id_mpa have changed.');
  $row
    ->setSourceProperty('title', 'new title');
  $row
    ->rehash();
  $this
    ->assertSame($this->testHashMod, $row
    ->getHash(), 'Hash changed correctly.');

  // Check hash calculation algorithm.
  $hash = hash('sha256', serialize($row
    ->getSource()));
  $this
    ->assertSame($hash, $row
    ->getHash());

  // Check length of generated hash used for mapping schema.
  $this
    ->assertSame(64, strlen($row
    ->getHash()));

  // Set the map to successfully imported.
  $test_id_map = [
    'original_hash' => '',
    'hash' => '',
    'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
  ];
  $row
    ->setIdMap($test_id_map);
  $this
    ->assertFalse($row
    ->needsUpdate());

  // Set the same hash value and ensure it was not changed.
  $random = $this
    ->randomMachineName();
  $test_id_map = [
    'original_hash' => $random,
    'hash' => $random,
    'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
  ];
  $row
    ->setIdMap($test_id_map);
  $this
    ->assertFalse($row
    ->changed());

  // Set different has values to ensure it is marked as changed.
  $test_id_map = [
    'original_hash' => $this
      ->randomMachineName(),
    'hash' => $this
      ->randomMachineName(),
    'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
  ];
  $row
    ->setIdMap($test_id_map);
  $this
    ->assertTrue($row
    ->changed());
}