You are here

public function FeedsMapperFileTestCase::testFileExistsReplaceIfDifferent in Feeds 7.2

Test mapping of local resources with the file exists "Replace if different" setting.

In this test, files to import should only be replaced under the following circumstances:

  • A file the same name already exist in the destination folder;
  • AND this file is different.

Example: on the destination folder there exist two files: one called 'foo.jpg' and an other called 'bar.jpg'. On an import two files with the same name are imported. The 'foo.jpg' is exactly the same as the one that already exist on the destination, but 'bar.jpg' is different. In this case, only 'bar.jpg' should get imported and it should overwrite the existing 'bar.jpg'. Importing 'foo.jpg' should be skipped as it is already there. The file's timestamp will remain the same.

File

tests/feeds_mapper_file.test, line 445
Contains FeedsMapperFileTestCase.

Class

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

Code

public function testFileExistsReplaceIfDifferent() {
  $source = 'public://images';
  $dest = 'public://destination_replace_diff';

  // 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_replace_diff'.
  $typename = $this
    ->createContentTypeWithFileField('destination_replace_diff');

  // Shuffle a couple of the file names so the files appear to be different.
  // Leave a few others the same.
  $same = array(
    'foosball.jpeg' => 'foosball.jpeg',
    'attersee.jpeg' => 'attersee.jpeg',
    'hstreet.jpeg' => 'hstreet.jpeg',
  );
  $different = array(
    'la fayette.jpeg' => 'tubing.jpeg',
    'tubing.jpeg' => 'la fayette.jpeg',
  );

  // Copy files with the same name to the destination folder. A few of them
  // however, will be different. Only these files should get replaced upon
  // import.
  $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 are different.
  foreach ($different as $file) {
    if (is_file("{$source}/{$file}")) {
      $message = "{$source}/{$file} is NOT the same as {$dest}/{$file}";
      $this
        ->assertFalse(file_feeds_file_compare("{$source}/{$file}", "{$dest}/{$file}"), $message);
    }
  }

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

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

  // Assert that some files have been kept the same.
  foreach ($same as $file) {
    if (is_file("{$source}/{$file}")) {
      $message = "{$source}/{$file} is STILL the same as {$dest}/{$file}";
      $this
        ->assertTrue(file_feeds_file_compare("{$source}/{$file}", "{$dest}/{$file}"), $message);
      $message = "{$dest}/{$file} was not replaced (modification time is the same as before import)";
      $this
        ->assertEqual(filemtime("{$dest}/{$file}"), $file_timestamps[$file], $message);
    }
  }

  // Assert that some files were replaced.
  foreach ($different as $file) {
    if (is_file("{$source}/{$file}")) {
      $message = "{$source}/{$file} successfully replaced {$dest}/{$file}";
      $this
        ->assertTrue(file_feeds_file_compare("{$source}/{$file}", "{$dest}/{$file}"), $message);
      $this
        ->assertNotEqual(filemtime("{$dest}/{$file}"), $file_timestamps[$file], $message);
    }
  }

  // Clean up the last import.
  $this
    ->drupalPost('import/node_replace_diff/delete-items', array(), 'Delete');
}