You are here

public function FeedsMapperFileTestCase::testFileExistsSkip in Feeds 7.2

Test mapping of local resources with the file exists "Skip existig" setting.

In this test, files should only be imported if no file exist yet with the given name. Example: on the destination folder there exist a file named 'foo.jpg'. When importing a file with the same name, that file should not be imported as there already is a file with that name.

File

tests/feeds_mapper_file.test, line 552
Contains FeedsMapperFileTestCase.

Class

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

Code

public function testFileExistsSkip() {
  $source = 'public://images';
  $dest = 'public://destination_skip';

  // Copy the files to import to the folder 'images'.
  $this
    ->copyDir($this
    ->absolutePath() . '/tests/feeds/assets', $source);

  // Create a content type. Save imported files into the directory
  // 'destination_skip'.
  $typename = $this
    ->createContentTypeWithFileField('destination_skip');

  // Copy a few images also to the destination directory.
  $same = array(
    'foosball.jpeg' => 'foosball.jpeg',
    'attersee.jpeg' => 'attersee.jpeg',
    'hstreet.jpeg' => 'hstreet.jpeg',
  );
  $different = array(
    'la fayette.jpeg' => FALSE,
    'tubing.jpeg' => FALSE,
  );
  $this
    ->copyDir($this
    ->absolutePath() . '/tests/feeds/assets', $dest, $same + $different);

  // Note the timestamps that the files got in the destination folder.
  $file_timestamps = array();
  foreach (@scandir($dest) as $file) {
    $file_timestamps[$file] = filemtime("{$dest}/{$file}");
  }

  // Confirm that some of the files are the same.
  foreach ($same as $file) {
    if (is_file("{$source}/{$file}")) {
      $message = "{$source}/{$file} IS the same as {$dest}/{$file}";
      $this
        ->assertTrue(file_feeds_file_compare("{$source}/{$file}", "{$dest}/{$file}"), $message);
    }
  }

  // Confirm that some of the files do not exist.
  foreach ($different as $file => $value) {
    $message = "{$dest}/{$file} does not exist.";
    $this
      ->assertFalse(file_exists("{$dest}/{$file}"), $message);
  }

  // Create a CSV importer configuration.
  $this
    ->createImporterConfiguration('Node import from CSV -- File Exists Replace if Different', 'node_skip');
  $this
    ->setSettings('node_skip', NULL, array(
    'content_type' => '',
  ));
  $this
    ->setPlugin('node_skip', 'FeedsCSVParser');
  $this
    ->setSettings('node_skip', 'FeedsNodeProcessor', array(
    'bundle' => $typename,
  ));
  $this
    ->addMappings('node_skip', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    1 => array(
      'source' => 'file',
      'target' => 'field_files:uri',
      'file_exists' => FEEDS_FILE_EXISTS_SKIP,
    ),
  ));

  // Perform the import.
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
  );
  $this
    ->drupalPost('import/node_skip', $edit, 'Import');
  $this
    ->assertText('Created 5 nodes');

  // Assert that files that were already in the destination folder were not
  // overwritten.
  foreach ($same as $file) {
    if (is_file("{$source}/{$file}")) {
      $message = "{$dest}/{$file} was skipped (modification time is the same as before import)";
      $this
        ->assertEqual(filemtime("{$dest}/{$file}"), $file_timestamps[$file], $message);
    }
  }

  // Assert that the other files were added with the expected names.
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'node_skip')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $f = new FeedsEnclosure(array_shift($files), NULL);
    $this
      ->assertRaw('destination_skip/' . $f
      ->getUrlEncodedValue());
  }
}