You are here

function TaxonomyTermTestCase::testTaxonomyTermHierarchy in Drupal 7

Test terms in a single and multiple hierarchy.

File

modules/taxonomy/taxonomy.test, line 574
Tests for taxonomy.module.

Class

TaxonomyTermTestCase
Tests for taxonomy term functions.

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->vid);
  $this
    ->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.');

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

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

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

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