comparison core/modules/migrate/tests/src/Kernel/MigrateRollbackEntityConfigTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\taxonomy\Entity\Vocabulary;
7
8 /**
9 * Tests rolling back of imports.
10 *
11 * @group migrate
12 */
13 class MigrateRollbackEntityConfigTest extends MigrateTestBase {
14
15 /**
16 * Modules to enable.
17 *
18 * @var array
19 */
20 public static $modules = ['field', 'taxonomy', 'text', 'language', 'config_translation', 'user'];
21
22 /**
23 * {@inheritdoc}
24 */
25 protected function setUp() {
26 parent::setUp();
27 $this->installEntitySchema('user');
28 $this->installEntitySchema('taxonomy_vocabulary');
29 $this->installEntitySchema('taxonomy_term');
30 $this->installConfig(['taxonomy']);
31 }
32
33 /**
34 * Tests rolling back configuration entity translations.
35 */
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'],
42 ];
43 $ids = ['id' => ['type' => 'integer']];
44 $definition = [
45 'id' => 'vocabularies',
46 'migration_tags' => ['Import and rollback test'],
47 'source' => [
48 'plugin' => 'embedded_data',
49 'data_rows' => $vocabulary_data_rows,
50 'ids' => $ids,
51 ],
52 'process' => [
53 'vid' => 'id',
54 'name' => 'name',
55 'weight' => 'weight',
56 ],
57 'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
58 ];
59
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();
64
65 $this->assertTrue($vocabulary_migration->getDestinationPlugin()
66 ->supportsRollback());
67
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']);
77 }
78
79 // Second, import translations of the vocabulary name property.
80 $vocabulary_i18n_data_rows = [
81 [
82 'id' => '1',
83 'name' => '1',
84 'language' => 'fr',
85 'property' => 'name',
86 'translation' => 'fr - categories'
87 ],
88 [
89 'id' => '2',
90 'name' => '2',
91 'language' => 'fr',
92 'property' => 'name',
93 'translation' => 'fr - tags'
94 ],
95 ];
96 $ids = [
97 'id' => ['type' => 'integer'],
98 'language' => ['type' => 'string'],
99 ];
100 $definition = [
101 'id' => 'i18n_vocabularies',
102 'migration_tags' => ['Import and rollback test'],
103 'source' => [
104 'plugin' => 'embedded_data',
105 'data_rows' => $vocabulary_i18n_data_rows,
106 'ids' => $ids,
107 'constants' => [
108 'name' => 'name',
109 ]
110 ],
111 'process' => [
112 'vid' => 'id',
113 'langcode' => 'language',
114 'property' => 'constants/name',
115 'translation' => 'translation',
116 ],
117 'destination' => [
118 'plugin' => 'entity:taxonomy_vocabulary',
119 'translations' => 'true',
120 ],
121 ];
122
123 $vocabulary_i18n__migration = \Drupal::service('plugin.manager.migration')
124 ->createStubMigration($definition);
125 $vocabulary_i18n_id_map = $vocabulary_i18n__migration->getIdMap();
126
127 $this->assertTrue($vocabulary_i18n__migration->getDestinationPlugin()
128 ->supportsRollback());
129
130 // Import and validate vocabulary config entities were created.
131 $vocabulary_i18n_executable = new MigrateExecutable($vocabulary_i18n__migration, $this);
132 $vocabulary_i18n_executable->import();
133
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']);
143 }
144
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);
156 }
157
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']);
165 }
166
167 }
168
169 }