function TermTest::testTaxonomyTermHierarchy

Test terms in a single and multiple hierarchy.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php, line 67
Definition of Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests for taxonomy term functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyTermHierarchy() {

  // Create two taxonomy terms.
  $term1 = $this
    ->createTerm($this->vocabulary);
  $term2 = $this
    ->createTerm($this->vocabulary);

  // Check that hierarchy is flat.
  $vocabulary = taxonomy_vocabulary_load($this->vocabulary
    ->id());
  $this
    ->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.');

  // Edit $term2, setting $term1 as parent.
  $edit = array();
  $edit['parent[]'] = array(
    $term1
      ->id(),
  );
  $this
    ->drupalPost('taxonomy/term/' . $term2
    ->id() . '/edit', $edit, t('Save'));

  // Check the hierarchy.
  $children = taxonomy_term_load_children($term1
    ->id());
  $parents = taxonomy_term_load_parents($term2
    ->id());
  $this
    ->assertTrue(isset($children[$term2
    ->id()]), 'Child found correctly.');
  $this
    ->assertTrue(isset($parents[$term1
    ->id()]), 'Parent found correctly.');

  // Load and save a term, confirming that parents are still set.
  $term = taxonomy_term_load($term2
    ->id());
  $term
    ->save();
  $parents = taxonomy_term_load_parents($term2
    ->id());
  $this
    ->assertTrue(isset($parents[$term1
    ->id()]), 'Parent found correctly.');

  // Create a third term and save this as a parent of term2.
  $term3 = $this
    ->createTerm($this->vocabulary);
  $term2->parent = array(
    $term1
      ->id(),
    $term3
      ->id(),
  );
  $term2
    ->save();
  $parents = taxonomy_term_load_parents($term2
    ->id());
  $this
    ->assertTrue(isset($parents[$term1
    ->id()]) && isset($parents[$term3
    ->id()]), 'Both parents found successfully.');
}