Pull merge.
[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     // Required for translation migrations.
24     'migrate_drupal_multilingual',
25     'node',
26     'taxonomy',
27   ];
28
29   /**
30    * The cached taxonomy tree items, keyed by vid and tid.
31    *
32    * @var array
33    */
34   protected $treeData = [];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41     $this->installEntitySchema('taxonomy_term');
42     $this->installConfig(static::$modules);
43     $this->executeMigrations([
44       'd6_node_type',
45       'd6_field',
46       'd6_taxonomy_vocabulary',
47       'd6_field_instance',
48       'd6_taxonomy_term',
49       'd6_taxonomy_term_translation',
50     ]);
51   }
52
53   /**
54    * Validate a migrated term contains the expected values.
55    *
56    * @param int $id
57    *   Entity ID to load and check.
58    * @param string $expected_language
59    *   The language code for this term.
60    * @param string $expected_label
61    *   The label the migrated entity should have.
62    * @param string $expected_vid
63    *   The parent vocabulary the migrated entity should have.
64    * @param string $expected_description
65    *   The description the migrated entity should have.
66    * @param string $expected_format
67    *   The format the migrated entity should have.
68    * @param int $expected_weight
69    *   The weight the migrated entity should have.
70    * @param array $expected_parents
71    *   The parent terms the migrated entity should have.
72    * @param int $expected_field_integer_value
73    *   The value the migrated entity field should have.
74    * @param int $expected_term_reference_tid
75    *   The term reference ID the migrated entity field should have.
76    */
77   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) {
78     /** @var \Drupal\taxonomy\TermInterface $entity */
79     $entity = Term::load($id);
80     $this->assertInstanceOf(TermInterface::class, $entity);
81     $this->assertSame($expected_language, $entity->language()->getId());
82     $this->assertSame($expected_label, $entity->label());
83     $this->assertSame($expected_vid, $entity->bundle());
84     $this->assertSame($expected_description, $entity->getDescription());
85     $this->assertSame($expected_format, $entity->getFormat());
86     $this->assertSame($expected_weight, $entity->getWeight());
87     $this->assertHierarchy($expected_vid, $id, $expected_parents);
88   }
89
90   /**
91    * Assert that a term is present in the tree storage, with the right parents.
92    *
93    * @param string $vid
94    *   Vocabulary ID.
95    * @param int $tid
96    *   ID of the term to check.
97    * @param array $parent_ids
98    *   The expected parent term IDs.
99    */
100   protected function assertHierarchy($vid, $tid, array $parent_ids) {
101     if (!isset($this->treeData[$vid])) {
102       $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
103       $this->treeData[$vid] = [];
104       foreach ($tree as $item) {
105         $this->treeData[$vid][$item->tid] = $item;
106       }
107     }
108
109     $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
110     $term = $this->treeData[$vid][$tid];
111     // PostgreSQL, MySQL and SQLite may not return the parent terms in the same
112     // order so sort before testing.
113     sort($parent_ids);
114     $actual_terms = array_filter($term->parents);
115     sort($actual_terms);
116     $this->assertEquals($parent_ids, $actual_terms, "Term $tid has correct parents in taxonomy tree");
117   }
118
119   /**
120    * Tests the Drupal 6 i18n taxonomy term to Drupal 8 migration.
121    */
122   public function testTranslatedTaxonomyTerms() {
123     $this->assertEntity(1, 'zu', 'zu - term 1 of vocabulary 1', 'vocabulary_1_i_0_', 'zu - description of term 1 of vocabulary 1', NULL, '0', []);
124     $this->assertEntity(2, 'fr', 'fr - term 2 of vocabulary 2', 'vocabulary_2_i_1_', 'fr - description of term 2 of vocabulary 2', NULL, '3', []);
125     $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']);
126     $this->assertEntity(4, 'en', 'term 4 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 4 of vocabulary 3', NULL, '6', []);
127     $this->assertEntity(5, 'en', 'term 5 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 5 of vocabulary 3', NULL, '7', ['4']);
128     $this->assertEntity(6, 'en', 'term 6 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 6 of vocabulary 3', NULL, '8', ['4', '5']);
129     $this->assertEntity(7, 'fr', 'fr - term 2 of vocabulary 1', 'vocabulary_1_i_0_', 'fr - desc of term 2 vocab 1', NULL, '0', []);
130   }
131
132 }