You are here

public function FeedsMapperFileTestCase::test in Feeds 7.2

Basic test loading a single entry CSV file.

File

tests/feeds_mapper_file.test, line 44
Contains FeedsMapperFileTestCase.

Class

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

Code

public function test() {

  // Only download simplepie if the plugin doesn't already exist somewhere.
  // People running tests locally might have it.
  $this
    ->requireSimplePie();
  $typename = $this
    ->createContentType(array(), array(
    'files' => array(
      'type' => 'file',
      'instance_settings' => array(
        'instance[settings][file_extensions]' => 'png, gif, jpg, jpeg',
      ),
    ),
  ));

  // 1) Test mapping remote resources to file field.
  // Create importer configuration.
  $this
    ->createImporterConfiguration();
  $this
    ->setPlugin('syndication', 'FeedsSimplePieParser');
  $this
    ->setSettings('syndication', 'FeedsNodeProcessor', array(
    'bundle' => $typename,
  ));
  $this
    ->addMappings('syndication', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
    1 => array(
      'source' => 'timestamp',
      'target' => 'created',
    ),
    2 => array(
      'source' => 'enclosures',
      'target' => 'field_files:uri',
    ),
  ));
  $nid = $this
    ->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml', 'Test Title');
  $this
    ->assertText('Created 5 nodes');
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'syndication')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertText(str_replace(' ', '_', array_shift($files)));
  }

  // 2) Test mapping local resources to file field.
  // Copy directory of files, CSV file expects them in public://images, point
  // file field to a 'resources' directory. Feeds should copy files from
  // images/ to resources/ on import.
  $this
    ->copyDir($this
    ->absolutePath() . '/tests/feeds/assets', 'public://images');
  $edit = array(
    'instance[settings][file_directory]' => 'resources',
  );
  $this
    ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));

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

  // Import.
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files.csv', array(
      'absolute' => TRUE,
    )),
  );
  $this
    ->drupalPost('import/node', $edit, 'Import');
  $this
    ->assertText('Created 5 nodes');

  // Assert: files should be in resources/.
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'node')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertRaw('resources/' . rawurlencode(array_shift($files)));
  }

  // 3) Test mapping of local resources, this time leave files in place.
  $this
    ->drupalPost('import/node/delete-items', array(), 'Delete');

  // Setting the fields file directory to images will make copying files
  // obsolete.
  $edit = array(
    'instance[settings][file_directory]' => 'images',
  );
  $this
    ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
  $edit = array(
    'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
  );
  $this
    ->drupalPost('import/node', $edit, 'Import');
  $this
    ->assertText('Created 5 nodes');

  // Assert: files should be in images/ now.
  $files = $this
    ->listTestFiles();
  $entities = db_select('feeds_item')
    ->fields('feeds_item', array(
    'entity_id',
  ))
    ->condition('id', 'node')
    ->execute();
  foreach ($entities as $entity) {
    $this
      ->drupalGet('node/' . $entity->entity_id . '/edit');
    $this
      ->assertRaw('images/' . rawurlencode(array_shift($files)));
  }

  // Deleting all imported items will delete the files from the images/ dir.
  $this
    ->drupalPost('import/node/delete-items', array(), 'Delete');
  foreach ($this
    ->listTestFiles() as $file) {
    $this
      ->assertFalse(is_file("public://images/{$file}"));
  }
}