feeds_mapper_file.test 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Test case for Filefield mapper mappers/filefield.inc.
  5. */
  6. /**
  7. * Class for testing Feeds file mapper.
  8. *
  9. * @todo Add a test for enclosures using a local file that is
  10. * a) in place and that
  11. * b) needs to be copied from one location to another.
  12. */
  13. class FeedsMapperFileTestCase extends FeedsMapperTestCase {
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Mapper: File field',
  17. 'description' => 'Test Feeds Mapper support for file fields. <strong>Requires SimplePie library</strong>.',
  18. 'group' => 'Feeds',
  19. );
  20. }
  21. /**
  22. * Basic test loading a single entry CSV file.
  23. */
  24. public function test() {
  25. // If this is unset (or FALSE) http_request.inc will use curl, and will generate a 404
  26. // for this feel url provided by feeds_tests. However, if feeds_tests was enabled in your
  27. // site before running the test, it will work fine. Since it is truly screwy, lets just
  28. // force it to use drupal_http_request for this test case.
  29. variable_set('feeds_never_use_curl', TRUE);
  30. variable_set('clean_url', TRUE);
  31. // Only download simplepie if the plugin doesn't already exist somewhere.
  32. // People running tests locally might have it.
  33. if (!feeds_simplepie_exists()) {
  34. $this->downloadExtractSimplePie('1.3');
  35. $this->assertTrue(feeds_simplepie_exists());
  36. // Reset all the caches!
  37. $this->resetAll();
  38. }
  39. $typename = $this->createContentType(array(), array('files' => 'file'));
  40. // 1) Test mapping remote resources to file field.
  41. // Create importer configuration.
  42. $this->createImporterConfiguration();
  43. $this->setPlugin('syndication', 'FeedsSimplePieParser');
  44. $this->setSettings('syndication', 'FeedsNodeProcessor', array('content_type' => $typename));
  45. $this->addMappings('syndication', array(
  46. 0 => array(
  47. 'source' => 'title',
  48. 'target' => 'title'
  49. ),
  50. 1 => array(
  51. 'source' => 'timestamp',
  52. 'target' => 'created'
  53. ),
  54. 2 => array(
  55. 'source' => 'enclosures',
  56. 'target' => 'field_files'
  57. ),
  58. ));
  59. $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml');
  60. $this->assertText('Created 5 nodes');
  61. $files = $this->_testFiles();
  62. $entities = db_select('feeds_item')
  63. ->fields('feeds_item', array('entity_id'))
  64. ->condition('id', 'syndication')
  65. ->execute();
  66. foreach ($entities as $entity) {
  67. $this->drupalGet('node/' . $entity->entity_id . '/edit');
  68. $f = new FeedsEnclosure(array_shift($files), NULL);
  69. $this->assertText($f->getLocalValue());
  70. }
  71. // 2) Test mapping local resources to file field.
  72. // Copy directory of files, CSV file expects them in public://images, point
  73. // file field to a 'resources' directory. Feeds should copy files from
  74. // images/ to resources/ on import.
  75. $this->copyDir($this->absolutePath() . '/tests/feeds/assets', 'public://images');
  76. $edit = array(
  77. 'instance[settings][file_directory]' => 'resources',
  78. );
  79. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
  80. // Create a CSV importer configuration.
  81. $this->createImporterConfiguration('Node import from CSV', 'node');
  82. $this->setPlugin('node', 'FeedsCSVParser');
  83. $this->setSettings('node', 'FeedsNodeProcessor', array('content_type' => $typename));
  84. $this->addMappings('node', array(
  85. 0 => array(
  86. 'source' => 'title',
  87. 'target' => 'title'
  88. ),
  89. 1 => array(
  90. 'source' => 'file',
  91. 'target' => 'field_files'
  92. ),
  93. ));
  94. $edit = array(
  95. 'content_type' => '',
  96. );
  97. $this->drupalPost('admin/structure/feeds/node/settings', $edit, 'Save');
  98. // Import.
  99. $edit = array(
  100. 'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
  101. );
  102. $this->drupalPost('import/node', $edit, 'Import');
  103. $this->assertText('Created 5 nodes');
  104. // Assert: files should be in resources/.
  105. $files = $this->_testFiles();
  106. $entities = db_select('feeds_item')
  107. ->fields('feeds_item', array('entity_id'))
  108. ->condition('id', 'node')
  109. ->execute();
  110. foreach ($entities as $entity) {
  111. $this->drupalGet('node/' . $entity->entity_id . '/edit');
  112. $f = new FeedsEnclosure(array_shift($files), NULL);
  113. $this->assertRaw('resources/' . $f->getUrlEncodedValue());
  114. }
  115. // 3) Test mapping of local resources, this time leave files in place.
  116. $this->drupalPost('import/node/delete-items', array(), 'Delete');
  117. // Setting the fields file directory to images will make copying files
  118. // obsolete.
  119. $edit = array(
  120. 'instance[settings][file_directory]' => 'images',
  121. );
  122. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
  123. $edit = array(
  124. 'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
  125. );
  126. $this->drupalPost('import/node', $edit, 'Import');
  127. $this->assertText('Created 5 nodes');
  128. // Assert: files should be in images/ now.
  129. $files = $this->_testFiles();
  130. $entities = db_select('feeds_item')
  131. ->fields('feeds_item', array('entity_id'))
  132. ->condition('id', 'node')
  133. ->execute();
  134. foreach ($entities as $entity) {
  135. $this->drupalGet('node/' . $entity->entity_id . '/edit');
  136. $f = new FeedsEnclosure(array_shift($files), NULL);
  137. $this->assertRaw('images/' . $f->getUrlEncodedValue());
  138. }
  139. // Deleting all imported items will delete the files from the images/ dir.
  140. // @todo: for some reason the first file does not get deleted.
  141. // $this->drupalPost('import/node/delete-items', array(), 'Delete');
  142. // foreach ($this->_testFiles() as $file) {
  143. // $this->assertFalse(is_file("public://images/$file"));
  144. // }
  145. }
  146. /**
  147. * Lists test files.
  148. */
  149. public function _testFiles() {
  150. return array('tubing.jpeg', 'foosball.jpeg', 'attersee.jpeg', 'hstreet.jpeg', 'la fayette.jpeg');
  151. }
  152. /**
  153. * Handle file field widgets.
  154. */
  155. public function selectFieldWidget($fied_name, $field_type) {
  156. if ($field_type == 'file') {
  157. return 'file_generic';
  158. }
  159. else {
  160. return parent::selectFieldWidget($fied_name, $field_type);
  161. }
  162. }
  163. }