Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\taxonomy\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\taxonomy\Entity\Term;
|
Chris@0
|
6 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
7 use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Kernel tests for taxonomy term functions.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group taxonomy
|
Chris@0
|
13 */
|
Chris@0
|
14 class TermKernelTest extends KernelTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 use TaxonomyTestTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * {@inheritdoc}
|
Chris@0
|
20 */
|
Chris@0
|
21 public static $modules = ['filter', 'taxonomy', 'text', 'user'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 protected function setUp() {
|
Chris@0
|
27 parent::setUp();
|
Chris@0
|
28 $this->installConfig(['filter']);
|
Chris@0
|
29 $this->installEntitySchema('taxonomy_term');
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Deleting terms should also remove related vocabulary.
|
Chris@0
|
34 * Deleting an invalid term should silently fail.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function testTermDelete() {
|
Chris@0
|
37 $vocabulary = $this->createVocabulary();
|
Chris@0
|
38 $valid_term = $this->createTerm($vocabulary);
|
Chris@0
|
39 // Delete a valid term.
|
Chris@0
|
40 $valid_term->delete();
|
Chris@0
|
41 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
|
Chris@0
|
42 $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');
|
Chris@0
|
43
|
Chris@0
|
44 // Delete an invalid term. Should not throw any notices.
|
Chris@0
|
45 entity_delete_multiple('taxonomy_term', [42]);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Deleting a parent of a term with multiple parents does not delete the term.
|
Chris@0
|
50 */
|
Chris@0
|
51 public function testMultipleParentDelete() {
|
Chris@0
|
52 $vocabulary = $this->createVocabulary();
|
Chris@0
|
53 $parent_term1 = $this->createTerm($vocabulary);
|
Chris@0
|
54 $parent_term2 = $this->createTerm($vocabulary);
|
Chris@0
|
55 $child_term = $this->createTerm($vocabulary);
|
Chris@0
|
56 $child_term->parent = [$parent_term1->id(), $parent_term2->id()];
|
Chris@0
|
57 $child_term->save();
|
Chris@0
|
58 $child_term_id = $child_term->id();
|
Chris@0
|
59
|
Chris@0
|
60 $parent_term1->delete();
|
Chris@0
|
61 $term_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
|
Chris@0
|
62 $term_storage->resetCache([$child_term_id]);
|
Chris@0
|
63 $child_term = Term::load($child_term_id);
|
Chris@0
|
64 $this->assertTrue(!empty($child_term), 'Child term is not deleted if only one of its parents is removed.');
|
Chris@0
|
65
|
Chris@0
|
66 $parent_term2->delete();
|
Chris@0
|
67 $term_storage->resetCache([$child_term_id]);
|
Chris@0
|
68 $child_term = Term::load($child_term_id);
|
Chris@0
|
69 $this->assertTrue(empty($child_term), 'Child term is deleted if all of its parents are removed.');
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Test a taxonomy with terms that have multiple parents of different depths.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function testTaxonomyVocabularyTree() {
|
Chris@0
|
76 // Create a new vocabulary with 6 terms.
|
Chris@0
|
77 $vocabulary = $this->createVocabulary();
|
Chris@0
|
78 $term = [];
|
Chris@0
|
79 for ($i = 0; $i < 6; $i++) {
|
Chris@0
|
80 $term[$i] = $this->createTerm($vocabulary);
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 // Get the taxonomy storage.
|
Chris@0
|
84 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
|
Chris@0
|
85
|
Chris@0
|
86 // Set the weight on $term[1] so it appears before $term[5] when fetching
|
Chris@0
|
87 // the parents for $term[2], in order to test for a regression on
|
Chris@0
|
88 // \Drupal\taxonomy\TermStorageInterface::loadAllParents().
|
Chris@0
|
89 $term[1]->weight = -1;
|
Chris@0
|
90 $term[1]->save();
|
Chris@0
|
91
|
Chris@0
|
92 // $term[2] is a child of 1 and 5.
|
Chris@0
|
93 $term[2]->parent = [$term[1]->id(), $term[5]->id()];
|
Chris@0
|
94 $term[2]->save();
|
Chris@0
|
95 // $term[3] is a child of 2.
|
Chris@0
|
96 $term[3]->parent = [$term[2]->id()];
|
Chris@0
|
97 $term[3]->save();
|
Chris@0
|
98 // $term[5] is a child of 4.
|
Chris@0
|
99 $term[5]->parent = [$term[4]->id()];
|
Chris@0
|
100 $term[5]->save();
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Expected tree:
|
Chris@0
|
104 * term[0] | depth: 0
|
Chris@0
|
105 * term[1] | depth: 0
|
Chris@0
|
106 * -- term[2] | depth: 1
|
Chris@0
|
107 * ---- term[3] | depth: 2
|
Chris@0
|
108 * term[4] | depth: 0
|
Chris@0
|
109 * -- term[5] | depth: 1
|
Chris@0
|
110 * ---- term[2] | depth: 2
|
Chris@0
|
111 * ------ term[3] | depth: 3
|
Chris@0
|
112 */
|
Chris@0
|
113 // Count $term[1] parents with $max_depth = 1.
|
Chris@0
|
114 $tree = $taxonomy_storage->loadTree($vocabulary->id(), $term[1]->id(), 1);
|
Chris@0
|
115 $this->assertEqual(1, count($tree), 'We have one parent with depth 1.');
|
Chris@0
|
116
|
Chris@0
|
117 // Count all vocabulary tree elements.
|
Chris@0
|
118 $tree = $taxonomy_storage->loadTree($vocabulary->id());
|
Chris@0
|
119 $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');
|
Chris@0
|
120
|
Chris@0
|
121 // Count elements in every tree depth.
|
Chris@0
|
122 foreach ($tree as $element) {
|
Chris@0
|
123 if (!isset($depth_count[$element->depth])) {
|
Chris@0
|
124 $depth_count[$element->depth] = 0;
|
Chris@0
|
125 }
|
Chris@0
|
126 $depth_count[$element->depth]++;
|
Chris@0
|
127 }
|
Chris@0
|
128 $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
|
Chris@0
|
129 $this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
|
Chris@0
|
130 $this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
|
Chris@0
|
131 $this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
|
Chris@0
|
132
|
Chris@0
|
133 /** @var \Drupal\taxonomy\TermStorageInterface $storage */
|
Chris@0
|
134 $storage = \Drupal::entityManager()->getStorage('taxonomy_term');
|
Chris@0
|
135 // Count parents of $term[2].
|
Chris@0
|
136 $parents = $storage->loadParents($term[2]->id());
|
Chris@0
|
137 $this->assertEqual(2, count($parents), 'The term has two parents.');
|
Chris@0
|
138
|
Chris@0
|
139 // Count parents of $term[3].
|
Chris@0
|
140 $parents = $storage->loadParents($term[3]->id());
|
Chris@0
|
141 $this->assertEqual(1, count($parents), 'The term has one parent.');
|
Chris@0
|
142
|
Chris@0
|
143 // Identify all ancestors of $term[2].
|
Chris@0
|
144 $ancestors = $storage->loadAllParents($term[2]->id());
|
Chris@0
|
145 $this->assertEqual(4, count($ancestors), 'The term has four ancestors including the term itself.');
|
Chris@0
|
146
|
Chris@0
|
147 // Identify all ancestors of $term[3].
|
Chris@0
|
148 $ancestors = $storage->loadAllParents($term[3]->id());
|
Chris@0
|
149 $this->assertEqual(5, count($ancestors), 'The term has five ancestors including the term itself.');
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 }
|