View source
<?php
namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\taxonomy\Entity\Term;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class TermKernelTest extends KernelTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'filter',
'taxonomy',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'filter',
]);
$this
->installEntitySchema('taxonomy_term');
}
public function testTermDelete() {
$vocabulary = $this
->createVocabulary();
$valid_term = $this
->createTerm($vocabulary);
$valid_term
->delete();
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'vid' => $vocabulary
->id(),
]);
$this
->assertEmpty($terms, 'Vocabulary is empty after deletion');
}
public function testMultipleParentDelete() {
$vocabulary = $this
->createVocabulary();
$parent_term1 = $this
->createTerm($vocabulary);
$parent_term2 = $this
->createTerm($vocabulary);
$child_term = $this
->createTerm($vocabulary);
$child_term->parent = [
$parent_term1
->id(),
$parent_term2
->id(),
];
$child_term
->save();
$child_term_id = $child_term
->id();
$parent_term1
->delete();
$term_storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
$term_storage
->resetCache([
$child_term_id,
]);
$child_term = Term::load($child_term_id);
$this
->assertNotEmpty($child_term, 'Child term is not deleted if only one of its parents is removed.');
$parent_term2
->delete();
$term_storage
->resetCache([
$child_term_id,
]);
$child_term = Term::load($child_term_id);
$this
->assertEmpty($child_term, 'Child term is deleted if all of its parents are removed.');
}
public function testTaxonomyVocabularyTree() {
$vocabulary = $this
->createVocabulary();
$term = [];
for ($i = 0; $i < 6; $i++) {
$term[$i] = $this
->createTerm($vocabulary);
}
$taxonomy_storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
$term[1]->weight = -1;
$term[1]
->save();
$term[2]->parent = [
$term[1]
->id(),
$term[5]
->id(),
];
$term[2]
->save();
$term[3]->parent = [
$term[2]
->id(),
];
$term[3]
->save();
$term[5]->parent = [
$term[4]
->id(),
];
$term[5]
->save();
$tree = $taxonomy_storage
->loadTree($vocabulary
->id(), $term[1]
->id(), 1);
$this
->assertCount(1, $tree, 'We have one parent with depth 1.');
$tree = $taxonomy_storage
->loadTree($vocabulary
->id());
$this
->assertCount(8, $tree, 'We have all vocabulary tree elements.');
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.');
$storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$parents = $storage
->loadParents($term[2]
->id());
$this
->assertCount(2, $parents, 'The term has two parents.');
$parents = $storage
->loadParents($term[3]
->id());
$this
->assertCount(1, $parents, 'The term has one parent.');
$ancestors = $storage
->loadAllParents($term[2]
->id());
$this
->assertCount(4, $ancestors, 'The term has four ancestors including the term itself.');
$ancestors = $storage
->loadAllParents($term[3]
->id());
$this
->assertCount(5, $ancestors, 'The term has five ancestors including the term itself.');
}
public function testTermPreview() {
$entity_manager = \Drupal::entityTypeManager();
$vocabulary = $this
->createVocabulary();
$term = $entity_manager
->getStorage('taxonomy_term')
->create([
'vid' => $vocabulary
->id(),
'name' => 'Inator',
]);
$render_array = $entity_manager
->getViewBuilder('taxonomy_term')
->view($term);
$this
->assertNotEmpty($render_array, 'Term view builder is built.');
$rendered = \Drupal::service('renderer')
->renderPlain($render_array);
$this
->assertNotEmpty(trim($rendered), 'Term is able to be rendered.');
}
}