You are here

public function VocabularyCrudTest::testTaxonomyVocabularyDeleteWithTerms in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php \Drupal\Tests\taxonomy\Kernel\VocabularyCrudTest::testTaxonomyVocabularyDeleteWithTerms()
  2. 10 core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php \Drupal\Tests\taxonomy\Kernel\VocabularyCrudTest::testTaxonomyVocabularyDeleteWithTerms()

Test deleting a taxonomy that contains terms.

File

core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php, line 45

Class

VocabularyCrudTest
Tests loading, saving and deleting vocabularies.

Namespace

Drupal\Tests\taxonomy\Kernel

Code

public function testTaxonomyVocabularyDeleteWithTerms() {
  $vocabulary = $this
    ->createVocabulary();
  $query = \Drupal::entityQuery('taxonomy_term')
    ->count();

  // Assert that there are no terms left.
  $this
    ->assertEquals(0, $query
    ->execute());
  $terms = [];
  for ($i = 0; $i < 5; $i++) {
    $terms[$i] = $this
      ->createTerm($vocabulary);
  }

  // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
  $terms[2]->parent = [
    $terms[1]
      ->id(),
  ];
  $terms[2]
    ->save();
  $terms[4]->parent = [
    $terms[1]
      ->id(),
    $terms[2]
      ->id(),
  ];
  $terms[4]
    ->save();

  // Assert that there are now 5 terms.
  $this
    ->assertEquals(5, $query
    ->execute());
  $vocabulary
    ->delete();

  // Assert that there are no terms left.
  $this
    ->assertEquals(0, $query
    ->execute());
}