Skip to content
Snippets Groups Projects
Commit 2ab46bb3 authored by Chris Leppanen's avatar Chris Leppanen
Browse files

Move path mapping to path.inc and add tests.

parent dd115f88
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
<?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, '/');
}
......@@ -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'];
......
......@@ -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();
......
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
<?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.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment