You are here

public function FeedsMapperFileTestCase::testFileExistsRenameIfDifferent in Feeds 7.2

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

In this test, files to import should only be renamed 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 be renamed to 'bar_0.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 318
Contains FeedsMapperFileTestCase.

Class

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

Code

public function testFileExistsRenameIfDifferent() {
  $source = 'public://images';
  $dest = 'public://destination_rename_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_rename_diff'.
  $typename = $this
    ->createContentTypeWithFileField('destination_rename_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 renamed 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 Rename if Different', 'node_rename_diff');
  $this
    ->setSettings('node_rename_diff', NULL, array(
    'content_type' => '',
  ));
  $this
    ->setPlugin('node_rename_diff', 'FeedsCSVParser');
  $this
    ->setSettings('node_rename_diff', 'FeedsNodeProcessor', array(
    'bundle' => $typename,
  ));
  $this
    ->addMappings('node_rename_diff', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    1 => array(
      'source' => 'file',
      'target' => 'field_files:uri',
      'file_exists' => FEEDS_FILE_EXISTS_RENAME_DIFFERENT,
    ),
  ));

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

  // Assert that only files that were different should have been renamed.
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'node_rename_diff')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $f = new FeedsEnclosure(array_shift($files), NULL);
    $original_file = $f
      ->getUrlEncodedValue();
    $renamed_file = str_replace('.jpeg', '_0.jpeg', $f
      ->getUrlEncodedValue());
    if (isset($same[$original_file])) {

      // Assert that the file still has the same name.
      $this
        ->assertRaw('destination_rename_diff/' . $original_file);
    }
    else {

      // Assert that the file still has been renamed.
      $this
        ->assertRaw('destination_rename_diff/' . $renamed_file);
    }
  }

  // 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);
    }
  }

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