View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\Migrate\d7;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\taxonomy\TermInterface;
class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
protected static $modules = [
'comment',
'content_translation',
'datetime',
'datetime_range',
'image',
'language',
'link',
'menu_ui',
'node',
'taxonomy',
'telephone',
'text',
];
protected $treeData = [];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('comment');
$this
->installEntitySchema('file');
$this
->migrateTaxonomyTerms();
$this
->executeMigrations([
'language',
'd7_user_role',
'd7_user',
'd7_entity_translation_settings',
'd7_taxonomy_term_entity_translation',
]);
}
protected function assertEntity(int $id, string $expected_language, string $expected_label, string $expected_vid, ?string $expected_description = '', ?string $expected_format = NULL, int $expected_weight = 0, array $expected_parents = [], int $expected_field_integer_value = NULL, int $expected_term_reference_tid = NULL, bool $expected_container_flag = FALSE) : void {
$entity = Term::load($id);
$this
->assertInstanceOf(TermInterface::class, $entity);
$this
->assertSame($expected_language, $entity
->language()
->getId());
$this
->assertEquals($expected_label, $entity
->label());
$this
->assertEquals($expected_vid, $entity
->bundle());
$this
->assertEquals($expected_description, $entity
->getDescription());
$this
->assertEquals($expected_format, $entity
->getFormat());
$this
->assertEquals($expected_weight, (int) $entity
->getWeight());
$this
->assertEquals($expected_parents, $this
->getParentIDs($id));
$this
->assertHierarchy($expected_vid, $id, $expected_parents);
if (!is_null($expected_field_integer_value)) {
$this
->assertTrue($entity
->hasField('field_integer'));
$this
->assertEquals($expected_field_integer_value, $entity->field_integer->value);
}
if (!is_null($expected_term_reference_tid)) {
$this
->assertTrue($entity
->hasField('field_integer'));
$this
->assertEquals($expected_term_reference_tid, $entity->field_term_reference->target_id);
}
if ($entity
->hasField('forum_container')) {
$this
->assertEquals($expected_container_flag, $entity->forum_container->value);
}
}
public function testTaxonomyTerms() {
$this
->assertEntity(1, 'en', 'General discussion', 'forums', '', NULL, 2);
$this
->assertEntity(2, 'en', 'Term1 (This is a real field!)', 'test_vocabulary', 'The first term. (This is a real field!)', 'filtered_html', 0, [], NULL, 3);
$this
->assertEntity(3, 'en', 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
$this
->assertEntity(4, 'en', 'Term3 in plain old English', 'test_vocabulary', 'The third term in plain old English.', 'full_html', 0, [
3,
], 6);
$this
->assertEntity(5, 'en', 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
$this
->assertEntity(6, 'en', 'Games', 'forums', NULL, '', 4, []);
$this
->assertEntity(7, 'en', 'Minecraft', 'forums', '', NULL, 1, [
6,
]);
$this
->assertEntity(8, 'en', 'Half Life 3', 'forums', '', NULL, 0, [
6,
]);
$term = Term::create([
'vid' => 'forums',
'name' => 'Forum Container',
'forum_container' => 1,
]);
$term
->save();
unset($this->treeData['forums']);
$this
->assertEntity(26, 'en', 'Forum Container', 'forums', '', '', 0, [], NULL, NULL, 1);
$this
->assertEntity(19, 'en', 'Jupiter Station', 'vocablocalized', 'Holographic research.', 'filtered_html', 0, [], NULL, NULL, 1);
$this
->assertEntity(20, 'en', 'DS9', 'vocablocalized', 'Terok Nor', 'filtered_html', 0, [], NULL, NULL, 1);
$this
->assertEntity(21, 'en', 'High council', 'vocabtranslate', NULL, NULL, 0, [], NULL, NULL, 1);
$this
->assertEntity(22, 'fr', 'fr - High council', 'vocabtranslate', NULL, NULL, 0, [], NULL, NULL, 1);
$this
->assertEntity(23, 'is', 'is - High council', 'vocabtranslate', NULL, NULL, 0, [], NULL, NULL, 1);
$this
->assertEntity(24, 'fr', 'FR - Crewman', 'vocabfixed', NULL, NULL, 0, [], NULL, NULL, 1);
$this
->assertEntity(19, 'en', 'Jupiter Station', 'vocablocalized', 'Holographic research.', 'filtered_html', '0', []);
$this
->assertEntity(20, 'en', 'DS9', 'vocablocalized', 'Terok Nor', 'filtered_html', '0', []);
$this
->assertEntity(25, 'en', 'Emissary', 'vocablocalized2', 'Pilot episode', 'filtered_html', '0', []);
$entity = Term::load(20);
$this
->assertSame('Bajor', $entity->field_sector->value);
$this
->assertEntity(21, 'en', 'High council', 'vocabtranslate', NULL, NULL, '0', []);
$entity = Term::load(21);
$this
->assertSame("K'mpec", $entity->field_chancellor->value);
$this
->assertEntity(22, 'fr', 'fr - High council', 'vocabtranslate', NULL, NULL, '0', []);
$this
->assertEntity(23, 'is', 'is - High council', 'vocabtranslate', NULL, NULL, '0', []);
$this
->assertEntity(24, 'fr', 'FR - Crewman', 'vocabfixed', NULL, NULL, '0', []);
$manager = $this->container
->get('content_translation.manager');
$term = Term::load(4);
$term_fr = $term
->getTranslation('fr');
$term_is = $term
->getTranslation('is');
$this
->assertSame('Term3 in plain old English', $term
->getName());
$this
->assertSame('Term3 en français s\'il vous plaît', $term_fr
->getName());
$this
->assertSame('Term3 á íslensku', $term_is
->getName());
$this
->assertSame('The third term in plain old English.', $term
->getDescription());
$this
->assertSame('The third term en français s\'il vous plaît.', $term_fr
->getDescription());
$this
->assertSame('The third term á íslensku.', $term_is
->getDescription());
$this
->assertSame('full_html', $term
->getFormat());
$this
->assertSame('filtered_html', $term_fr
->getFormat());
$this
->assertSame('plain_text', $term_is
->getFormat());
$this
->assertSame('6', $term->field_integer->value);
$this
->assertSame('5', $term_fr->field_integer->value);
$this
->assertSame('4', $term_is->field_integer->value);
$metadata_fr = $manager
->getTranslationMetadata($term_fr);
$this
->assertTrue($metadata_fr
->isPublished());
$this
->assertSame('en', $metadata_fr
->getSource());
$this
->assertSame('2', $metadata_fr
->getAuthor()->uid->value);
$this
->assertSame('1531922267', $metadata_fr
->getCreatedTime());
$this
->assertSame('1531922268', $metadata_fr
->getChangedTime());
$this
->assertTrue($metadata_fr
->isOutdated());
$metadata_is = $manager
->getTranslationMetadata($term_is);
$this
->assertFalse($metadata_is
->isPublished());
$this
->assertSame('en', $metadata_is
->getSource());
$this
->assertSame('1', $metadata_is
->getAuthor()->uid->value);
$this
->assertSame('1531922278', $metadata_is
->getCreatedTime());
$this
->assertSame('1531922279', $metadata_is
->getChangedTime());
$this
->assertFalse($metadata_is
->isOutdated());
$this
->assertSame($term
->bundle(), $term_fr
->bundle());
$this
->assertSame($term
->bundle(), $term_is
->bundle());
$this
->assertSame($term
->getWeight(), $term_fr
->getWeight());
$this
->assertSame($term
->getWeight(), $term_is
->getWeight());
$this
->assertSame($term->parent->terget_id, $term_fr->parent->terget_id);
$this
->assertSame($term->parent->terget_id, $term_is->parent->terget_id);
}
protected function getParentIDs($tid) {
return array_keys(\Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadParents($tid));
}
protected function assertHierarchy(string $vid, int $tid, array $parent_ids) : void {
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];
$this
->assertEquals($parent_ids, array_filter($term->parents), "Term {$tid} has correct parents in taxonomy tree");
}
}