Upgraded drupal core with security updates
[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         'language' => 'zu',
40       ],
41       '2' => [
42         'source_vid' => 2,
43         'vid' => 'vocabulary_2_i_1_',
44         'weight' => 3,
45         'parent' => [0],
46         'language' => 'fr',
47       ],
48       '3' => [
49         'source_vid' => 2,
50         'vid' => 'vocabulary_2_i_1_',
51         'weight' => 4,
52         'parent' => [2],
53         'language' => 'fr',
54       ],
55       '4' => [
56         'source_vid' => 3,
57         'vid' => 'vocabulary_3_i_2_',
58         'weight' => 6,
59         'parent' => [0],
60       ],
61       '5' => [
62         'source_vid' => 3,
63         'vid' => 'vocabulary_3_i_2_',
64         'weight' => 7,
65         'parent' => [4],
66       ],
67       '6' => [
68         'source_vid' => 3,
69         'vid' => 'vocabulary_3_i_2_',
70         'weight' => 8,
71         'parent' => [4, 5],
72       ],
73     ];
74     $terms = Term::loadMultiple(array_keys($expected_results));
75
76     // Find each term in the tree.
77     $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
78     $vids = array_unique(array_column($expected_results, 'vid'));
79     $tree_terms = [];
80     foreach ($vids as $vid) {
81       foreach ($storage->loadTree($vid) as $term) {
82         $tree_terms[$term->tid] = $term;
83       }
84     }
85
86     foreach ($expected_results as $tid => $values) {
87       /** @var Term $term */
88       $term = $terms[$tid];
89       $language = isset($values['language']) ? $values['language'] . ' - ' : '';
90       $this->assertSame("{$language}term {$tid} of vocabulary {$values['source_vid']}", $term->name->value);
91       $this->assertSame("{$language}description of term {$tid} of vocabulary {$values['source_vid']}", $term->description->value);
92       $this->assertIdentical($values['vid'], $term->vid->target_id);
93       $this->assertIdentical((string) $values['weight'], $term->weight->value);
94       if ($values['parent'] === [0]) {
95         $this->assertNull($term->parent->target_id);
96       }
97       else {
98         $parents = [];
99         foreach (\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
100           $parents[] = (int) $parent->id();
101         }
102         $this->assertIdentical($parents, $values['parent']);
103       }
104
105       $this->assertArrayHasKey($tid, $tree_terms, "Term $tid exists in vocabulary tree");
106       $tree_term = $tree_terms[$tid];
107       $this->assertEquals($values['parent'], $tree_term->parents, "Term $tid has correct parents in vocabulary tree");
108     }
109   }
110
111 }