3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d7;
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 use Drupal\taxonomy\TermInterface;
10 * Upgrade taxonomy terms.
14 class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
16 public static $modules = [
18 'content_translation',
25 // Required for translation migrations.
26 'migrate_drupal_multilingual',
34 * The cached taxonomy tree items, keyed by vid and tid.
38 protected $treeData = [];
43 protected function setUp() {
45 $this->installEntitySchema('comment');
46 $this->installEntitySchema('node');
47 $this->installEntitySchema('taxonomy_term');
48 $this->installConfig(static::$modules);
50 $this->executeMigrations([
57 'd7_taxonomy_vocabulary',
60 'd7_entity_translation_settings',
61 'd7_taxonomy_term_entity_translation',
66 * Validate a migrated term contains the expected values.
69 * Entity ID to load and check.
70 * @param $expected_label
71 * The label the migrated entity should have.
72 * @param $expected_vid
73 * The parent vocabulary the migrated entity should have.
74 * @param string $expected_description
75 * The description the migrated entity should have.
76 * @param string $expected_format
77 * The format the migrated entity should have.
78 * @param int $expected_weight
79 * The weight the migrated entity should have.
80 * @param array $expected_parents
81 * The parent terms the migrated entity should have.
82 * @param int $expected_field_integer_value
83 * The value the migrated entity field should have.
84 * @param int $expected_term_reference_tid
85 * The term reference id the migrated entity field should have.
86 * @param bool $expected_container_flag
87 * The term should be a container entity.
89 protected function assertEntity($id, $expected_label, $expected_vid, $expected_description = '', $expected_format = NULL, $expected_weight = 0, $expected_parents = [], $expected_field_integer_value = NULL, $expected_term_reference_tid = NULL, $expected_container_flag = 0) {
90 /** @var \Drupal\taxonomy\TermInterface $entity */
91 $entity = Term::load($id);
92 $this->assertInstanceOf(TermInterface::class, $entity);
93 $this->assertEquals($expected_label, $entity->label());
94 $this->assertEquals($expected_vid, $entity->bundle());
95 $this->assertEquals($expected_description, $entity->getDescription());
96 $this->assertEquals($expected_format, $entity->getFormat());
97 $this->assertEquals($expected_weight, $entity->getWeight());
98 $this->assertEquals($expected_parents, $this->getParentIDs($id));
99 $this->assertHierarchy($expected_vid, $id, $expected_parents);
100 if (!is_null($expected_field_integer_value)) {
101 $this->assertTrue($entity->hasField('field_integer'));
102 $this->assertEquals($expected_field_integer_value, $entity->field_integer->value);
104 if (!is_null($expected_term_reference_tid)) {
105 $this->assertTrue($entity->hasField('field_integer'));
106 $this->assertEquals($expected_term_reference_tid, $entity->field_term_reference->target_id);
108 if ($entity->hasField('forum_container')) {
109 $this->assertEquals($expected_container_flag, $entity->forum_container->value);
114 * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
116 public function testTaxonomyTerms() {
117 $this->assertEntity(1, 'General discussion', 'forums', '', NULL, 2);
119 // Tests that terms that used the Drupal 7 Title module and that have their
120 // name and description replaced by real fields are correctly migrated.
121 $this->assertEntity(2, 'Term1 (This is a real field!)', 'test_vocabulary', 'The first term. (This is a real field!)', 'filtered_html', 0, [], NULL, 3);
123 $this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
124 $this->assertEntity(4, 'Term3 in plain old English', 'test_vocabulary', 'The third term in plain old English.', 'full_html', 0, [3], 6);
125 $this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
126 $this->assertEntity(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1);
127 $this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
128 $this->assertEntity(8, 'Half Life 3', 'forums', '', NULL, 0, [6]);
130 // Verify that we still can create forum containers after the migration.
131 $term = Term::create(['vid' => 'forums', 'name' => 'Forum Container', 'forum_container' => 1]);
134 // Reset the forums tree data so this new term is included in the tree.
135 unset($this->treeData['forums']);
136 $this->assertEntity(19, 'Forum Container', 'forums', '', NULL, 0, [], NULL, NULL, 1);
140 * Retrieves the parent term IDs for a given term.
143 * ID of the term to check.
146 * List of parent term IDs.
148 protected function getParentIDs($tid) {
149 return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
153 * Assert that a term is present in the tree storage, with the right parents.
158 * ID of the term to check.
159 * @param array $parent_ids
160 * The expected parent term IDs.
162 protected function assertHierarchy($vid, $tid, array $parent_ids) {
163 if (!isset($this->treeData[$vid])) {
164 $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
165 $this->treeData[$vid] = [];
166 foreach ($tree as $item) {
167 $this->treeData[$vid][$item->tid] = $item;
171 $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
172 $term = $this->treeData[$vid][$tid];
173 $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
177 * Tests the migration of taxonomy term entity translations.
179 public function testTaxonomyTermEntityTranslations() {
180 $manager = $this->container->get('content_translation.manager');
182 // Get the term and its translations.
183 $term = Term::load(4);
184 $term_fr = $term->getTranslation('fr');
185 $term_is = $term->getTranslation('is');
187 // Test that fields translated with Entity Translation are migrated.
188 $this->assertSame('Term3 in plain old English', $term->getName());
189 $this->assertSame('Term3 en français s\'il vous plaît', $term_fr->getName());
190 $this->assertSame('Term3 á íslensku', $term_is->getName());
191 $this->assertSame('The third term in plain old English.', $term->getDescription());
192 $this->assertSame('The third term en français s\'il vous plaît.', $term_fr->getDescription());
193 $this->assertSame('The third term á íslensku.', $term_is->getDescription());
194 $this->assertSame('full_html', $term->getFormat());
195 $this->assertSame('filtered_html', $term_fr->getFormat());
196 $this->assertSame('plain_text', $term_is->getFormat());
197 $this->assertSame('6', $term->field_integer->value);
198 $this->assertSame('5', $term_fr->field_integer->value);
199 $this->assertSame('4', $term_is->field_integer->value);
201 // Test that the French translation metadata is correctly migrated.
202 $metadata_fr = $manager->getTranslationMetadata($term_fr);
203 $this->assertTrue($metadata_fr->isPublished());
204 $this->assertSame('en', $metadata_fr->getSource());
205 $this->assertSame('2', $metadata_fr->getAuthor()->uid->value);
206 $this->assertSame('1531922267', $metadata_fr->getCreatedTime());
207 $this->assertSame('1531922268', $metadata_fr->getChangedTime());
208 $this->assertTrue($metadata_fr->isOutdated());
210 // Test that the Icelandic translation metadata is correctly migrated.
211 $metadata_is = $manager->getTranslationMetadata($term_is);
212 $this->assertFalse($metadata_is->isPublished());
213 $this->assertSame('en', $metadata_is->getSource());
214 $this->assertSame('1', $metadata_is->getAuthor()->uid->value);
215 $this->assertSame('1531922278', $metadata_is->getCreatedTime());
216 $this->assertSame('1531922279', $metadata_is->getChangedTime());
217 $this->assertFalse($metadata_is->isOutdated());
219 // Test that untranslatable properties are the same as the source language.
220 $this->assertSame($term->bundle(), $term_fr->bundle());
221 $this->assertSame($term->bundle(), $term_is->bundle());
222 $this->assertSame($term->getWeight(), $term_fr->getWeight());
223 $this->assertSame($term->getWeight(), $term_is->getWeight());
224 $this->assertSame($term->parent->terget_id, $term_fr->parent->terget_id);
225 $this->assertSame($term->parent->terget_id, $term_is->parent->terget_id);