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