View source
<?php
namespace Drupal\Tests\migrate\Kernel;
class TrackChangesTest extends MigrateTestBase {
public static $modules = [
'system',
'user',
'taxonomy',
'migrate',
'migrate_track_changes_test',
'text',
];
protected function setUp() {
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');
$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');
$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');
}
protected function assertTermExists($property, $value) {
self::assertTrue($this
->termExists($property, $value));
}
protected function assertTermDoesNotExist($property, $value) {
self::assertFalse($this
->termExists($property, $value));
}
protected function termExists($property, $value) {
$property = $property === 'description' ? 'description__value' : $property;
$query = \Drupal::entityQuery('taxonomy_term');
$result = $query
->condition($property, $value)
->range(0, 1)
->execute();
return !empty($result);
}
}