From 2ab46bb31c4a68bf5a3741e66d79a7dda5f3bbbb Mon Sep 17 00:00:00 2001
From: Chris Leppanen <chris.leppanen@gmail.com>
Date: Wed, 5 Sep 2012 20:07:27 -0700
Subject: [PATCH] Move path mapping to path.inc and add tests.

---
 feeds.info                     |   1 +
 mappers/path.inc               |  63 ++++++++++++++
 plugins/FeedsNodeProcessor.inc |  24 ------
 plugins/FeedsTermProcessor.inc |  24 ------
 tests/feeds/path_alias.csv     |  10 +++
 tests/feeds_mapper_path.test   | 153 +++++++++++++++++++++++++++++++++
 6 files changed, 227 insertions(+), 48 deletions(-)
 create mode 100644 mappers/path.inc
 create mode 100644 tests/feeds/path_alias.csv
 create mode 100644 tests/feeds_mapper_path.test

diff --git a/feeds.info b/feeds.info
index 22b8456f..c50f680d 100644
--- a/feeds.info
+++ b/feeds.info
@@ -16,6 +16,7 @@ files[] = tests/feeds_date_time.test
 files[] = tests/feeds_mapper_date.test
 files[] = tests/feeds_mapper_field.test
 files[] = tests/feeds_mapper_file.test
+files[] = tests/feeds_mapper_path.test
 files[] = tests/feeds_mapper.test
 files[] = tests/feeds_fetcher_file.test
 files[] = tests/feeds_processor_node.test
diff --git a/mappers/path.inc b/mappers/path.inc
new file mode 100644
index 00000000..d9319d95
--- /dev/null
+++ b/mappers/path.inc
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * @file
+ * On behalf implementation of Feeds mapping API for path.module.
+ */
+
+/**
+ * Implements hook_feeds_processor_targets_alter().
+ *
+ * @see FeedsNodeProcessor::getMappingTargets().
+ */
+function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
+  if (module_exists('path')) {
+
+    switch ($entity_type) {
+      case 'node':
+      case 'taxonomy_term':
+        $targets['path_alias'] = array(
+          'name' => t('Path alias'),
+          'description' => t('URL path alias of the node.'),
+          'callback' => 'path_feeds_set_target',
+        );
+        break;
+    }
+  }
+}
+
+/**
+ * Callback for mapping. Here is where the actual mapping happens.
+ *
+ * When the callback is invoked, $target contains the name of the field the
+ * user has decided to map to and $value contains the value of the feed item
+ * element the user has picked as a source.
+ */
+function path_feeds_set_target($source, $entity, $target, $value) {
+
+  // Path alias cannot be multi-valued, so use the first value.
+  if (is_array($value) && $value) {
+    $value = $value[0];
+  }
+
+  $entity->path = array();
+
+  $entity_type = $source->importer->processor->entityType();
+
+  list($id, , ) = entity_extract_ids($entity_type, $entity);
+
+  if ($id) {
+    $uri = entity_uri($entity_type, $entity);
+
+    // Check for existing aliases.
+    if ($path = path_load($uri['path'])) {
+      $entity->path = $path;
+    }
+  }
+
+  // Prevent Pathauto (http://drupal.org/project/pathauto) from overwriting the 
+  // alias.
+  $entity->path['pathauto'] = FALSE;
+
+  $entity->path['alias'] = ltrim($value, '/');
+}
diff --git a/plugins/FeedsNodeProcessor.inc b/plugins/FeedsNodeProcessor.inc
index 01457e6a..11844c12 100644
--- a/plugins/FeedsNodeProcessor.inc
+++ b/plugins/FeedsNodeProcessor.inc
@@ -219,22 +219,6 @@ class FeedsNodeProcessor extends FeedsProcessor {
           $target_node->feeds['suppress_import'] = TRUE;
         }
         break;
-      case 'path_alias':
-        $target_node->path = array();
-
-        // Check for existing aliases.
-        if (!empty($target_node->nid)) {
-          if ($path = path_load('node/' . $target_node->nid)) {
-            $target_node->path = $path;
-          }
-        }
-
-        // Prevent Pathauto (http://drupal.org/project/pathauto) from
-        // overwriting the alias.
-        $target_node->path['pathauto'] = FALSE;
-
-        $target_node->path['alias'] = $value;
-        break;
       case 'user_name':
         if ($user = user_load_by_name($value)) {
           $target_node->uid = $user->uid;
@@ -314,14 +298,6 @@ class FeedsNodeProcessor extends FeedsProcessor {
       );
     }
 
-    // If path is enabled expose path alias.
-    if (module_exists('path')) {
-      $targets['path_alias'] = array(
-        'name' => t('Path alias'),
-        'description' => t('URL path alias of the node.'),
-      );
-    }
-
     // If the target content type is a Feed node, expose its source field.
     if ($id = feeds_get_importer_id($this->config['content_type'])) {
       $name = feeds_importer($id)->config['name'];
diff --git a/plugins/FeedsTermProcessor.inc b/plugins/FeedsTermProcessor.inc
index 4bad536f..48ee6e59 100644
--- a/plugins/FeedsTermProcessor.inc
+++ b/plugins/FeedsTermProcessor.inc
@@ -166,22 +166,6 @@ class FeedsTermProcessor extends FeedsProcessor {
         }
         $target_term->weight = $weight;
         break;
-      case 'path_alias':
-        $target_term->path = array();
-
-        // Check for existing aliases.
-        if (!empty($target_term->tid)) {
-          if ($path = path_load('taxonomy/term/' . $target_term->tid)) {
-            $target_term->path = $path;
-          }
-        }
-
-        // Prevent Pathauto (http://drupal.org/project/pathauto) from
-        // overwriting the alias.
-        $target_term->path['pathauto'] = FALSE;
-
-        $target_term->path['alias'] = $value;
-        break;
       default:
         parent::setTargetElement($source, $target_term, $target_element, $value);
         break;
@@ -220,14 +204,6 @@ class FeedsTermProcessor extends FeedsProcessor {
       ),
     );
 
