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 = [
30 * The cached taxonomy tree items, keyed by vid and tid.
34 protected $treeData = [];
39 protected function setUp() {
41 $this->installEntitySchema('taxonomy_term');
42 $this->installConfig(static::$modules);
44 $this->executeMigrations([
48 'd7_taxonomy_vocabulary',
55 * Validate a migrated term contains the expected values.
58 * Entity ID to load and check.
59 * @param $expected_label
60 * The label the migrated entity should have.
61 * @param $expected_vid
62 * The parent vocabulary the migrated entity should have.
63 * @param string $expected_description
64 * The description the migrated entity should have.
65 * @param string $expected_format
66 * The format the migrated entity should have.
67 * @param int $expected_weight
68 * The weight the migrated entity should have.
69 * @param array $expected_parents
70 * The parent terms the migrated entity should have.
71 * @param int $expected_field_integer_value
72 * The value the migrated entity field should have.
73 * @param int $expected_term_reference_tid
74 * The term reference id the migrated entity field should have.
75 * @param bool $expected_container_flag
76 * The term should be a container entity.
78 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, $expected_container_flag = 0) {
79 /** @var \Drupal\taxonomy\TermInterface $entity */
80 $entity = Term::load($id);
81 $this->assertInstanceOf(TermInterface::class, $entity);
82 $this->assertEquals($expected_label, $entity->label());
83 $this->assertEquals($expected_vid, $entity->bundle());
84 $this->assertEquals($expected_description, $entity->getDescription());
85 $this->assertEquals($expected_format, $entity->getFormat());
86 $this->assertEquals($expected_weight, $entity->getWeight());
87 $this->assertEquals($expected_parents, $this->getParentIDs($id));
88 $this->assertHierarchy($expected_vid, $id, $expected_parents);
89 if (!is_null($expected_field_integer_value)) {
90 $this->assertTrue($entity->hasField('field_integer'));
91 $this->assertEquals($expected_field_integer_value, $entity->field_integer->value);
93 if (!is_null($expected_term_reference_tid)) {
94 $this->assertTrue($entity->hasField('field_integer'));
95 $this->assertEquals($expected_term_reference_tid, $entity->field_term_reference->target_id);
97 if ($entity->hasField('forum_container')) {
98 $this->assertEquals($expected_container_flag, $entity->forum_container->value);
103 * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
105 public function testTaxonomyTerms() {
106 $this->assertEntity(1, 'General discussion', 'forums', '', NULL, 2);
107 $this->assertEntity(2, 'Term1', 'test_vocabulary', 'The first term.', 'filtered_html', 0, [], NULL, 3);
108 $this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
109 $this->assertEntity(4, 'Term3', 'test_vocabulary', 'The third term.', 'full_html', 0, [3], 6);
110 $this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
111 $this->assertEntity(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1);
112 $this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
113 $this->assertEntity(8, 'Half Life 3', 'forums', '', NULL, 0, [6]);
115 // Verify that we still can create forum containers after the migration.
116 $term = Term::create(['vid' => 'forums', 'name' => 'Forum Container', 'forum_container' => 1]);
119 // Reset the forums tree data so this new term is included in the tree.
120 unset($this->treeData['forums']);
121 $this->assertEntity(19, 'Forum Container', 'forums', '', NULL, 0, [], NULL, NULL, 1);
125 * Retrieves the parent term IDs for a given term.
128 * ID of the term to check.
131 * List of parent term IDs.
133 protected function getParentIDs($tid) {
134 return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
138 * Assert that a term is present in the tree storage, with the right parents.
143 * ID of the term to check.
144 * @param array $parent_ids
145 * The expected parent term IDs.
147 protected function assertHierarchy($vid, $tid, array $parent_ids) {
148 if (!isset($this->treeData[$vid])) {
149 $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
150 $this->treeData[$vid] = [];
151 foreach ($tree as $item) {
152 $this->treeData[$vid][$item->tid] = $item;
156 $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
157 $term = $this->treeData[$vid][$tid];
158 $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");