3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d7;
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 use Drupal\taxonomy\TermInterface;
10 * Upgrade taxonomy terms.
14 class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
16 public static $modules = [
29 * The cached taxonomy tree items, keyed by vid and tid.
33 protected $treeData = [];
38 protected function setUp() {
40 $this->installEntitySchema('taxonomy_term');
41 $this->installConfig(static::$modules);
43 $this->executeMigrations([
47 'd7_taxonomy_vocabulary',
54 * Validate a migrated term contains the expected values.
57 * Entity ID to load and check.
58 * @param $expected_label
59 * The label the migrated entity should have.
60 * @param $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.
75 protected function assertEntity($id, $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->assertTrue($entity instanceof TermInterface);
79 $this->assertIdentical($expected_label, $entity->label());
80 $this->assertIdentical($expected_vid, $entity->getVocabularyId());
81 $this->assertEqual($expected_description, $entity->getDescription());
82 $this->assertEquals($expected_format, $entity->getFormat());
83 $this->assertEqual($expected_weight, $entity->getWeight());
84 $this->assertIdentical($expected_parents, $this->getParentIDs($id));
85 $this->assertHierarchy($expected_vid, $id, $expected_parents);
86 if (!is_null($expected_field_integer_value)) {
87 $this->assertTrue($entity->hasField('field_integer'));
88 $this->assertEquals($expected_field_integer_value, $entity->field_integer->value);
90 if (!is_null($expected_term_reference_tid)) {
91 $this->assertTrue($entity->hasField('field_integer'));
92 $this->assertEquals($expected_term_reference_tid, $entity->field_term_reference->target_id);
97 * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
99 public function testTaxonomyTerms() {
100 $this->assertEntity(1, 'General discussion', 'forums', '', NULL, 2);
101 $this->assertEntity(2, 'Term1', 'test_vocabulary', 'The first term.', 'filtered_html', 0, [], NULL, 3);
102 $this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
103 $this->assertEntity(4, 'Term3', 'test_vocabulary', 'The third term.', 'full_html', 0, [3], 6);
104 $this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
105 $this->assertEntity(6, 'Games', 'forums', '', NULL, 4);
106 $this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
107 $this->assertEntity(8, 'Half Life 3', 'forums', '', NULL, 0, [6]);
111 * Retrieves the parent term IDs for a given term.
114 * ID of the term to check.
117 * List of parent term IDs.
119 protected function getParentIDs($tid) {
120 return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
124 * Assert that a term is present in the tree storage, with the right parents.
129 * ID of the term to check.
130 * @param array $parent_ids
131 * The expected parent term IDs.
133 protected function assertHierarchy($vid, $tid, array $parent_ids) {
134 if (!isset($this->treeData[$vid])) {
135 $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
136 $this->treeData[$vid] = [];
137 foreach ($tree as $item) {
138 $this->treeData[$vid][$item->tid] = $item;
142 $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
143 $term = $this->treeData[$vid][$tid];
144 $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");