View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\MigrateExecutable;
use Drupal\taxonomy\Entity\Vocabulary;
class MigrateRollbackEntityConfigTest extends MigrateTestBase {
protected static $modules = [
'field',
'taxonomy',
'text',
'language',
'config_translation',
'user',
'system',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('taxonomy_vocabulary');
$this
->installEntitySchema('taxonomy_term');
$this
->installConfig([
'taxonomy',
]);
}
public function testConfigEntityRollback() {
$vocabulary_data_rows = [
[
'id' => '1',
'name' => 'categories',
'weight' => '2',
],
[
'id' => '2',
'name' => 'tags',
'weight' => '1',
],
];
$ids = [
'id' => [
'type' => 'integer',
],
];
$definition = [
'id' => 'vocabularies',
'migration_tags' => [
'Import and rollback test',
],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => $vocabulary_data_rows,
'ids' => $ids,
],
'process' => [
'vid' => 'id',
'name' => 'name',
'weight' => 'weight',
],
'destination' => [
'plugin' => 'entity:taxonomy_vocabulary',
],
];
$vocabulary_migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$vocabulary_id_map = $vocabulary_migration
->getIdMap();
$this
->assertTrue($vocabulary_migration
->getDestinationPlugin()
->supportsRollback());
$vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
$vocabulary_executable
->import();
foreach ($vocabulary_data_rows as $row) {
$vocabulary = Vocabulary::load($row['id']);
$this
->assertNotEmpty($vocabulary);
$map_row = $vocabulary_id_map
->getRowBySource([
'id' => $row['id'],
]);
$this
->assertNotNull($map_row['destid1']);
}
$vocabulary_i18n_data_rows = [
[
'id' => '1',
'name' => '1',
'language' => 'fr',
'property' => 'name',
'translation' => 'fr - categories',
],
[
'id' => '2',
'name' => '2',
'language' => 'fr',
'property' => 'name',
'translation' => 'fr - tags',
],
];
$ids = [
'id' => [
'type' => 'integer',
],
'language' => [
'type' => 'string',
],
];
$definition = [
'id' => 'i18n_vocabularies',
'migration_tags' => [
'Import and rollback test',
],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => $vocabulary_i18n_data_rows,
'ids' => $ids,
'constants' => [
'name' => 'name',
],
],
'process' => [
'vid' => 'id',
'langcode' => 'language',
'property' => 'constants/name',
'translation' => 'translation',
],
'destination' => [
'plugin' => 'entity:taxonomy_vocabulary',
'translations' => 'true',
],
];
$vocabulary_i18n__migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$vocabulary_i18n_id_map = $vocabulary_i18n__migration
->getIdMap();
$this
->assertTrue($vocabulary_i18n__migration
->getDestinationPlugin()
->supportsRollback());
$vocabulary_i18n_executable = new MigrateExecutable($vocabulary_i18n__migration, $this);
$vocabulary_i18n_executable
->import();
$language_manager = \Drupal::service('language_manager');
foreach ($vocabulary_i18n_data_rows as $row) {
$langcode = $row['language'];
$id = 'taxonomy.vocabulary.' . $row['id'];
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, $id);
$this
->assertSame($row['translation'], $config_translation
->get('name'));
$map_row = $vocabulary_i18n_id_map
->getRowBySource([
'id' => $row['id'],
'language' => $row['language'],
]);
$this
->assertNotNull($map_row['destid1']);
}
$vocabulary_i18n_executable
->rollback();
foreach ($vocabulary_i18n_data_rows as $row) {
$langcode = $row['language'];
$id = 'taxonomy.vocabulary.' . $row['id'];
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, $id);
$this
->assertNull($config_translation
->get('name'));
$map_row = $vocabulary_i18n_id_map
->getRowBySource([
'id' => $row['id'],
'language' => $row['language'],
]);
$this
->assertFalse($map_row);
}
foreach ($vocabulary_data_rows as $row) {
$vocabulary = Vocabulary::load($row['id']);
$this
->assertNotEmpty($vocabulary);
$map_row = $vocabulary_id_map
->getRowBySource([
'id' => $row['id'],
]);
$this
->assertNotNull($map_row['destid1']);
}
}
}