You are here

public function FeedsMapperFileTestCase::testClearOutValues in Feeds 7.2

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

File

tests/feeds_mapper_file.test, line 763
Contains FeedsMapperFileTestCase.

Class

FeedsMapperFileTestCase
Test case for Filefield mapper mappers/filefield.inc.

Code

public function testClearOutValues() {
  variable_set('feeds_never_use_curl', TRUE);
  $this
    ->createContentType(array(), array(
    'files' => 'file',
  ));
  $typename = $this
    ->createContentType(array(), array(
    'images' => 'image',
  ));

  // Enable title and alt mapping.
  $edit = array(
    'instance[settings][alt_field]' => 1,
    'instance[settings][title_field]' => 1,
  );
  $this
    ->drupalPost("admin/structure/types/manage/{$typename}/fields/field_images", $edit, t('Save settings'));

  // Create and configure importer.
  $this
    ->createImporterConfiguration('Content CSV', 'csv');
  $this
    ->setSettings('csv', NULL, array(
    'content_type' => '',
    'import_period' => FEEDS_SCHEDULE_NEVER,
  ));
  $this
    ->setPlugin('csv', 'FeedsCSVParser');
  $this
    ->setSettings('csv', 'FeedsNodeProcessor', array(
    'bundle' => $typename,
    'update_existing' => 1,
  ));
  $this
    ->addMappings('csv', array(
    0 => array(
      'source' => 'guid',
      'target' => 'guid',
      'unique' => TRUE,
    ),
    1 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    2 => array(
      'source' => 'file',
      'target' => 'field_images:uri',
    ),
    3 => array(
      'source' => 'title2',
      'target' => 'field_images:title',
    ),
    4 => array(
      'source' => 'alt',
      'target' => 'field_images:alt',
    ),
  ));

  // Import.
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-remote.csv', array(
      'absolute' => TRUE,
    )),
  );
  $this
    ->drupalPost('import/csv', $edit, 'Import');
  $this
    ->assertText('Created 5 nodes');

  // Assert files exist.
  $files = $this
    ->listTestFiles();
  foreach ($files as $file) {
    $file_path = drupal_realpath('public://') . '/' . str_replace(' ', '_', $file);
    $this
      ->assertTrue(file_exists($file_path), format_string('The file %file exists.', array(
      '%file' => $file_path,
    )));
  }

  // Assert files exists with the expected alt/title on node edit form.
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'csv')
    ->execute()
    ->fetchAll();
  foreach ($entities as $i => $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertRaw(str_replace(' ', '_', array_shift($files)));
    $this
      ->assertRaw("Alt text {$i}");
    $this
      ->assertRaw("Title text {$i}");
  }

  // Import CSV with empty alt/title fields and check if these are removed.
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-empty-alt-title.csv', array(
      'absolute' => TRUE,
    )),
  );
  $this
    ->drupalPost('import/csv', $edit, 'Import');
  $this
    ->assertText('Updated 5 nodes');
  $files = $this
    ->listTestFiles();
  foreach ($entities as $i => $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertRaw(str_replace(' ', '_', array_shift($files)));
    $this
      ->assertNoRaw("Alt text {$i}");
    $this
      ->assertNoRaw("Title text {$i}");
  }

  // Import CSV with empty file fields and check if all files are removed.
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-empty.csv', array(
      'absolute' => TRUE,
    )),
  );
  $this
    ->drupalPost('import/csv', $edit, 'Import');
  $this
    ->assertText('Updated 5 nodes');

  // Assert files are removed.
  $files = $this
    ->listTestFiles();
  foreach ($files as $file) {
    $file_path = drupal_realpath('public://') . '/' . str_replace(' ', '_', $file);
    $this
      ->assertFalse(file_exists($file_path), format_string('The file %file no longer exists.', array(
      '%file' => $file_path,
    )));
  }

  // Check if the files are removed from the node edit form as well.
  foreach ($entities as $i => $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertNoRaw(str_replace(' ', '_', array_shift($files)));
  }
}