diff --git a/feeds.info b/feeds.info index 22b8456fa8fa20c82d87ad0ba80a8ec188d41de4..c50f680d199865106664569784655b21d88964ca 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 0000000000000000000000000000000000000000..d9319d959e415452c6bb62c7652c76c90b544a8d --- /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 01457e6a01463a00064b06c111f78c259a57e44d..11844c1267407910055a3f2b41d8b79f6481f89d 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 4bad536ff38ab84cf444372f81231d3fb5d0effb..48ee6e592c6ecfbcb6ca816e23ddc7055528a95c 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 0000000000000000000000000000000000000000..af4ebb95d03d2fe3b297d1b6af1e0520c3abed56 --- /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 0000000000000000000000000000000000000000..227e9aebbc0d50a539931e7cd839dfaa07a4a768 --- /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.'); + } +}