57d7ccc5cfcbf16bb4b06e933e7fe6f15aad30f5
[yaffs-website] / web / core / modules / content_translation / tests / src / Kernel / Migrate / d6 / MigrateTaxonomyTermTranslationTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Kernel\Migrate\d6;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
7 use Drupal\taxonomy\TermInterface;
8
9 /**
10  * Test migration of translated taxonomy terms.
11  *
12  * @group migrate_drupal_6
13  */
14 class MigrateTaxonomyTermTranslationTest extends MigrateDrupal6TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'content_translation',
21     'language',
22     'menu_ui',
23     'node',
24     'taxonomy',
25   ];
26
27   /**
28    * The cached taxonomy tree items, keyed by vid and tid.
29    *
30    * @var array
31    */
32   protected $treeData = [];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39     $this->installEntitySchema('taxonomy_term');
40     $this->installConfig(static::$modules);
41     $this->executeMigrations([
42       'd6_node_type',
43       'd6_field',
44       'd6_taxonomy_vocabulary',
45       'd6_field_instance',
46       'd6_taxonomy_term',
47       'd6_taxonomy_term_translation',
48     ]);
49   }
50
51   /**
52    * Validate a migrated term contains the expected values.
53    *
54    * @param int $id
55    *   Entity ID to load and check.
56    * @param string $expected_language
57    *   The language code for this term.
58    * @param string $expected_label
59    *   The label the migrated entity should have.
60    * @param string $expected_vid
61    *   The parent vocabulary the migrated entity should have.
62    * @param string $expected_description
63    *   The description the migrated entity should have.
64    * @param string $expected_format
65    *   The format the migrated entity should have.
66    * @param int $expected_weight
67    *   The weight the migrated entity should have.
68    * @param array $expected_parents
69    *   The parent terms the migrated entity should have.
70    * @param int $expected_field_integer_value
71    *   The value the migrated entity field should have.
72    * @param int $expected_term_reference_tid
73    *   The term reference ID the migrated entity field should have.
74    */
75   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) {
76     /** @var \Drupal\taxonomy\TermInterface $entity */
77     $entity = Term::load($id);
78     $this->assertInstanceOf(TermInterface::class, $entity);
79     $this->assertSame($expected_language, $entity->language()->getId());
80     $this->assertSame($expected_label, $entity->label());
81     $this->assertSame($expected_vid, $entity->getVocabularyId());
82     $this->assertSame($expected_description, $entity->getDescription());
83     $this->assertSame($expected_format, $entity->getFormat());
84     $this->assertSame($expected_weight, $entity->getWeight());
85     $this->assertHierarchy($expected_vid, $id, $expected_parents);
86   }
87
88   /**
89    * Assert that a term is present in the tree storage, with the right parents.
90    *
91    * @param string $vid
92    *   Vocabulary ID.
93    * @param int $tid
94    *   ID of the term to check.
95    * @param array $parent_ids
96    *   The expected parent term IDs.
97    */
98   protected function assertHierarchy($vid, $tid, array $parent_ids) {
99     if (!isset($this->treeData[$vid])) {
100       $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
101       $this->treeData[$vid] = [];
102       foreach ($tree as $item) {
103         $this->treeData[$vid][$item->tid] = $item;
104       }
105     }
106
107     $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
108     $term = $this->treeData[$vid][$tid];
109     $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
110   }
111
112   /**
113    * Tests the Drupal 6 i18n taxonomy term to Drupal 8 migration.
114    */
115   public function testTranslatedTaxonomyTerms() {
116     $this->assertEntity(1, 'zu', 'zu - term 1 of vocabulary 1', 'vocabulary_1_i_0_', 'zu - description of term 1 of vocabulary 1', NULL, '0', []);
117     $this->assertEntity(2, 'fr', 'fr - term 2 of vocabulary 2', 'vocabulary_2_i_1_', 'fr - description of term 2 of vocabulary 2', NULL, '3', []);
118     $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']);
119     $this->assertEntity(4, 'en', 'term 4 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 4 of vocabulary 3', NULL, '6', []);
120     $this->assertEntity(5, 'en', 'term 5 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 5 of vocabulary 3', NULL, '7', ['4']);
121     $this->assertEntity(6, 'en', 'term 6 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 6 of vocabulary 3', NULL, '8', ['4', '5']);
122     $this->assertEntity(7, 'fr', 'fr - term 2 of vocabulary 1', 'vocabulary_1_i_0_', 'fr - desc of term 2 vocab 1', NULL, '0', []);
123   }
124
125 }