You are here

public function FeedsMapperMultilingualFieldsTestCase::testClearOutValues in Feeds 7.2

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

File

tests/feeds_mapper_multilingual_fields.test, line 609
Contains FeedsMapperMultilingualFieldsTestCase.

Class

FeedsMapperMultilingualFieldsTestCase
Tests field mapping with multiple languages.

Code

public function testClearOutValues() {

  // Set to update existing nodes.
  $this
    ->setSettings('node', 'FeedsNodeProcessor', array(
    'update_existing' => FEEDS_UPDATE_EXISTING,
  ));

  // Add English mappers.
  $index = 2;
  $mappings = $this
    ->getMappingsInLanguage('en', $index);

  // Append "_en" to each source name.
  foreach ($mappings as &$mapping) {
    $mapping['source'] .= '_en';
  }
  $this
    ->addMappings('node', $mappings);
  $index += count($mappings);

  // Add French mappers.
  $mappings = $this
    ->getMappingsInLanguage('fr', $index);

  // Append "_fr" to each source name.
  foreach ($mappings as &$mapping) {
    $mapping['source'] .= '_fr';
  }
  $this
    ->addMappings('node', $mappings);

  // Import file that has items with both English and French field values.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/multilingual_en_fr.csv');
  $this
    ->assertText(t('Created 1 node'));

  // Now import a file where the French remained, but the English values were
  // removed.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/multilingual_en_fr_empty.csv');
  $this
    ->assertText(t('Updated 1 node'));

  // Load node.
  $node = node_load(1, NULL, TRUE);

  // Check that the English values are gone, but the French values are still
  // there.
  $fields = array(
    'body',
    'field_date',
    'field_datestamp',
    'field_datetime',
    'field_image',
    'field_link',
    'field_list_boolean',
    'field_number_decimal',
    'field_number_float',
    'field_number_integer',
    'field_category',
    'field_text',
  );
  foreach ($fields as $field_name) {
    $this
      ->assertTrue(empty($node->{$field_name}['en']), format_string('The field %field is empty.', array(
      '%field' => $field_name,
    )));
  }

  // Inspect availability of French values.
  $french = $this
    ->getFrenchValues($node) + array(
    'field_category' => array(
      'expected' => 2,
      'actual' => $node->field_category['fr'][0]['tid'],
    ),
  );

  // Since the image was placed on the node again, its file name is now
  // "la fayette_0.jpeg."
  $french['field_image']['expected'] = 'la fayette_0.jpeg';
  foreach ($french as $field_name => $value) {
    $this
      ->assertEqual($value['expected'], $value['actual'], format_string('The French field %field has the expected value (actual: @actual).', array(
      '%field' => $field_name,
      '@actual' => $value['actual'],
    )));
  }
}