474969dde1f38e6b05069aab7344e502b4ca4ea8
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Migrate / d7 / MigrateTaxonomyTermTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d7;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 use Drupal\taxonomy\TermInterface;
8
9 /**
10  * Upgrade taxonomy terms.
11  *
12  * @group taxonomy
13  */
14 class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
15
16   public static $modules = [
17     'comment',
18     'datetime',
19     'forum',
20     'image',
21     'link',
22     'menu_ui',
23     'node',
24     'taxonomy',
25     'telephone',
26     'text',
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
44     $this->executeMigrations([
45       'd7_node_type',
46       'd7_comment_type',
47       'd7_field',
48       'd7_taxonomy_vocabulary',
49       'd7_field_instance',
50       'd7_taxonomy_term'
51     ]);
52   }
53
54   /**
55    * Validate a migrated term contains the expected values.
56    *
57    * @param $id
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.
77    */
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);
92     }
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);
96     }
97     if ($entity->hasField('forum_container')) {
98       $this->assertEquals($expected_container_flag, $entity->forum_container->value);
99     }
100   }
101
102   /**
103    * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
104    */
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]);
114
115     // Verify that we still can create forum containers after the migration.
116     $term = Term::create(['vid' => 'forums', 'name' => 'Forum Container', 'forum_container' => 1]);
117     $term->save();
118
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);
122   }
123
124   /**
125    * Retrieves the parent term IDs for a given term.
126    *
127    * @param $tid
128    *   ID of the term to check.
129    *
130    * @return array
131    *   List of parent term IDs.
132    */
133   protected function getParentIDs($tid) {
134     return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
135   }
136
137   /**
138    * Assert that a term is present in the tree storage, with the right parents.
139    *
140    * @param string $vid
141    *   Vocabular ID.
142    * @param int $tid
143    *   ID of the term to check.
144    * @param array $parent_ids
145    *   The expected parent term IDs.
146    */
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;
153       }
154     }
155
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");
159   }
160
161 }