1 entityreference.taxonomy.test protected EntityReferenceTaxonomyTestCase::setupForIndex()

Add an ER field from node/post to taxonomy and a term reference field.

File

core/modules/entityreference/tests/entityreference.taxonomy.test, line 18

Class

EntityReferenceTaxonomyTestCase
Test for Entity Reference taxonomy integration.

Code

protected function setupForIndex() {
  // Create an entity reference field.
  $field = array(
    'entity_types' => array('node'),
    'settings' => array(
      'handler' => 'base',
      'target_type' => 'taxonomy_term',
      'handler_settings' => array(
        'target_bundles' => array(),
      ),
    ),
    'field_name' => 'field_entityreference_term',
    'type' => 'entityreference',
  );
  $field = field_create_field($field);
  $instance = array(
    'field_name' => 'field_entityreference_term',
    'bundle' => 'post',
    'entity_type' => 'node',
  );

  // Enable the taxonomy-index behavior.
  $instance['settings']['behaviors']['taxonomy']['status'] = TRUE;
  field_create_instance($instance);

  // Create a term reference field.
  $field = array(
    'translatable' => FALSE,
    'entity_types' => array('node'),
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => 'terms',
          'parent' => 0,
        ),
      ),
    ),
    'field_name' => 'field_taxonomy_term',
    'type' => 'taxonomy_term_reference',
  );
  $field = field_create_field($field);
  $instance = array(
    'field_name' => 'field_taxonomy_term',
    'bundle' => 'post',
    'entity_type' => 'node',
  );
  field_create_instance($instance);

  // Create a terms vocabulary.
  $vocabulary = new TaxonomyVocabulary(array(
    'name' => 'Terms',
    'machine_name' => 'terms',
  ));
  taxonomy_vocabulary_save($vocabulary);

  // Create term.
  for ($i = 1; $i <= 2; $i++) {
    $term = entity_create('taxonomy_term', array(
      'name' => "term $i",
      'vocabulary' => $vocabulary->machine_name,
      'langcode' => LANGUAGE_NONE,
    ));
    taxonomy_term_save($term);
  }
}