125283355ee2e0362d24b9618384521b9eed4a4c
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Migrate / d6 / MigrateTaxonomyTermTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Migrate\d6;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
7
8 /**
9  * Upgrade taxonomy terms.
10  *
11  * @group migrate_drupal_6
12  */
13 class MigrateTaxonomyTermTest extends MigrateDrupal6TestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['taxonomy'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25     $this->installEntitySchema('taxonomy_term');
26     $this->executeMigrations(['d6_taxonomy_vocabulary', 'd6_taxonomy_term']);
27   }
28
29   /**
30    * Tests the Drupal 6 taxonomy term to Drupal 8 migration.
31    */
32   public function testTaxonomyTerms() {
33     $expected_results = [
34       '1' => [
35         'source_vid' => 1,
36         'vid' => 'vocabulary_1_i_0_',
37         'weight' => 0,
38         'parent' => [0],
39       ],
40       '2' => [
41         'source_vid' => 2,
42         'vid' => 'vocabulary_2_i_1_',
43         'weight' => 3,
44         'parent' => [0],
45       ],
46       '3' => [
47         'source_vid' => 2,
48         'vid' => 'vocabulary_2_i_1_',
49         'weight' => 4,
50         'parent' => [2],
51       ],
52       '4' => [
53         'source_vid' => 3,
54         'vid' => 'vocabulary_3_i_2_',
55         'weight' => 6,
56         'parent' => [0],
57       ],
58       '5' => [
59         'source_vid' => 3,
60         'vid' => 'vocabulary_3_i_2_',
61         'weight' => 7,
62         'parent' => [4],
63       ],
64       '6' => [
65         'source_vid' => 3,
66         'vid' => 'vocabulary_3_i_2_',
67         'weight' => 8,
68         'parent' => [4, 5],
69       ],
70     ];
71     $terms = Term::loadMultiple(array_keys($expected_results));
72
73     // Find each term in the tree.
74     $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
75     $vids = array_unique(array_column($expected_results, 'vid'));
76     $tree_terms = [];
77     foreach ($vids as $vid) {
78       foreach ($storage->loadTree($vid) as $term) {
79         $tree_terms[$term->tid] = $term;
80       }
81     }
82
83     foreach ($expected_results as $tid => $values) {
84       /** @var Term $term */
85       $term = $terms[$tid];
86       $this->assertIdentical("term {$tid} of vocabulary {$values['source_vid']}", $term->name->value);
87       $this->assertIdentical("description of term {$tid} of vocabulary {$values['source_vid']}", $term->description->value);
88       $this->assertIdentical($values['vid'], $term->vid->target_id);
89       $this->assertIdentical((string) $values['weight'], $term->weight->value);
90       if ($values['parent'] === [0]) {
91         $this->assertNull($term->parent->target_id);
92       }
93       else {
94         $parents = [];
95         foreach (\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
96           $parents[] = (int) $parent->id();
97         }
98         $this->assertIdentical($parents, $values['parent']);
99       }
100
101       $this->assertArrayHasKey($tid, $tree_terms, "Term $tid exists in vocabulary tree");
102       $tree_term = $tree_terms[$tid];
103       $this->assertEquals($values['parent'], $tree_term->parents, "Term $tid has correct parents in vocabulary tree");
104     }
105   }
106
107 }