You are here

function HierarchicalTermFormatterTestCase::testVocabTerms in Hierarchical Term Formatter 7

Test that vocab and terms are created.

File

./hierarchical_term_formatter.test, line 239
Tests for Field Multiple Limit, based on examples from field.test

Class

HierarchicalTermFormatterTestCase
Test the field formatter settings work.

Code

function testVocabTerms() {

  // Check that our test vocab and terms were added.
  $this
    ->drupalGet('admin/structure/taxonomy');
  $this
    ->assertText($this->vocabulary->name, 'Vocabulary found in the vocabulary overview listing.');
  $this
    ->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
  $this
    ->assertText($this->terms['names'][0], 'Term found in the vocabulary term list.');

  //Test that the terms are display according to the formatter settings.
  $instance = field_read_instance($this->entity_type, $this->field_name, $this->bundle);
  $this
    ->createNode();
  $this
    ->drupalGet('node/' . $this->node_id);

  // Test display all terms.
  foreach ($this->terms['names'] as $name) {
    $this
      ->assertText($name, 'Expected term is displayed.');
  }

  // Test for separator. Separator should only appear between parent and
  // child terms.
  foreach ($this->terms['terms'] as $term) {
    $term_tree = array_reverse(taxonomy_get_parents_all($term->tid));

    // Get rid of the term itself.
    array_pop($term_tree);
    if (count($term_tree)) {

      // Next one to pop is the term's parent.
      $parent = array_pop($term_tree);
      $this
        ->assertPattern('/' . $parent->name . $instance['display']['default']['settings']['separator'] . $term->name . '/', 'Pattern appears between parent and child term.');
    }
  }

  // Test linked terms.
  $instance['display']['default']['settings']['link'] = 1;
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {
    $this
      ->assertPattern('/\\>' . $term->name . '<\\/a>/', 'Term is linked.');
  }

  // Test wrapped terms.
  $instance['display']['default']['settings']['link'] = '';
  $instance['display']['default']['settings']['wrap'] = 'span';
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {
    $this
      ->assertPattern('/\\>' . $term->name . '<\\/span>/', 'Term is wrapped.');
  }

  // Test order. 0 is the first root term. 10 should be a child of 0 based on
  // the way the hierarchy was built.
  $first_term = $this->terms['terms'][0];
  $last_term = $this->terms['terms'][10];
  $this
    ->assertPattern('/' . $first_term->name . '.*' . $last_term->name . '/', 'Natural order is displayed.');
  $instance['display']['default']['settings']['reverse'] = TRUE;
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  $this
    ->assertPattern('/' . $last_term->name . '.*' . $first_term->name . '/', 'Reverse order is displayed.');

  // Test parents only display.
  $instance = field_read_instance($this->entity_type, $this->instance['field_name'], $this->instance['bundle']);
  $instance['display']['default']['settings']['display'] = 'parents';
  $instance['display']['default']['settings']['wrap'] = '';
  $instance['display']['default']['settings']['reverse'] = FALSE;
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {

    // Parents display should show terms only if they have children.
    $term_tree = array_reverse(taxonomy_get_parents_all($term->tid));

    // Get rid of the term itself.
    array_pop($term_tree);
    if (count($term_tree)) {

      // Next one to pop is the term's parent.
      $parent = array_pop($term_tree);
      $this
        ->assertText($parent->name, 'Expected parent is displayed.');

      // Check that the term has no children before testing it is not shown.
      $children = taxonomy_get_children($term->tid);
      if (empty($children)) {
        $this
          ->assertNoText($term->name, 'Not-Expected child is not displayed.');
      }
    }
  }

  // Test root term only display.
  $instance = field_read_instance($this->entity_type, $this->instance['field_name'], $this->instance['bundle']);
  $instance['display']['default']['settings']['display'] = 'root';
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {
    $parents = taxonomy_get_parents_all($term->tid);

    // Root is the last array item.
    $root_term = array(
      array_pop($parents),
    );
    if (count($root_term)) {
      preg_match_all('/' . $root_term[0]->name . '/', $this->content, $matches);
      $this
        ->assertTrue(count($matches[0]) === 1, 'Expected root is displayed once.');

      // If the current term is not a root term, test that the current term is
      // not displayed.
      if ($root_term['0']->tid !== $term->tid) {
        $this
          ->assertNoText($term->name, 'Not-Expected child is not displayed.');
      }
    }
  }

  // Test non-root / non-topmost terms.
  $instance = field_read_instance($this->entity_type, $this->instance['field_name'], $this->instance['bundle']);
  $instance['display']['default']['settings']['display'] = 'nonroot';
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {
    $parents = taxonomy_get_parents_all($term->tid);

    // Test that the term is present. More than 1 $parents
    // means $term is not a root term.
    if (count($parents) > 1) {
      $this
        ->assertText($term->name, 'Expected non-root term is displayed. ' . $term->name);
    }

    // Check all parents to account for terms with multiple parents -- a
    // second parent term may be in the middle of the $parents array.
    foreach ($parents as $possible_root) {
      $get_parents = taxonomy_get_parents($possible_root->tid);

      // This is a topmost term.
      if (empty($get_parents)) {
        $this
          ->assertNoText($possible_root->name, 'Not-Expected root term is displayed. ' . $possible_root->name);
      }
    }
  }

  // Test leaf display.
  $instance = field_read_instance($this->entity_type, $this->instance['field_name'], $this->instance['bundle']);
  $instance['display']['default']['settings']['display'] = 'leaf';
  field_update_instance($instance);

  // Create a new node with random selection of terms.
  $random = TRUE;
  $this
    ->createNode($random);
  $this
    ->drupalGet('node/' . $this->node_id);
  foreach ($this->terms['terms'] as $term) {
    if (isset($this->random_terms[$term->tid])) {
      $this
        ->assertText($term->name, 'Expected leaf is displayed.');
    }
    else {
      $this
        ->assertNoText($term->name, 'Unselected term is not displayed.');
    }
  }
}