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\migrate\Plugin\MigrateIdMapInterface;
|
Chris@0
|
7 use Drupal\migrate\Row;
|
Chris@0
|
8 use Drupal\taxonomy\Entity\Term;
|
Chris@0
|
9 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests rolling back of imports.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group migrate
|
Chris@0
|
15 */
|
Chris@0
|
16 class MigrateRollbackTest extends MigrateTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['field', 'taxonomy', 'text', 'user'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 protected function setUp() {
|
Chris@0
|
29 parent::setUp();
|
Chris@0
|
30 $this->installEntitySchema('user');
|
Chris@0
|
31 $this->installEntitySchema('taxonomy_vocabulary');
|
Chris@0
|
32 $this->installEntitySchema('taxonomy_term');
|
Chris@0
|
33 $this->installConfig(['taxonomy']);
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Tests rolling back configuration and content entities.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function testRollback() {
|
Chris@0
|
40 // We use vocabularies to demonstrate importing and rolling back
|
Chris@0
|
41 // configuration entities.
|
Chris@0
|
42 $vocabulary_data_rows = [
|
Chris@0
|
43 ['id' => '1', 'name' => 'categories', 'weight' => '2'],
|
Chris@0
|
44 ['id' => '2', 'name' => 'tags', 'weight' => '1'],
|
Chris@0
|
45 ];
|
Chris@0
|
46 $ids = ['id' => ['type' => 'integer']];
|
Chris@0
|
47 $definition = [
|
Chris@0
|
48 'id' => 'vocabularies',
|
Chris@0
|
49 'migration_tags' => ['Import and rollback test'],
|
Chris@0
|
50 'source' => [
|
Chris@0
|
51 'plugin' => 'embedded_data',
|
Chris@0
|
52 'data_rows' => $vocabulary_data_rows,
|
Chris@0
|
53 'ids' => $ids,
|
Chris@0
|
54 ],
|
Chris@0
|
55 'process' => [
|
Chris@0
|
56 'vid' => 'id',
|
Chris@0
|
57 'name' => 'name',
|
Chris@0
|
58 'weight' => 'weight',
|
Chris@0
|
59 ],
|
Chris@0
|
60 'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
|
Chris@0
|
61 ];
|
Chris@0
|
62
|
Chris@0
|
63 $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
64 $vocabulary_id_map = $vocabulary_migration->getIdMap();
|
Chris@0
|
65
|
Chris@0
|
66 $this->assertTrue($vocabulary_migration->getDestinationPlugin()->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@12
|
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 // We use taxonomy terms to demonstrate importing and rolling back content
|
Chris@0
|
80 // entities.
|
Chris@0
|
81 $term_data_rows = [
|
Chris@0
|
82 ['id' => '1', 'vocab' => '1', 'name' => 'music'],
|
Chris@0
|
83 ['id' => '2', 'vocab' => '2', 'name' => 'Bach'],
|
Chris@0
|
84 ['id' => '3', 'vocab' => '2', 'name' => 'Beethoven'],
|
Chris@0
|
85 ];
|
Chris@0
|
86 $ids = ['id' => ['type' => 'integer']];
|
Chris@0
|
87 $definition = [
|
Chris@0
|
88 'id' => 'terms',
|
Chris@0
|
89 'migration_tags' => ['Import and rollback test'],
|
Chris@0
|
90 'source' => [
|
Chris@0
|
91 'plugin' => 'embedded_data',
|
Chris@0
|
92 'data_rows' => $term_data_rows,
|
Chris@0
|
93 'ids' => $ids,
|
Chris@0
|
94 ],
|
Chris@0
|
95 'process' => [
|
Chris@0
|
96 'tid' => 'id',
|
Chris@0
|
97 'vid' => 'vocab',
|
Chris@0
|
98 'name' => 'name',
|
Chris@0
|
99 ],
|
Chris@0
|
100 'destination' => ['plugin' => 'entity:taxonomy_term'],
|
Chris@0
|
101 'migration_dependencies' => ['required' => ['vocabularies']],
|
Chris@0
|
102 ];
|
Chris@0
|
103
|
Chris@0
|
104 $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
105 $term_id_map = $term_migration->getIdMap();
|
Chris@0
|
106
|
Chris@0
|
107 $this->assertTrue($term_migration->getDestinationPlugin()->supportsRollback());
|
Chris@0
|
108
|
Chris@0
|
109 // Pre-create a term, to make sure it isn't deleted on rollback.
|
Chris@0
|
110 $preserved_term_ids[] = 1;
|
Chris@0
|
111 $new_term = Term::create(['tid' => 1, 'vid' => 1, 'name' => 'music']);
|
Chris@0
|
112 $new_term->save();
|
Chris@0
|
113
|
Chris@0
|
114 // Import and validate term entities were created.
|
Chris@0
|
115 $term_executable = new MigrateExecutable($term_migration, $this);
|
Chris@0
|
116 $term_executable->import();
|
Chris@0
|
117 // Also explicitly mark one row to be preserved on rollback.
|
Chris@0
|
118 $preserved_term_ids[] = 2;
|
Chris@0
|
119 $map_row = $term_id_map->getRowBySource(['id' => 2]);
|
Chris@0
|
120 $dummy_row = new Row(['id' => 2], $ids);
|
Chris@0
|
121 $term_id_map->saveIdMapping($dummy_row, [$map_row['destid1']],
|
Chris@0
|
122 $map_row['source_row_status'], MigrateIdMapInterface::ROLLBACK_PRESERVE);
|
Chris@0
|
123
|
Chris@0
|
124 foreach ($term_data_rows as $row) {
|
Chris@12
|
125 /** @var \Drupal\taxonomy\Entity\Term $term */
|
Chris@0
|
126 $term = Term::load($row['id']);
|
Chris@0
|
127 $this->assertTrue($term);
|
Chris@0
|
128 $map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
|
Chris@0
|
129 $this->assertNotNull($map_row['destid1']);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 // Add a failed row to test if this can be rolled back without errors.
|
Chris@0
|
133 $this->mockFailure($term_migration, ['id' => '4', 'vocab' => '2', 'name' => 'FAIL']);
|
Chris@0
|
134
|
Chris@0
|
135 // Rollback and verify the entities are gone.
|
Chris@0
|
136 $term_executable->rollback();
|
Chris@0
|
137 foreach ($term_data_rows as $row) {
|
Chris@0
|
138 $term = Term::load($row['id']);
|
Chris@0
|
139 if (in_array($row['id'], $preserved_term_ids)) {
|
Chris@0
|
140 $this->assertNotNull($term);
|
Chris@0
|
141 }
|
Chris@0
|
142 else {
|
Chris@0
|
143 $this->assertNull($term);
|
Chris@0
|
144 }
|
Chris@0
|
145 $map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
|
Chris@0
|
146 $this->assertFalse($map_row);
|
Chris@0
|
147 }
|
Chris@0
|
148 $vocabulary_executable->rollback();
|
Chris@0
|
149 foreach ($vocabulary_data_rows as $row) {
|
Chris@0
|
150 $term = Vocabulary::load($row['id']);
|
Chris@0
|
151 $this->assertNull($term);
|
Chris@0
|
152 $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
|
Chris@0
|
153 $this->assertFalse($map_row);
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 // Test that simple configuration is not rollbackable.
|
Chris@0
|
157 $term_setting_rows = [
|
Chris@0
|
158 ['id' => 1, 'override_selector' => '0', 'terms_per_page_admin' => '10'],
|
Chris@0
|
159 ];
|
Chris@0
|
160 $ids = ['id' => ['type' => 'integer']];
|
Chris@0
|
161 $definition = [
|
Chris@0
|
162 'id' => 'taxonomy_settings',
|
Chris@0
|
163 'migration_tags' => ['Import and rollback test'],
|
Chris@0
|
164 'source' => [
|
Chris@0
|
165 'plugin' => 'embedded_data',
|
Chris@0
|
166 'data_rows' => $term_setting_rows,
|
Chris@0
|
167 'ids' => $ids,
|
Chris@0
|
168 ],
|
Chris@0
|
169 'process' => [
|
Chris@0
|
170 'override_selector' => 'override_selector',
|
Chris@0
|
171 'terms_per_page_admin' => 'terms_per_page_admin',
|
Chris@0
|
172 ],
|
Chris@0
|
173 'destination' => [
|
Chris@0
|
174 'plugin' => 'config',
|
Chris@0
|
175 'config_name' => 'taxonomy.settings',
|
Chris@0
|
176 ],
|
Chris@0
|
177 'migration_dependencies' => ['required' => ['vocabularies']],
|
Chris@0
|
178 ];
|
Chris@0
|
179
|
Chris@0
|
180 $settings_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
181 $this->assertFalse($settings_migration->getDestinationPlugin()->supportsRollback());
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 }
|