function TermKernelTest::testTaxonomyVocabularyTree

Same name in other branches
  1. 8.9.x core/modules/taxonomy/tests/src/Kernel/TermKernelTest.php \Drupal\Tests\taxonomy\Kernel\TermKernelTest::testTaxonomyVocabularyTree()
  2. 10 core/modules/taxonomy/tests/src/Kernel/TermKernelTest.php \Drupal\Tests\taxonomy\Kernel\TermKernelTest::testTaxonomyVocabularyTree()
  3. 11.x core/modules/taxonomy/tests/src/Kernel/TermKernelTest.php \Drupal\Tests\taxonomy\Kernel\TermKernelTest::testTaxonomyVocabularyTree()

Tests a taxonomy with terms that have multiple parents of different depths.

File

core/modules/taxonomy/tests/src/Kernel/TermKernelTest.php, line 71

Class

TermKernelTest
Kernel tests for taxonomy term functions.

Namespace

Drupal\Tests\taxonomy\Kernel

Code

public function testTaxonomyVocabularyTree() {
    // Create a new vocabulary with 6 terms.
    $vocabulary = $this->createVocabulary();
    $term = [];
    for ($i = 0; $i < 6; $i++) {
        $term[$i] = $this->createTerm($vocabulary);
    }
    // Get the taxonomy storage.
    $taxonomy_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('taxonomy_term');
    // 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();
    // $term[2] is a child of 1 and 5.
    $term[2]->parent = [
        $term[1]->id(),
        $term[5]->id(),
    ];
    $term[2]->save();
    // $term[3] is a child of 2.
    $term[3]->parent = [
        $term[2]->id(),
    ];
    $term[3]->save();
    // $term[5] is a child of 4.
    $term[5]->parent = [
        $term[4]->id(),
    ];
    $term[5]->save();
    
    /**
     * 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.
    $tree = $taxonomy_storage->loadTree($vocabulary->id(), $term[1]->id(), 1);
    $this->assertCount(1, $tree, 'We have one parent with depth 1.');
    // Count all vocabulary tree elements.
    $tree = $taxonomy_storage->loadTree($vocabulary->id());
    $this->assertCount(8, $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->assertEquals(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
    $this->assertEquals(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
    $this->assertEquals(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
    $this->assertEquals(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
    
    /** @var \Drupal\taxonomy\TermStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    // Count parents of $term[2].
    $parents = $storage->loadParents($term[2]->id());
    $this->assertCount(2, $parents, 'The term has two parents.');
    // Count parents of $term[3].
    $parents = $storage->loadParents($term[3]->id());
    $this->assertCount(1, $parents, 'The term has one parent.');
    // Identify all ancestors of $term[2].
    $ancestors = $storage->loadAllParents($term[2]->id());
    $this->assertCount(4, $ancestors, 'The term has four ancestors including the term itself.');
    // Identify all ancestors of $term[3].
    $ancestors = $storage->loadAllParents($term[3]->id());
    $this->assertCount(5, $ancestors, 'The term has five ancestors including the term itself.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.