You are here

public function TaxonomyMenuWebTestCase::setUpTermReferenceAndNodes in Taxonomy menu 7.2

Adds a taxonomy reference field to a content type and creates a number of nodes that references different taxonomy terms

Parameters

$type string: The content type's machine name to add a term reference field to.

$terms_index array: An array of term indexes from the terms hierarchy of this class. Each index will be used to attach a node to this term. Indexes can be duplicated in order to attach several nodes to the same term.

2 calls to TaxonomyMenuWebTestCase::setUpTermReferenceAndNodes()
TaxonomyMenuConfigurationTest::testTaxonomyMenuCountNodes in tests/taxonomy_menu.test
Tests Taxonommy Menu "Node count" option.
TaxonomyMenuConfigurationTest::testTaxonomyMenuHideEmptyTerms in tests/taxonomy_menu.test
Tests Taxonommy Menu "Hide Empty terms" option.

File

tests/taxonomy_menu.test, line 120
Defines abstract base test class for the Taxonomy menu module tests.

Class

TaxonomyMenuWebTestCase
Abstract class for Taxonomy menu testing. All Taxonomy menu tests should extend this class.

Code

public function setUpTermReferenceAndNodes($type, $terms_index) {
  $field = array(
    'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
    'type' => 'taxonomy_term_reference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $this->vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);
  $this->instance = array(
    'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
    'bundle' => $type,
    'entity_type' => 'node',
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
      ),
    ),
  );
  field_create_instance($this->instance);

  // Create nodes that reference each term represented by their indexes.
  foreach ($terms_index as $index) {
    $edit = array();
    $langcode = LANGUAGE_NONE;
    $edit["title"] = $this
      ->randomName();
    $edit["body[{$langcode}][0][value]"] = $this
      ->randomName();
    $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $this->terms_hierarchy[$index]->tid;
    $this
      ->drupalPost('node/add/article', $edit, t('Save'));
  }
}