You are here

function VocabularyCrudTest::testTaxonomyVocabularyDeleteWithTerms in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/Tests/VocabularyCrudTest.php \Drupal\taxonomy\Tests\VocabularyCrudTest::testTaxonomyVocabularyDeleteWithTerms()

Test deleting a taxonomy that contains terms.

File

core/modules/taxonomy/src/Tests/VocabularyCrudTest.php, line 38
Contains \Drupal\taxonomy\Tests\VocabularyCrudTest.

Class

VocabularyCrudTest
Tests loading, saving and deleting vocabularies.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyVocabularyDeleteWithTerms() {

  // Delete any existing vocabularies.
  foreach (Vocabulary::loadMultiple() as $vocabulary) {
    $vocabulary
      ->delete();
  }
  $query = \Drupal::entityQuery('taxonomy_term')
    ->count();

  // Assert that there are no terms left.
  $this
    ->assertEqual(0, $query
    ->execute(), 'There are no terms remaining.');
  $terms = array();
  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 = array(
    $terms[1]
      ->id(),
  );
  $terms[2]
    ->save();
  $terms[4]->parent = array(
    $terms[1]
      ->id(),
    $terms[2]
      ->id(),
  );
  $terms[4]
    ->save();

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

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