function TrackChangesTest::testTrackChanges

Same name in other branches
  1. 9 core/modules/migrate/tests/src/Kernel/TrackChangesTest.php \Drupal\Tests\migrate\Kernel\TrackChangesTest::testTrackChanges()
  2. 8.9.x core/modules/migrate/tests/src/Kernel/TrackChangesTest.php \Drupal\Tests\migrate\Kernel\TrackChangesTest::testTrackChanges()
  3. 10 core/modules/migrate/tests/src/Kernel/TrackChangesTest.php \Drupal\Tests\migrate\Kernel\TrackChangesTest::testTrackChanges()

Tests track changes property of SqlBase.

File

core/modules/migrate/tests/src/Kernel/TrackChangesTest.php, line 94

Class

TrackChangesTest
Tests migration track changes property.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testTrackChanges() : void {
    // Assert all of the terms have been imported.
    $this->assertTermExists('name', 'Item 1');
    $this->assertTermExists('name', 'Item 2');
    $this->assertTermExists('description', 'Text item 3');
    $this->assertTermExists('description', 'Text item 4');
    // Save the original hash, rerun the migration and check that the hashes
    // are the same.
    $id_map = $this->migration
        ->getIdMap();
    for ($i = 1; $i < 5; $i++) {
        $row = $id_map->getRowBySource([
            'tid' => $i,
        ]);
        $original_hash[$i] = $row['hash'];
    }
    $this->executeMigration($this->migration);
    for ($i = 1; $i < 5; $i++) {
        $row = $id_map->getRowBySource([
            'tid' => $i,
        ]);
        $new_hash[$i] = $row['hash'];
    }
    $this->assertEquals($original_hash, $new_hash);
    // Update Item 1 triggering its track_changes by name.
    $this->sourceDatabase
        ->update('track_changes_term')
        ->fields([
        'name' => 'Item 1 updated',
    ])
        ->condition('name', 'Item 1')
        ->execute();
    // Update Item 2 keeping it's track_changes name the same.
    $this->sourceDatabase
        ->update('track_changes_term')
        ->fields([
        'name' => 'Item 2',
    ])
        ->condition('name', 'Item 2')
        ->execute();
    // Update Item 3 triggering its track_changes by field.
    $this->sourceDatabase
        ->update('track_changes_term')
        ->fields([
        'description' => 'Text item 3 updated',
    ])
        ->condition('name', 'Item 3')
        ->execute();
    // Update Item 2 keeping it's track_changes field the same.
    $this->sourceDatabase
        ->update('track_changes_term')
        ->fields([
        'description' => 'Text item 4',
    ])
        ->condition('name', 'Item 4')
        ->execute();
    // Execute migration again.
    $this->executeMigration($this->migration);
    // Check that the all the hashes except for 'Item 2'and 'Item 4' have
    // changed.
    for ($i = 1; $i < 5; $i++) {
        $row = $id_map->getRowBySource([
            'tid' => $i,
        ]);
        $new_hash[$i] = $row['hash'];
    }
    $this->assertNotEquals($original_hash[1], $new_hash[1]);
    $this->assertEquals($original_hash[2], $new_hash[2]);
    $this->assertNotEquals($original_hash[3], $new_hash[3]);
    $this->assertEquals($original_hash[4], $new_hash[4]);
    // Item with name changes should be updated.
    $this->assertTermExists('name', 'Item 1 updated');
    $this->assertTermDoesNotExist('name', 'Item 1');
    // Item without name changes should not be updated.
    $this->assertTermExists('name', 'Item 2');
    // Item with field changes should be updated.
    $this->assertTermExists('description', 'Text item 3 updated');
    $this->assertTermDoesNotExist('description', 'Text item 3');
    // Item without field changes should not be updated.
    $this->assertTermExists('description', 'Text item 4');
    // Test hashes again after forcing all rows to be re-imported.
    $id_map->prepareUpdate();
    // Execute migration again.
    $this->executeMigration('track_changes_test');
    for ($i = 1; $i < 5; $i++) {
        $row = $id_map->getRowBySource([
            'tid' => $i,
        ]);
        $newer_hash[$i] = $row['hash'];
    }
    $this->assertEquals($new_hash[1], $newer_hash[1]);
    $this->assertEquals($new_hash[2], $newer_hash[2]);
    $this->assertEquals($new_hash[3], $newer_hash[3]);
    $this->assertEquals($new_hash[4], $newer_hash[4]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.