You are here

public function FeedsMapperFileTestCase::testFileExistsReplace in Feeds 7.2

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

In this test, files to import should be replaced if files with the same name already exist in the destination folder. Example: on the destination folder there exist a file named 'foo.jpg'. When importing a file with the same name, that file should replace the existing 'foo.jpg'.

File

tests/feeds_mapper_file.test, line 239
Contains FeedsMapperFileTestCase.

Class

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

Code

public function testFileExistsReplace() {
  $source = 'public://images';
  $dest = 'public://destination_replace';

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

  // Copy files with the same name to the destination folder, but make sure
  // that the files are different by shuffling the file names. These files
  // should get overwritten upon import.
  $this
    ->copyDir($this
    ->absolutePath() . '/tests/feeds/assets', $dest, $this
    ->listTestFilesNameMap());

  // Confirm the files from the source folder are all different from the
  // destination folder.
  foreach (@scandir($source) 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', 'node_replace');
  $this
    ->setSettings('node_replace', NULL, array(
    'content_type' => '',
  ));
  $this
    ->setPlugin('node_replace', 'FeedsCSVParser');
  $this
    ->setSettings('node_replace', 'FeedsNodeProcessor', array(
    'bundle' => $typename,
  ));
  $this
    ->addMappings('node_replace', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    1 => array(
      'source' => 'file',
      'target' => 'field_files:uri',
      'file_exists' => FILE_EXISTS_REPLACE,
    ),
  ));

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

  // Assert: all files in the destination folder should be exactly the same as
  // the files in the source folder.
  foreach (@scandir($source) 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);
    }
  }

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