3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d6;
5 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\TermInterface;
10 * Tests migration of localized translated taxonomy terms.
12 * @group migrate_drupal_6
14 class MigrateTermLocalizedTranslationTest extends MigrateDrupal6TestBase {
19 public static $modules = [
20 'content_translation',
25 // Required for translation migrations.
26 'migrate_drupal_multilingual',
30 * The cached taxonomy tree items, keyed by vid and tid.
34 protected $treeData = [];
39 protected function setUp() {
41 $this->installEntitySchema('taxonomy_term');
42 $this->installConfig(static::$modules);
43 $this->executeMigrations([
47 'd6_taxonomy_vocabulary',
50 'd6_taxonomy_term_localized_translation',
55 * Validates a migrated term contains the expected values.
58 * Entity ID to load and check.
59 * @param string $expected_language
60 * The language code for this term.
61 * @param string $expected_label
62 * The label the migrated entity should have.
63 * @param string $expected_vid
64 * The parent vocabulary the migrated entity should have.
65 * @param string $expected_description
66 * The description the migrated entity should have.
67 * @param string $expected_format
68 * The format the migrated entity should have.
69 * @param int $expected_weight
70 * The weight the migrated entity should have.
71 * @param array $expected_parents
72 * The parent terms the migrated entity should have.
73 * @param int $expected_field_integer_value
74 * The value the migrated entity field should have.
75 * @param int $expected_term_reference_tid
76 * The term reference ID the migrated entity field should have.
78 protected function assertEntity($id, $expected_language, $expected_label, $expected_vid, $expected_description = '', $expected_format = NULL, $expected_weight = 0, array $expected_parents = [], $expected_field_integer_value = NULL, $expected_term_reference_tid = NULL) {
79 /** @var \Drupal\taxonomy\TermInterface $entity */
80 $entity = Term::load($id);
81 $this->assertInstanceOf(TermInterface::class, $entity);
82 $this->assertSame($expected_language, $entity->language()->getId());
83 $this->assertSame($expected_label, $entity->label());
84 $this->assertSame($expected_vid, $entity->bundle());
85 $this->assertSame($expected_description, $entity->getDescription());
86 $this->assertSame($expected_format, $entity->getFormat());
87 $this->assertSame($expected_weight, $entity->getWeight());
88 $this->assertHierarchy($expected_vid, $id, $expected_parents);
92 * Asserts that a term is present in the tree storage, with the right parents.
97 * ID of the term to check.
98 * @param array $parent_ids
99 * The expected parent term IDs.
101 protected function assertHierarchy($vid, $tid, array $parent_ids) {
102 if (!isset($this->treeData[$vid])) {
103 $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
104 $this->treeData[$vid] = [];
105 foreach ($tree as $item) {
106 $this->treeData[$vid][$item->tid] = $item;
110 $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
111 $term = $this->treeData[$vid][$tid];
112 $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
116 * Tests the Drupal 6 i18n localized taxonomy term to Drupal 8 migration.
118 public function testTranslatedLocalizedTaxonomyTerms() {
119 $this->assertEntity(14, 'en', 'Talos IV', 'vocabulary_name_much_longer_than', 'The home of Captain Christopher Pike.', NULL, '0', []);
120 $this->assertEntity(15, 'en', 'Vulcan', 'vocabulary_name_much_longer_than', NULL, NULL, '0', []);
122 /** @var \Drupal\taxonomy\TermInterface $entity */
123 $entity = Term::load(14);
124 $this->assertTrue($entity->hasTranslation('fr'));
125 $translation = $entity->getTranslation('fr');
126 $this->assertSame('fr - Talos IV', $translation->label());
127 $this->assertSame('fr - The home of Captain Christopher Pike.', $translation->getDescription());
129 $this->assertTrue($entity->hasTranslation('zu'));
130 $translation = $entity->getTranslation('zu');
131 $this->assertSame('Talos IV', $translation->label());
132 $this->assertSame('zu - The home of Captain Christopher Pike.', $translation->getDescription());
134 $entity = Term::load(15);
135 $this->assertFalse($entity->hasTranslation('fr'));
136 $this->assertTrue($entity->hasTranslation('zu'));
137 $translation = $entity->getTranslation('zu');
138 $this->assertSame('zu - Vulcan', $translation->label());
139 $this->assertSame('', $translation->getDescription());