-    // If path is enabled expose path alias.
-    if (module_exists('path')) {
-      $targets['path_alias'] = array(
-        'name' => t('Path alias'),
-        'description' => t('URL path alias for the taxonomy term.'),
-      );
-    }
-
     // Let implementers of hook_feeds_term_processor_targets() add their targets.
     try {
       self::loadMappers();
diff --git a/tests/feeds/path_alias.csv b/tests/feeds/path_alias.csv
new file mode 100644
index 00000000..af4ebb95
--- /dev/null
+++ b/tests/feeds/path_alias.csv
@@ -0,0 +1,10 @@
+Title,GUID,path
+"Ut wisi enim ad minim veniam",1,path1
+"Duis autem vel eum iriure dolor",2,path2
+"Nam liber tempor",3,path3
+Typi non habent"",4,path4
+"Lorem ipsum",5,path5
+"Investigationes demonstraverunt",6,path6
+"Claritas est etiam",7,path7
+"Mirum est notare",8,path8
+"Eodem modo typi",9,path9
diff --git a/tests/feeds_mapper_path.test b/tests/feeds_mapper_path.test
new file mode 100644
index 00000000..227e9aeb
--- /dev/null
+++ b/tests/feeds_mapper_path.test
@@ -0,0 +1,153 @@
+<?php
+
+/**
+ * @file
+ * Test case for path alias mapper path.inc.
+ */
+
+/**
+ * Class for testing Feeds <em>path</em> mapper.
+ */
+class FeedsMapperPathTestCase extends FeedsMapperTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Mapper: Path',
+      'description' => 'Test Feeds Mapper support for path aliases.',
+      'group' => 'Feeds',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp(array('path'));
+  }
+
+  /**
+   * Basic test loading a single entry CSV file.
+   */
+  public function _testNodeAlias() {
+
+    // Create importer configuration.
+    $this->createImporterConfiguration($this->randomName(), 'path_test');
+    $this->setPlugin('path_test', 'FeedsFileFetcher');
+    $this->setPlugin('path_test', 'FeedsCSVParser');
+    $this->addMappings('path_test', array(
+      array(
+        'source' => 'Title',
+        'target' => 'title',
+      ),
+      array(
+        'source' => 'path',
+        'target' => 'path_alias',
+      ),
+      array(
+        'source' => 'GUID',
+        'target' => 'guid',
+        'unique' => TRUE,
+      ),
+    ));
+
+    // Turn on update existing.
+    $this->setSettings('path_test', 'FeedsNodeProcessor', array('update_existing' => 2));
+
+    // Import RSS file.
+    $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
+    $this->assertText('Created 9 nodes');
+
+    $aliases = array();
+
+    for ($i = 1; $i <= 9; $i++) {
+      $aliases[] = "path$i";
+    }
+
+    $this->assertAliasCount($aliases);
+
+    // Adding a mapping will force update.
+    $this->addMappings('path_test', array(
+      array(
+        'source' => 'fake',
+        'target' => 'body',
+      ),
+    ));
+    // Import RSS file.
+    $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
+    $this->assertText('Updated 9 nodes');
+
+    // Check that duplicate aliases are not created.
+    $this->assertAliasCount($aliases);
+  }
+
+  /**
+   * Test support for term aliases.
+   */
+  public function testTermAlias() {
+
+    // Create importer configuration.
+    $this->createImporterConfiguration($this->randomName(), 'path_test');
+    $this->setPlugin('path_test', 'FeedsFileFetcher');
+    $this->setPlugin('path_test', 'FeedsCSVParser');
+    $this->setPlugin('path_test', 'FeedsTermProcessor');
+    $this->addMappings('path_test', array(
+      array(
+        'source' => 'Title',
+        'target' => 'name',
+      ),
+      array(
+        'source' => 'path',
+        'target' => 'path_alias',
+      ),
+      array(
+        'source' => 'GUID',
+        'target' => 'guid',
+        'unique' => TRUE,
+      ),
+    ));
+
+    // Turn on update existing.
+    $this->setSettings('path_test', 'FeedsTermProcessor', array('update_existing' => 2));
+
+    // Create vocabulary.
+    $edit = array(
+      'name' => 'Addams vocabulary',
+      'machine_name' => 'addams',
+    );
+    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
+
+    $this->setSettings('path_test', 'FeedsTermProcessor', array('vocabulary' => 'addams'));
+
+    // Import RSS file.
+    $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
+    $this->assertText('Created 9 terms');
+
+    $aliases = array();
+
+    for ($i = 1; $i <= 9; $i++) {
+      $aliases[] = "path$i";
+    }
+
+    $this->assertAliasCount($aliases);
+
+    // Adding a mapping will force update.
+    $this->addMappings('path_test', array(
+      array(
+        'source' => 'fake',
+        'target' => 'description',
+      ),
+    ));
+    // Import RSS file.
+    $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
+    $this->assertText('Updated 9 terms');
+
+    // Check that duplicate aliases are not created.
+    $this->assertAliasCount($aliases);
+  }
+
+  public function assertAliasCount($aliases) {
+    $in_db = db_select('url_alias', 'a')
+      ->fields('a')
+      ->condition('a.alias', $aliases)
+      ->execute()
+      ->fetchAll();
+
+    $this->assertEqual(count($in_db), count($aliases), 'Correct number of aliases in db.');
+  }
+}
-- 
GitLab