Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\migrate\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\migrate\MigrateExecutable;
|
Chris@0
|
6 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests rolling back of imports.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group migrate
|
Chris@0
|
12 */
|
Chris@0
|
13 class MigrateRollbackEntityConfigTest extends MigrateTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Modules to enable.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var array
|
Chris@0
|
19 */
|
Chris@0
|
20 public static $modules = ['field', 'taxonomy', 'text', 'language', 'config_translation', 'user'];
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * {@inheritdoc}
|
Chris@0
|
24 */
|
Chris@0
|
25 protected function setUp() {
|
Chris@0
|
26 parent::setUp();
|
Chris@0
|
27 $this->installEntitySchema('user');
|
Chris@0
|
28 $this->installEntitySchema('taxonomy_vocabulary');
|
Chris@0
|
29 $this->installEntitySchema('taxonomy_term');
|
Chris@0
|
30 $this->installConfig(['taxonomy']);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Tests rolling back configuration entity translations.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function testConfigEntityRollback() {
|
Chris@0
|
37 // We use vocabularies to demonstrate importing and rolling back
|
Chris@0
|
38 // configuration entities with translations. First, import vocabularies.
|
Chris@0
|
39 $vocabulary_data_rows = [
|
Chris@0
|
40 ['id' => '1', 'name' => 'categories', 'weight' => '2'],
|
Chris@0
|
41 ['id' => '2', 'name' => 'tags', 'weight' => '1'],
|
Chris@0
|
42 ];
|
Chris@0
|
43 $ids = ['id' => ['type' => 'integer']];
|
Chris@0
|
44 $definition = [
|
Chris@0
|
45 'id' => 'vocabularies',
|
Chris@0
|
46 'migration_tags' => ['Import and rollback test'],
|
Chris@0
|
47 'source' => [
|
Chris@0
|
48 'plugin' => 'embedded_data',
|
Chris@0
|
49 'data_rows' => $vocabulary_data_rows,
|
Chris@0
|
50 'ids' => $ids,
|
Chris@0
|
51 ],
|
Chris@0
|
52 'process' => [
|
Chris@0
|
53 'vid' => 'id',
|
Chris@0
|
54 'name' => 'name',
|
Chris@0
|
55 'weight' => 'weight',
|
Chris@0
|
56 ],
|
Chris@0
|
57 'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
|
Chris@0
|
58 ];
|
Chris@0
|
59
|
Chris@0
|
60 /** @var \Drupal\migrate\Plugin\Migration $vocabulary_migration */
|
Chris@0
|
61 $vocabulary_migration = \Drupal::service('plugin.manager.migration')
|
Chris@0
|
62 ->createStubMigration($definition);
|
Chris@0
|
63 $vocabulary_id_map = $vocabulary_migration->getIdMap();
|
Chris@0
|
64
|
Chris@0
|
65 $this->assertTrue($vocabulary_migration->getDestinationPlugin()
|
Chris@0
|
66 ->supportsRollback());
|
Chris@0
|
67
|
Chris@0
|
68 // Import and validate vocabulary config entities were created.
|
Chris@0
|
69 $vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
|
Chris@0
|
70 $vocabulary_executable->import();
|
Chris@0
|
71 foreach ($vocabulary_data_rows as $row) {
|
Chris@0
|
72 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
|
Chris@0
|
73 $vocabulary = Vocabulary::load($row['id']);
|
Chris@0
|
74 $this->assertTrue($vocabulary);
|
Chris@0
|
75 $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
|
Chris@0
|
76 $this->assertNotNull($map_row['destid1']);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 // Second, import translations of the vocabulary name property.
|
Chris@0
|
80 $vocabulary_i18n_data_rows = [
|
Chris@0
|
81 [
|
Chris@0
|
82 'id' => '1',
|
Chris@0
|
83 'name' => '1',
|
Chris@0
|
84 'language' => 'fr',
|
Chris@0
|
85 'property' => 'name',
|
Chris@17
|
86 'translation' => 'fr - categories',
|
Chris@0
|
87 ],
|
Chris@0
|
88 [
|
Chris@0
|
89 'id' => '2',
|
Chris@0
|
90 'name' => '2',
|
Chris@0
|
91 'language' => 'fr',
|
Chris@0
|
92 'property' => 'name',
|
Chris@17
|
93 'translation' => 'fr - tags',
|
Chris@0
|
94 ],
|
Chris@0
|
95 ];
|
Chris@0
|
96 $ids = [
|
Chris@0
|
97 'id' => ['type' => 'integer'],
|
Chris@0
|
98 'language' => ['type' => 'string'],
|
Chris@0
|
99 ];
|
Chris@0
|
100 $definition = [
|
Chris@0
|
101 'id' => 'i18n_vocabularies',
|
Chris@0
|
102 'migration_tags' => ['Import and rollback test'],
|
Chris@0
|
103 'source' => [
|
Chris@0
|
104 'plugin' => 'embedded_data',
|
Chris@0
|
105 'data_rows' => $vocabulary_i18n_data_rows,
|
Chris@0
|
106 'ids' => $ids,
|
Chris@0
|
107 'constants' => [
|
Chris@0
|
108 'name' => 'name',
|
Chris@17
|
109 ],
|
Chris@0
|
110 ],
|
Chris@0
|
111 'process' => [
|
Chris@0
|
112 'vid' => 'id',
|
Chris@0
|
113 'langcode' => 'language',
|
Chris@0
|
114 'property' => 'constants/name',
|
Chris@0
|
115 'translation' => 'translation',
|
Chris@0
|
116 ],
|
Chris@0
|
117 'destination' => [
|
Chris@0
|
118 'plugin' => 'entity:taxonomy_vocabulary',
|
Chris@0
|
119 'translations' => 'true',
|
Chris@0
|
120 ],
|
Chris@0
|
121 ];
|
Chris@0
|
122
|
Chris@0
|
123 $vocabulary_i18n__migration = \Drupal::service('plugin.manager.migration')
|
Chris@0
|
124 ->createStubMigration($definition);
|
Chris@0
|
125 $vocabulary_i18n_id_map = $vocabulary_i18n__migration->getIdMap();
|
Chris@0
|
126
|
Chris@0
|
127 $this->assertTrue($vocabulary_i18n__migration->getDestinationPlugin()
|
Chris@0
|
128 ->supportsRollback());
|
Chris@0
|
129
|
Chris@0
|
130 // Import and validate vocabulary config entities were created.
|
Chris@0
|
131 $vocabulary_i18n_executable = new MigrateExecutable($vocabulary_i18n__migration, $this);
|
Chris@0
|
132 $vocabulary_i18n_executable->import();
|
Chris@0
|
133
|
Chris@0
|
134 $language_manager = \Drupal::service('language_manager');
|
Chris@0
|
135 foreach ($vocabulary_i18n_data_rows as $row) {
|
Chris@0
|
136 $langcode = $row['language'];
|
Chris@0
|
137 $id = 'taxonomy.vocabulary.' . $row['id'];
|
Chris@0
|
138 /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
|
Chris@0
|
139 $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id);
|
Chris@0
|
140 $this->assertSame($row['translation'], $config_translation->get('name'));
|
Chris@0
|
141 $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
|
Chris@0
|
142 $this->assertNotNull($map_row['destid1']);
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 // Perform the rollback and confirm the translation was deleted and the map
|
Chris@0
|
146 // table row removed.
|
Chris@0
|
147 $vocabulary_i18n_executable->rollback();
|
Chris@0
|
148 foreach ($vocabulary_i18n_data_rows as $row) {
|
Chris@0
|
149 $langcode = $row['language'];
|
Chris@0
|
150 $id = 'taxonomy.vocabulary.' . $row['id'];
|
Chris@0
|
151 /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
|
Chris@0
|
152 $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id);
|
Chris@0
|
153 $this->assertNull($config_translation->get('name'));
|
Chris@0
|
154 $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
|
Chris@0
|
155 $this->assertFalse($map_row);
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 // Confirm the original vocabulary still exists.
|
Chris@0
|
159 foreach ($vocabulary_data_rows as $row) {
|
Chris@0
|
160 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
|
Chris@0
|
161 $vocabulary = Vocabulary::load($row['id']);
|
Chris@0
|
162 $this->assertTrue($vocabulary);
|
Chris@0
|
163 $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
|
Chris@0
|
164 $this->assertNotNull($map_row['destid1']);
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 }
|