3 namespace Drupal\Tests\migrate\Kernel;
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\taxonomy\Entity\Vocabulary;
9 * Tests rolling back of imports.
13 class MigrateRollbackEntityConfigTest extends MigrateTestBase {
20 public static $modules = ['field', 'taxonomy', 'text', 'language', 'config_translation', 'user'];
25 protected function setUp() {
27 $this->installEntitySchema('user');
28 $this->installEntitySchema('taxonomy_vocabulary');
29 $this->installEntitySchema('taxonomy_term');
30 $this->installConfig(['taxonomy']);
34 * Tests rolling back configuration entity translations.
36 public function testConfigEntityRollback() {
37 // We use vocabularies to demonstrate importing and rolling back
38 // configuration entities with translations. First, import vocabularies.
39 $vocabulary_data_rows = [
40 ['id' => '1', 'name' => 'categories', 'weight' => '2'],
41 ['id' => '2', 'name' => 'tags', 'weight' => '1'],
43 $ids = ['id' => ['type' => 'integer']];
45 'id' => 'vocabularies',
46 'migration_tags' => ['Import and rollback test'],
48 'plugin' => 'embedded_data',
49 'data_rows' => $vocabulary_data_rows,
57 'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
60 /** @var \Drupal\migrate\Plugin\Migration $vocabulary_migration */
61 $vocabulary_migration = \Drupal::service('plugin.manager.migration')
62 ->createStubMigration($definition);
63 $vocabulary_id_map = $vocabulary_migration->getIdMap();
65 $this->assertTrue($vocabulary_migration->getDestinationPlugin()
66 ->supportsRollback());
68 // Import and validate vocabulary config entities were created.
69 $vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
70 $vocabulary_executable->import();
71 foreach ($vocabulary_data_rows as $row) {
72 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
73 $vocabulary = Vocabulary::load($row['id']);
74 $this->assertTrue($vocabulary);
75 $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
76 $this->assertNotNull($map_row['destid1']);
79 // Second, import translations of the vocabulary name property.
80 $vocabulary_i18n_data_rows = [
86 'translation' => 'fr - categories',
93 'translation' => 'fr - tags',
97 'id' => ['type' => 'integer'],
98 'language' => ['type' => 'string'],
101 'id' => 'i18n_vocabularies',
102 'migration_tags' => ['Import and rollback test'],
104 'plugin' => 'embedded_data',
105 'data_rows' => $vocabulary_i18n_data_rows,
113 'langcode' => 'language',
114 'property' => 'constants/name',
115 'translation' => 'translation',
118 'plugin' => 'entity:taxonomy_vocabulary',
119 'translations' => 'true',
123 $vocabulary_i18n__migration = \Drupal::service('plugin.manager.migration')
124 ->createStubMigration($definition);
125 $vocabulary_i18n_id_map = $vocabulary_i18n__migration->getIdMap();
127 $this->assertTrue($vocabulary_i18n__migration->getDestinationPlugin()
128 ->supportsRollback());
130 // Import and validate vocabulary config entities were created.
131 $vocabulary_i18n_executable = new MigrateExecutable($vocabulary_i18n__migration, $this);
132 $vocabulary_i18n_executable->import();
134 $language_manager = \Drupal::service('language_manager');
135 foreach ($vocabulary_i18n_data_rows as $row) {
136 $langcode = $row['language'];
137 $id = 'taxonomy.vocabulary.' . $row['id'];
138 /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
139 $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id);
140 $this->assertSame($row['translation'], $config_translation->get('name'));
141 $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
142 $this->assertNotNull($map_row['destid1']);
145 // Perform the rollback and confirm the translation was deleted and the map
146 // table row removed.
147 $vocabulary_i18n_executable->rollback();
148 foreach ($vocabulary_i18n_data_rows as $row) {
149 $langcode = $row['language'];
150 $id = 'taxonomy.vocabulary.' . $row['id'];
151 /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
152 $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id);
153 $this->assertNull($config_translation->get('name'));
154 $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
155 $this->assertFalse($map_row);
158 // Confirm the original vocabulary still exists.
159 foreach ($vocabulary_data_rows as $row) {
160 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
161 $vocabulary = Vocabulary::load($row['id']);
162 $this->assertTrue($vocabulary);
163 $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
164 $this->assertNotNull($map_row['destid1']);