Pull merge.
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Migrate / d6 / MigrateTermLocalizedTranslationTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d6;
4
5 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\TermInterface;
8
9 /**
10  * Tests migration of localized translated taxonomy terms.
11  *
12  * @group migrate_drupal_6
13  */
14 class MigrateTermLocalizedTranslationTest extends MigrateDrupal6TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'content_translation',
21     'language',
22     'menu_ui',
23     'node',
24     'taxonomy',
25     // Required for translation migrations.
26     'migrate_drupal_multilingual',
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       'language',
45       'd6_node_type',
46       'd6_field',
47       'd6_taxonomy_vocabulary',
48       'd6_field_instance',
49       'd6_taxonomy_term',
50       'd6_taxonomy_term_localized_translation',
51     ]);
52   }
53
54   /**
55    * Validates a migrated term contains the expected values.
56    *
57    * @param int $id
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.
77    */
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);
89   }
90
91   /**
92    * Asserts that a term is present in the tree storage, with the right parents.
93    *
94    * @param string $vid
95    *   Vocabulary ID.
96    * @param int $tid
97    *   ID of the term to check.
98    * @param array $parent_ids
99    *   The expected parent term IDs.
100    */
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;
107       }
108     }
109
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");
113   }
114
115   /**
116    * Tests the Drupal 6 i18n localized taxonomy term to Drupal 8 migration.
117    */
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', []);
121
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());
128
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());
133
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());
140   }
141
142 }