From 5549eea7457f4c26b24ba268c3b226a6191738fb Mon Sep 17 00:00:00 2001
From: Will White <will_white@32237.no-reply.drupal.org>
Date: Tue, 30 Mar 2010 18:39:19 +0000
Subject: [PATCH] bug report #740962 by alex_b, ocean_cybrarian: FileFetcher
 Attached to Feed Node, Upload Field Not Saving File Path.

---
 CHANGELOG.txt    |  2 ++
 feeds.module     | 30 ++++++++++++++++++++----------
 tests/feeds.test | 15 ++++++++++++++-
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 45ff899d..01928ae5 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -31,6 +31,8 @@ Feeds 6.x 1.0 xxxxx xx, 2010-xx-xx
   importer id.
 - #718474 jerdavis: In FeedsNodeProcessor, check for duplicate items within
   same importer id.
+- #740962 Fix FileFetcher Attached to Feed Node, Upload Field Not Saving File
+  Path.
 
 Feeds 6.x 1.0 Alpha 12, 2010-02-23
 ----------------------------------
diff --git a/feeds.module b/feeds.module
index 644a962f..e4257e06 100644
--- a/feeds.module
+++ b/feeds.module
@@ -238,9 +238,19 @@ function feeds_feeds_plugins() {
 
 /**
  * Implementation of hook_nodeapi().
+ *
+ * @todo For Drupal 7, revisit static cache based shuttling of values between
+ *   'validate' and 'update'/'insert'.
  */
 function feeds_nodeapi(&$node, $op, $form) {
+
+  // $node looses any changes after 'validate' stage (see node_form_validate()).
+  // Keep a copy of title and feeds array between 'validate' and subsequent
+  // stages. This allows for automatically populating the title of the node form
+  // and modifying the $form['feeds'] array on node validation just like on the
+  // standalone form.
   static $last_title;
+  static $node_feeds;
 
   // Break out node processor related nodeapi functionality.
   _feeds_nodeapi_node_processor($node, $op);
@@ -253,16 +263,17 @@ function feeds_nodeapi(&$node, $op, $form) {
         // this stage.
         $source = feeds_source($importer_id);
 
+        // Node module magically moved $form['feeds'] to $node->feeds :P
+        $node_feeds = $node->feeds;
+        $source->configFormValidate($node_feeds);
+
         // If node title is empty, try to retrieve title from feed.
         if (trim($node->title) == '') {
           try {
-            $source->addConfig($node->feeds);
+            $source->addConfig($node_feeds);
             // @todo Too many indirections. Clean up.
             $batch = $source->importer->fetcher->fetch($source);
             $source->importer->parser->parse($batch, $source);
-            // Keep the title in a static cache and populate $node->title on
-            // 'presave' as node module looses any changes to $node after
-            // 'validate'.
             if (!$last_title = $batch->getTitle()) {
               throw new Exception();
             }
@@ -272,10 +283,6 @@ function feeds_nodeapi(&$node, $op, $form) {
             form_set_error('title', t('Could not retrieve title from feed.'), 'error');
           }
         }
-
-        // Invoke source
-        // Node module magically moved $form['feeds'] to $node->feeds :P
-        $source->configFormValidate($node->feeds);
         break;
       case 'presave':
         if (!empty($last_title)) {
@@ -287,12 +294,12 @@ function feeds_nodeapi(&$node, $op, $form) {
       case 'update':
         // Add configuration to feed source and save.
         $source = feeds_source($importer_id, $node->nid);
-        $source->addConfig($node->feeds);
+        $source->addConfig($node_feeds);
         $source->save();
 
         // Refresh feed if import on create is selected and suppress_import is
         // not set.
-        if ($op == 'insert' && feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
+        if ($op == 'insert' && feeds_importer($importer_id)->config['import_on_create'] && !isset($node_feeds['suppress_import'])) {
           feeds_batch_set(t('Importing'), 'import', $importer_id, $node->nid);
         }
         // Add import to scheduler.
@@ -300,6 +307,7 @@ function feeds_nodeapi(&$node, $op, $form) {
         // Add expiry to schedule, in case this is the first feed of this
         // configuration.
         feeds_scheduler()->add($importer_id, 'expire');
+        unset($node_feeds);
         break;
       case 'delete':
         // Remove feed from scheduler and delete source.
@@ -348,6 +356,8 @@ function feeds_form_alter(&$form, $form_state, $form_id) {
     if ($importer_id = feeds_get_importer_id($form['type']['#value'])) {
       // Set title to not required, try to retrieve it from feed.
       $form['title']['#required'] = FALSE;
+      // Enable uploads.
+      $form['#attributes']['enctype'] = 'multipart/form-data';
 
       // Build form.
       $source = feeds_source($importer_id, empty($form['nid']['#value']) ? 0 : $form['nid']['#value']);
diff --git a/tests/feeds.test b/tests/feeds.test
index 6b0ca720..dcbc5b1d 100644
--- a/tests/feeds.test
+++ b/tests/feeds.test
@@ -20,7 +20,7 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase {
   public function getInfo() {
     return array(
       'name' => t('RSS import to nodes'),
-      'description' => t('Tests a feed configuration that is attached to a content type, uses common syndication parser and a node processor. Repeats the same test for a feed configuration that is not attached to a content type.'),
+      'description' => t('Tests a feed configuration that is attached to a content type, uses HTTP fetcher, common syndication parser and a node processor. Repeats the same test for an importer configuration that is not attached to a content type and for a configuration that is attached to a content type and uses the file fetcher.'),
       'group' => t('Feeds'),
     );
   }
@@ -321,6 +321,19 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase {
     // Navigate to feed import form, access should be denied.
     $this->drupalGet('import/syndication_standalone');
     $this->assertResponse(403);
+
+    // Use File Fetcher.
+    $this->drupalLogin(
+      $this->drupalCreateUser(array('administer feeds', 'administer nodes'))
+    );
+    $this->setPlugin('syndication', 'FeedsFileFetcher');
+    // Create a feed node.
+    $edit = array(
+      'files[feeds]' => $this->absolutePath() .'/tests/feeds/drupalplanet.rss2',
+    );
+    $this->drupalPost('node/add/page', $edit, 'Save');
+    $this->assertText('has been created.');
+    $this->assertText('Created 25 Story nodes.');
   }
 }
 
-- 
GitLab