View source
<?php
namespace Drupal\Tests\migrate\Kernel;
class TrackChangesTest extends MigrateTestBase {
protected static $modules = [
'system',
'user',
'taxonomy',
'migrate',
'migrate_track_changes_test',
'text',
];
protected function setUp() : void {
parent::setUp();
$this->sourceDatabase
->schema()
->createTable('track_changes_term', [
'fields' => [
'tid' => [
'description' => 'Serial',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => 'Name',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'description' => [
'description' => 'Name',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
],
],
'primary key' => [
'tid',
],
'description' => 'Contains taxonomy terms to import',
]);
$this->sourceDatabase
->insert('track_changes_term')
->fields([
'name',
'description',
])
->values([
'name' => 'Item 1',
'description' => 'Text item 1',
])
->values([
'name' => 'Item 2',
'description' => 'Text item 2',
])
->values([
'name' => 'Item 3',
'description' => 'Text item 3',
])
->values([
'name' => 'Item 4',
'description' => 'Text item 4',
])
->execute();
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('user');
$this
->executeMigration('track_changes_test');
}
public function testTrackChanges() {
$this
->assertTermExists('name', 'Item 1');
$this
->assertTermExists('name', 'Item 2');
$this
->assertTermExists('description', 'Text item 3');
$this
->assertTermExists('description', 'Text item 4');
$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);
$this->sourceDatabase
->update('track_changes_term')
->fields([
'name' => 'Item 1 updated',
])
->condition('name', 'Item 1')
->execute();
$this->sourceDatabase
->update('track_changes_term')
->fields([
'name' => 'Item 2',
])
->condition('name', 'Item 2')
->execute();
$this->sourceDatabase
->update('track_changes_term')
->fields([
'description' => 'Text item 3 updated',
])
->condition('name', 'Item 3')
->execute();
$this->sourceDatabase
->update('track_changes_term')
->fields([
'description' => 'Text item 4',
])
->condition('name', 'Item 4')
->execute();
$this
->executeMigration('track_changes_test');
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]);
$this
->assertTermExists('name', 'Item 1 updated');
$this
->assertTermDoesNotExist('name', 'Item 1');
$this
->assertTermExists('name', 'Item 2');
$this
->assertTermExists('description', 'Text item 3 updated');
$this
->assertTermDoesNotExist('description', 'Text item 3');
$this
->assertTermExists('description', 'Text item 4');
$id_map
->prepareUpdate();
$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]);
}
protected function assertTermExists(string $property, string $value) : void {
self::assertTrue($this
->termExists($property, $value));
}
protected function assertTermDoesNotExist(string $property, string $value) : void {
self::assertFalse($this
->termExists($property, $value));
}
protected function termExists($property, $value) {
$property = $property === 'description' ? 'description__value' : $property;
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE);
$result = $query
->condition($property, $value)
->range(0, 1)
->execute();
return !empty($result);
}
}