TermKernelTest.php 5.25 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Kernel;
4

5
use Drupal\taxonomy\Entity\Term;
6
use Drupal\KernelTests\KernelTestBase;
7
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
8

9
/**
10
 * Kernel tests for taxonomy term functions.
11 12
 *
 * @group taxonomy
13
 */
14
class TermKernelTest extends KernelTestBase {
15

16 17 18 19 20
  use TaxonomyTestTrait;

  /**
   * {@inheritdoc}
   */
21
  public static $modules = ['filter', 'taxonomy', 'text', 'user'];
22 23 24 25 26 27

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
28
    $this->installConfig(['filter']);
29 30 31 32 33 34 35 36
    $this->installEntitySchema('taxonomy_term');
  }

  /**
   * Deleting terms should also remove related vocabulary.
   * Deleting an invalid term should silently fail.
   */
  public function testTermDelete() {
37 38 39
    $vocabulary = $this->createVocabulary();
    $valid_term = $this->createTerm($vocabulary);
    // Delete a valid term.
40
    $valid_term->delete();
41
    $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
42 43 44
    $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');

    // Delete an invalid term. Should not throw any notices.
45
    entity_delete_multiple('taxonomy_term', [42]);
46 47
  }

48 49 50
  /**
   * Deleting a parent of a term with multiple parents does not delete the term.
   */
51
  public function testMultipleParentDelete() {
52 53 54 55
    $vocabulary = $this->createVocabulary();
    $parent_term1 = $this->createTerm($vocabulary);
    $parent_term2 = $this->createTerm($vocabulary);
    $child_term = $this->createTerm($vocabulary);
56
    $child_term->parent = [$parent_term1->id(), $parent_term2->id()];
57 58 59 60
    $child_term->save();
    $child_term_id = $child_term->id();

    $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 65 66
    $this->assertTrue(!empty($child_term), 'Child term is not deleted if only one of its parents is removed.');

    $parent_term2->delete();
67
    $term_storage->resetCache([$child_term_id]);
68
    $child_term = Term::load($child_term_id);
69 70 71
    $this->assertTrue(empty($child_term), 'Child term is deleted if all of its parents are removed.');
  }

72 73 74
  /**
   * Test a taxonomy with terms that have multiple parents of different depths.
   */
75
  public function testTaxonomyVocabularyTree() {
76 77
    // Create a new vocabulary with 6 terms.
    $vocabulary = $this->createVocabulary();
78
    $term = [];
79 80 81 82
    for ($i = 0; $i < 6; $i++) {
      $term[$i] = $this->createTerm($vocabulary);
    }

83 84 85
    // Get the taxonomy storage.
    $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');

86 87 88 89 90 91
    // Set the weight on $term[1] so it appears before $term[5] when fetching
    // the parents for $term[2], in order to test for a regression on
    // \Drupal\taxonomy\TermStorageInterface::loadAllParents().
    $term[1]->weight = -1;
    $term[1]->save();

92
    // $term[2] is a child of 1 and 5.
93
    $term[2]->parent = [$term[1]->id(), $term[5]->id()];
94
    $term[2]->save();
95
    // $term[3] is a child of 2.
96
    $term[3]->parent = [$term[2]->id()];
97
    $term[3]->save();
98
    // $term[5] is a child of 4.
99
    $term[5]->parent = [$term[4]->id()];
100
    $term[5]->save();
101 102 103 104 105 106 107 108 109 110 111 112 113

    /**
     * Expected tree:
     * term[0] | depth: 0
     * term[1] | depth: 0
     * -- term[2] | depth: 1
     * ---- term[3] | depth: 2
     * term[4] | depth: 0
     * -- term[5] | depth: 1
     * ---- term[2] | depth: 2
     * ------ term[3] | depth: 3
     */
    // Count $term[1] parents with $max_depth = 1.
114
    $tree = $taxonomy_storage->loadTree($vocabulary->id(), $term[1]->id(), 1);
115 116 117
    $this->assertEqual(1, count($tree), 'We have one parent with depth 1.');

    // Count all vocabulary tree elements.
118
    $tree = $taxonomy_storage->loadTree($vocabulary->id());
119 120 121 122 123 124 125 126 127 128 129 130 131
    $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');

    // Count elements in every tree depth.
    foreach ($tree as $element) {
      if (!isset($depth_count[$element->depth])) {
        $depth_count[$element->depth] = 0;
      }
      $depth_count[$element->depth]++;
    }
    $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
    $this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
    $this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
    $this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    /** @var \Drupal\taxonomy\TermStorageInterface $storage */
    $storage = \Drupal::entityManager()->getStorage('taxonomy_term');
    // Count parents of $term[2].
    $parents = $storage->loadParents($term[2]->id());
    $this->assertEqual(2, count($parents), 'The term has two parents.');

    // Count parents of $term[3].
    $parents = $storage->loadParents($term[3]->id());
    $this->assertEqual(1, count($parents), 'The term has one parent.');

    // Identify all ancestors of $term[2].
    $ancestors = $storage->loadAllParents($term[2]->id());
    $this->assertEqual(4, count($ancestors), 'The term has four ancestors including the term itself.');

    // Identify all ancestors of $term[3].
    $ancestors = $storage->loadAllParents($term[3]->id());
    $this->assertEqual(5, count($ancestors), 'The term has five ancestors including the term itself.');
150
  }
151

152
}