fceaf0bdbc31085cd16c51424365650715c15540
[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     'image',
20     'link',
21     'menu_ui',
22     'node',
23     'taxonomy',
24     'telephone',
25     'text',
26   ];
27
28   /**
29    * The cached taxonomy tree items, keyed by vid and tid.
30    *
31    * @var array
32    */
33   protected $treeData = [];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40     $this->installEntitySchema('taxonomy_term');
41     $this->installConfig(static::$modules);
42
43     $this->executeMigrations([
44       'd7_node_type',
45       'd7_comment_type',
46       'd7_field',
47       'd7_taxonomy_vocabulary',
48       'd7_field_instance',
49       'd7_taxonomy_term'
50     ]);
51   }
52
53   /**
54    * Validate a migrated term contains the expected values.
55    *
56    * @param $id
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.
74    */
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);
89     }
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);
93     }
94   }
95
96   /**
97    * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
98    */
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]);
108   }
109
110   /**
111    * Retrieves the parent term IDs for a given term.
112    *
113    * @param $tid
114    *   ID of the term to check.
115    *
116    * @return array
117    *   List of parent term IDs.
118    */
119   protected function getParentIDs($tid) {
120     return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
121   }
122
123   /**
124    * Assert that a term is present in the tree storage, with the right parents.
125    *
126    * @param string $vid
127    *   Vocabular ID.
128    * @param int $tid
129    *   ID of the term to check.
130    * @param array $parent_ids
131    *   The expected parent term IDs.
132    */
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;
139       }
140     }
141
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");
145   }
146
147 }