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