View source
<?php
namespace Drupal\Tests\content_translation\Kernel\Migrate\d6;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
use Drupal\taxonomy\TermInterface;
class MigrateTaxonomyTermTranslationTest extends MigrateDrupal6TestBase {
protected static $modules = [
'content_translation',
'language',
'menu_ui',
'node',
'taxonomy',
];
protected $treeData = [];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
$this
->installConfig(static::$modules);
$this
->executeMigrations([
'language',
'd6_node_type',
'd6_field',
'd6_taxonomy_vocabulary',
'd6_field_instance',
'd6_taxonomy_term',
'd6_taxonomy_term_translation',
]);
}
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) {
$entity = Term::load($id);
$this
->assertInstanceOf(TermInterface::class, $entity);
$this
->assertSame($expected_language, $entity
->language()
->getId());
$this
->assertSame($expected_label, $entity
->label());
$this
->assertSame($expected_vid, $entity
->bundle());
$this
->assertSame($expected_description, $entity
->getDescription());
$this
->assertSame($expected_format, $entity
->getFormat());
$this
->assertSame($expected_weight, $entity
->getWeight());
$this
->assertHierarchy($expected_vid, $id, $expected_parents);
}
protected function assertHierarchy($vid, $tid, array $parent_ids) {
if (!isset($this->treeData[$vid])) {
$tree = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($vid);
$this->treeData[$vid] = [];
foreach ($tree as $item) {
$this->treeData[$vid][$item->tid] = $item;
}
}
$this
->assertArrayHasKey($tid, $this->treeData[$vid], "Term {$tid} exists in taxonomy tree");
$term = $this->treeData[$vid][$tid];
sort($parent_ids);
$actual_terms = array_filter($term->parents);
sort($actual_terms);
$this
->assertEquals($parent_ids, $actual_terms, "Term {$tid} has correct parents in taxonomy tree");
}
public function testTranslatedTaxonomyTerms() {
$this
->assertEntity(1, 'zu', 'zu - term 1 of vocabulary 1', 'vocabulary_1_i_0_', 'zu - description of term 1 of vocabulary 1', NULL, '0', []);
$this
->assertEntity(2, 'fr', 'fr - term 2 of vocabulary 2', 'vocabulary_2_i_1_', 'fr - description of term 2 of vocabulary 2', NULL, '3', []);
$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',
]);
$this
->assertEntity(4, 'en', 'term 4 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 4 of vocabulary 3', NULL, '6', []);
$this
->assertEntity(5, 'en', 'term 5 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 5 of vocabulary 3', NULL, '7', [
'4',
]);
$this
->assertEntity(6, 'en', 'term 6 of vocabulary 3', 'vocabulary_3_i_2_', 'description of term 6 of vocabulary 3', NULL, '8', [
'4',
'5',
]);
$this
->assertEntity(7, 'fr', 'fr - term 2 of vocabulary 1', 'vocabulary_1_i_0_', 'fr - desc of term 2 vocab 1', NULL, '0', []);
}
}