3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d6;
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
7 use Drupal\taxonomy\TermInterface;
10 * Test migration of translated taxonomy terms.
12 * @group migrate_drupal_6
14 class MigrateTaxonomyTermTranslationTest extends MigrateDrupal6TestBase {
19 public static $modules = [
27 * The cached taxonomy tree items, keyed by vid and tid.
31 protected $treeData = [];
36 protected function setUp() {
38 $this->installEntitySchema('taxonomy_term');
39 $this->installConfig(static::$modules);
40 $this->executeMigrations([
43 'd6_taxonomy_vocabulary',
46 'd6_taxonomy_term_translation',
51 * Validate a migrated term contains the expected values.
54 * Entity ID to load and check.
55 * @param string $expected_language
56 * The language code for this term.
57 * @param string $expected_label
58 * The label the migrated entity should have.
59 * @param string $expected_vid
60 * The parent vocabulary the migrated entity should have.
61 * @param string $expected_description
62 * The description the migrated entity should have.
63 * @param string $expected_format
64 * The format the migrated entity should have.
65 * @param int $expected_weight
66 * The weight the migrated entity should have.
67 * @param array $expected_parents
68 * The parent terms the migrated entity should have.
69 * @param int $expected_field_integer_value
70 * The value the migrated entity field should have.
71 * @param int $expected_term_reference_tid
72 * The term reference ID the migrated entity field should have.
74 protected function assertEntity($id, $expected_language, $expected_label, $expected_vid, $expected_description = '', $expected_format = NULL, $expected_weight = 0, $expected_parents = [], $expected_field_integer_value = NULL, $expected_term_reference_tid = NULL) {
75 /** @var \Drupal\taxonomy\TermInterface $entity */
76 $entity = Term::load($id);
77 $this->assertInstanceOf(TermInterface::class, $entity);
78 $this->assertSame($expected_language, $entity->language()->getId());
79 $this->assertSame($expected_label, $entity->label());
80 $this->assertSame($expected_vid, $entity->getVocabularyId());
81 $this->assertSame($expected_description, $entity->getDescription());
82 $this->assertSame($expected_format, $entity->getFormat());
83 $this->assertSame($expected_weight, $entity->getWeight());
84 $this->assertHierarchy($expected_vid, $id, $expected_parents);
88 * Assert that a term is present in the tree storage, with the right parents.
93 * ID of the term to check.
94 * @param array $parent_ids
95 * The expected parent term IDs.
97 protected function assertHierarchy($vid, $tid, array $parent_ids) {
98 if (!isset($this->treeData[$vid])) {
99 $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
100 $this->treeData[$vid] = [];
101 foreach ($tree as $item) {
102 $this->treeData[$vid][$item->tid] = $item;
106 $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
107 $term = $this->treeData[$vid][$tid];
108 $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
112 * Tests the Drupal 6 i18n taxonomy term to Drupal 8 migration.
114 public function testTranslatedTaxonomyTerms() {
115 $this->assertEntity(1, 'zu', 'zu - term 1 of vocabulary 1', 'vocabulary_1_i_0_', 'zu - description of term 1 of vocabulary 1', NULL, '0', []);
116 $this->assertEntity(2, 'fr', 'fr - term 2 of vocabulary 2', 'vocabulary_2_i_1_', 'fr - description of term 2 of vocabulary 2', NULL, '3', []);
117 $this->assertEntity(3, 'fr', 'fr - term 3 of vocabulary 2', 'vocabulary_2_i_1_', 'fr - description of term 3 of vocabulary 2', NULL, '4', ['2']);
118 $this->assertEntity(4, 'en', 'term 4 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 4 of vocabulary 3', NULL, '6', []);
119 $this->assertEntity(5, 'en', 'term 5 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 5 of vocabulary 3', NULL, '7', ['4']);
120 $this->assertEntity(6, 'en', 'term 6 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 6 of vocabulary 3', NULL, '8', ['4', '5']);
121 $this->assertEntity(7, 'fr', 'fr - term 2 of vocabulary 1', 'vocabulary_1_i_0_', 'fr - desc of term 2 vocab 1', NULL, '0', []);