You are here

public function FeedsMapperTaxonomyTestCase::testClearOutValues in Feeds 7.2

Tests if values are cleared out when an empty value or no value is provided.

File

tests/feeds_mapper_taxonomy.test, line 607
Contains FeedsMapperTaxonomyTestCase.

Class

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

Code

public function testClearOutValues() {

  // 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' => 1,
  ));
  $this
    ->addMappings('node', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    1 => array(
      'source' => 'alpha',
      'target' => 'field_tags',
      'term_search' => 0,
      'autocreate' => 1,
    ),
    2 => array(
      'source' => 'guid',
      'target' => 'guid',
      'unique' => TRUE,
    ),
  ));
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/content.csv');
  $this
    ->assertText('Created 2 nodes');

  // Check the imported nodes.
  $terms1 = taxonomy_get_term_by_name('Lorem');
  $term1 = reset($terms1);
  $terms2 = taxonomy_get_term_by_name('Ut wisi');
  $term2 = reset($terms2);
  $taxonomy_values = array(
    1 => $term1->tid,
    2 => $term2->tid,
  );
  for ($i = 1; $i <= 2; $i++) {
    $this
      ->drupalGet("node/{$i}/edit");
    $this
      ->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
  }

  // Import CSV file with empty values.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/content_empty.csv');
  $this
    ->assertText('Updated 2 nodes');

  // Check if the taxonomy reference field was cleared out for node 1.
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertFieldByName('field_tags[und][]', '_none');
  $this
    ->drupalGet('node/1');
  $this
    ->assertNoText('field_tags');

  // Check if zero's didn't cleared out the taxonomy reference field for
  // node 2.
  $terms0 = taxonomy_get_term_by_name('0');
  $term0 = reset($terms0);
  $this
    ->drupalGet('node/2/edit');
  $this
    ->assertFieldByName('field_tags[und][]', $term0->tid);
  $this
    ->drupalGet('node/2');
  $this
    ->assertText('field_tags');

  // Re-import the first file again and check if the values returned.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/content.csv');
  $this
    ->assertText('Updated 2 nodes');
  for ($i = 1; $i <= 2; $i++) {
    $this
      ->drupalGet("node/{$i}/edit");
    $this
      ->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
  }

  // Import CSV file with non-existent values.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/content_non_existent.csv');
  $this
    ->assertText('Updated 2 nodes');

  // Check if the taxonomy reference field was cleared out for node 1.
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertFieldByName('field_tags[und][]', '_none');
  $this
    ->drupalGet('node/1');
  $this
    ->assertNoText('field_tags');
}