You are here

public function FeedsMapperTaxonomyTestCase::testAutoCreateUpdatesAllCaches in Feeds 7.2

Tests how term caching works across multiple term reference fields.

This specifically verifies that terms added to a vocabulary by one mapping are available for use by other fields that are mapping to the same vocabulary.

See also

https://www.drupal.org/project/feeds/issues/3091682

File

tests/feeds_mapper_taxonomy.test, line 369
Contains FeedsMapperTaxonomyTestCase.

Class

FeedsMapperTaxonomyTestCase
Test case for taxonomy mapper mappers/taxonomy.inc.

Code

public function testAutoCreateUpdatesAllCaches() {

  // Create a second term reference field base.
  $field = array(
    'field_name' => 'field_tags2',
    'type' => 'taxonomy_term_reference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => 'tags',
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);

  // Add a second term reference field instance to to feed node bundle.
  $this->article_tags2 = array(
    'field_name' => 'field_tags2',
    'bundle' => 'article',
    'entity_type' => 'node',
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
      ),
    ),
  );
  field_create_instance($this->article_tags2);

  // Create a CSV importer configuration.
  $this
    ->createImporterConfiguration('Node import from CSV', 'node');
  $this
    ->setSettings('node', NULL, array(
    'content_type' => '',
  ));
  $this
    ->setPlugin('node', 'FeedsFileFetcher');
  $this
    ->setPlugin('node', 'FeedsCSVParser');
  $this
    ->setSettings('node', 'FeedsNodeProcessor', array(
    'bundle' => 'article',
    'update_existing' => TRUE,
  ));
  $this
    ->addMappings('node', array(
    0 => array(
      'source' => 'guid',
      'target' => 'guid',
      'unique' => TRUE,
    ),
    1 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    2 => array(
      'source' => 'tag1',
      'target' => 'field_tags',
      'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
      'autocreate' => TRUE,
    ),
    3 => array(
      'source' => 'tag2',
      'target' => 'field_tags2',
      'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
      'autocreate' => TRUE,
    ),
  ));
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/taxonomy_multiple_tag_fields.csv');
  $this
    ->assertText('Created 2 nodes');
  $term_names = array(
    'Alpha',
    'Beta',
    'Kiwi',
  );
  foreach ($term_names as $term_name) {
    $loaded_term = taxonomy_get_term_by_name($term_name);
    $terms[$term_name] = reset($loaded_term);
  }
  $expected_node_values = array(
    1 => array(
      'field_tags' => $terms['Alpha']->tid,
      'field_tags2' => $terms['Beta']->tid,
    ),
    2 => array(
      'field_tags' => $terms['Kiwi']->tid,
      'field_tags2' => $terms['Kiwi']->tid,
    ),
  );
  foreach ($expected_node_values as $nid => $node_values) {
    $this
      ->drupalGet("node/{$nid}/edit");
    foreach ($node_values as $field_name => $field_value) {
      $this
        ->assertFieldByName("{$field_name}[und][]", $field_value);
    }
  }
}