You are here

public function FeedsMapperFileTestCase::testFileExistsRename in Feeds 7.2

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

In this test, files to import should be renamed 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 be renamed to 'foo_0.jpg'.

File

tests/feeds_mapper_file.test, line 177
Contains FeedsMapperFileTestCase.

Class

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

Code

public function testFileExistsRename() {

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

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

  // Copy files with the same names to the destination folder. These files
  // should remain intact, while the files to import should get renamed.
  $this
    ->copyDir($this
    ->absolutePath() . '/tests/feeds/assets', 'public://destination_rename');

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

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

  // Assert: all imported files should be renamed.
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'node_rename')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $f = new FeedsEnclosure(array_shift($files), NULL);
    $renamed_file = str_replace('.jpeg', '_0.jpeg', $f
      ->getUrlEncodedValue());
    $this
      ->assertRaw('destination_rename/' . $renamed_file);
  }

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