function VocabularyUnitTest::testTaxonomyVocabularyDeleteWithTerms

Test deleting a taxonomy that contains terms.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php, line 41
Definition of Drupal\taxonomy\Tests\VocabularyUnitTest.

Class

VocabularyUnitTest
Tests for taxonomy vocabulary functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyVocabularyDeleteWithTerms() {

  // Delete any existing vocabularies.
  foreach (taxonomy_vocabulary_load_multiple() as $vocabulary) {
    $vocabulary
      ->delete();
  }

  // Assert that there are no terms left.
  $this
    ->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')
    ->fetchField());

  // Create a new vocabulary and add a few terms to it.
  $vocabulary = $this
    ->createVocabulary();
  $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, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')
    ->fetchField());
  $vocabulary
    ->delete();

  // Assert that there are no terms left.
  $this
    ->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')
    ->fetchField());
}