You are here

public function TaxonomyMenuWebTestCase::setUpTermReferenceAndNodes in Taxonomy menu 8

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 lib/Drupal/taxonomy_menu/Tests/TaxonomyMenuConfigurationTest.php
Tests Taxonommy Menu "Node count" option.
TaxonomyMenuConfigurationTest::testTaxonomyMenuHideEmptyTerms in lib/Drupal/taxonomy_menu/Tests/TaxonomyMenuConfigurationTest.php
Tests Taxonommy Menu "Hide Empty terms" option.

File

lib/Drupal/taxonomy_menu/Tests/TaxonomyMenuWebTestCase.php, line 131
Definition of Drupal\taxonomy_menu\Tests\TaxonomyMenuWebTestCase.

Class

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

Namespace

Drupal\taxonomy_menu\Tests

Code

public function setUpTermReferenceAndNodes($type, $terms_index) {
  $this->field_name = 'taxonomy_' . $this->vocabulary
    ->id();
  entity_create('field_config', array(
    'name' => $this->field_name,
    'entity_type' => 'node',
    'type' => 'taxonomy_term_reference',
    'cardinality' => FieldDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $this->vocabulary
            ->id(),
          'parent' => 0,
        ),
      ),
    ),
  ))
    ->save();
  entity_create('field_instance_config', array(
    'field_name' => $this->field_name,
    'bundle' => $type,
    'entity_type' => 'node',
  ))
    ->save();
  entity_get_form_display('node', 'article', 'default')
    ->setComponent($this->field_name, array(
    'type' => 'options_select',
  ))
    ->save();
  entity_get_display('node', 'article', 'default')
    ->setComponent($this->field_name, array(
    'type' => 'taxonomy_term_reference_link',
  ))
    ->save();

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