Skip to content
Snippets Groups Projects
Commit 21e3fc53 authored by megachriz's avatar megachriz Committed by Megachriz
Browse files

Issue #2446307 by MegaChriz: Added rules feeds import action.

parent 2ae64219
No related branches found
No related tags found
No related merge requests found
......@@ -60,6 +60,26 @@ function feeds_rules_event_info() {
*/
function feeds_rules_action_info() {
return array(
'feeds_import_feed' => array(
'base' => 'feeds_action_import_feed',
'label' => t('Execute feeds importer'),
'parameter' => array(
'importer' => array(
'type' => 'text',
'label' => t('Feeds importer'),
'options list' => 'feeds_importer_list',
'default mode' => 'input',
),
'feed_nid' => array(
'type' => 'node',
'label' => t('Feed node'),
'default mode' => 'input',
'description' => t("The feed node, if the importer is attached to a content type. Put in '0' if the importer is not attached to a content type."),
),
),
'group' => t('Feeds'),
'access callback' => 'feeds_rules_access_callback',
),
'feeds_skip_item' => array(
'base' => 'feeds_action_skip_item',
'label' => t('Skip import of feeds item'),
......@@ -98,6 +118,37 @@ function feeds_rules_data_info() {
);
}
/**
* Rules action callback for "feeds_import_feed" action.
*
* @param string $importer_id
* ID of the importer.
* @param object|null $feed_node
* The feed node, if found. Null otherwise.
* @param array $params
* The raw parameters.
*/
function feeds_action_import_feed($importer_id, $feed_node, array $params) {
$source = feeds_source($importer_id, $params['feed_nid']);
try {
$source->existing()->startImport();
// Execute batch, if there is any. Set 'progressive' to false to prevent
// batch from triggering a drupal_goto().
$batch =& batch_get();
if (!empty($batch)) {
$batch['progressive'] = FALSE;
batch_process();
}
}
catch (FeedsNotExistingException $e) {
// Ignore this kind of exception.
}
catch (Exception $e) {
$source->log('import', $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
/**
* Mark feeds import item as skipped.
*/
......@@ -115,6 +166,21 @@ function feeds_action_skip_item_help() {
return t("This action allows skipping certain feed items during feeds processing, i.e. before an imported item is saved. Once this action is used on a item, the changes to the entity of the feed item are not saved.");
}
/**
* List callback for selecting a Feeds importer.
*/
function feeds_importer_list() {
$list = array();
ctools_include('export');
$configs = ctools_export_load_object('feeds_importer', 'all');
foreach ($configs as $id => $config) {
if (empty($config->disabled)) {
$list[$id] = $config->config['name'];
}
}
return $list;
}
/**
* Access callback for the feeds rules integration.
*/
......
<?php
/**
* @file
* Contains FeedsRulesTest.
*/
/**
* Tests for Rules integration.
*/
class FeedsRulesTest extends FeedsWebTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Rules integration',
......@@ -182,4 +190,106 @@ class FeedsRulesTest extends FeedsWebTestCase {
$node = node_load(1);
$this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
}
/**
* Tests the Rules action 'feeds_import_feed'.
*/
public function testFeedsImportAction() {
// Attach importer to content type and set to not import on submission.
$this->setSettings('node', NULL, array(
'content_type' => 'page',
'import_period' => FEEDS_SCHEDULE_NEVER,
'import_on_create' => FALSE,
));
// Create a feed node.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$this->createFeedNode('node', $source_url, 'Feed node 1', 'page');
// Assert that nothing has been imported yet.
$this->assertNodeCount(1);
// Create rule with import action.
$rule = $this->createTestRule('cron', FALSE);
$rule->action('feeds_import_feed', array(
'importer' => 'node',
'feed_nid' => 1,
));
$rule->integrityCheck()->save();
// Trigger rules event.
$this->cronRun();
// Assert that 2 items have been imported (three nodes exist in total).
$this->assertNodeCount(3);
}
/**
* Tests the Rules action 'feeds_import_feed' with standalone form.
*/
public function testFeedsImportActionUsingStandaloneForm() {
$this->setSettings('node', NULL, array(
'import_period' => FEEDS_SCHEDULE_NEVER,
'import_on_create' => FALSE,
));
// Save data on import form.
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv',
);
$this->drupalPost('import/node', $edit, 'Save');
// Create rule with import action.
$rule = $this->createTestRule('cron', FALSE);
$rule->action('feeds_import_feed', array(
'importer' => 'node',
'feed_nid' => 0,
));
$rule->integrityCheck()->save();
// Trigger rules event.
$this->cronRun();
// Assert that 2 items have been imported.
$this->assertNodeCount(2);
}
/**
* Tests the Rules action 'feeds_import_feed' with process in background.
*/
public function testFeedsImportActionWithProcessInBackgroundOption() {
// Attach importer to content type, set to not import on submission and set
// to run in background.
$this->setSettings('node', NULL, array(
'content_type' => 'page',
'import_period' => FEEDS_SCHEDULE_NEVER,
'import_on_create' => FALSE,
'process_in_background' => TRUE,
));
// Create a feed node.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$this->createFeedNode('node', $source_url, 'Feed node 1', 'page');
// Assert that nothing has been imported yet.
$this->assertNodeCount(1);
// Create rule with import action.
$rule = $this->createTestRule('feeds_tests_rules_event', FALSE);
$rule->action('feeds_import_feed', array(
'importer' => 'node',
'feed_nid' => 1,
));
$rule->integrityCheck()->save();
// Trigger rules event.
$this->drupalGet('testing/feeds/trigger-rules-event');
// Run cron to run background task.
$this->cronRun();
// Assert that 2 items have been imported (three nodes exist in total).
$this->assertNodeCount(3);
}
}
......@@ -39,6 +39,13 @@ function feeds_tests_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
if (module_exists('rules')) {
$items['testing/feeds/trigger-rules-event'] = array(
'page callback' => 'feeds_tests_trigger_rules_event',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
......@@ -347,6 +354,16 @@ function feeds_tests_query_term_access_alter(QueryAlterableInterface $query) {
}
}
/**
* Page callback. Triggers the rules event 'feeds_tests_rules_event'.
*/
function feeds_tests_trigger_rules_event() {
rules_invoke_event('feeds_tests_rules_event');
return array(
'#markup' => 'Rules event "feeds_tests_rules_event" triggered.',
);
}
/**
* Implements hook_feeds_processor_targets().
*/
......@@ -551,7 +568,7 @@ function feeds_tests_feeds_after_parse(FeedsSource $source, FeedsParserResult $r
// items in there with encoding issues. These items can not be processed
// during tests without having a test failure because in < PHP 5.4 that would
// produce the following warning:
// htmlspecialchars(): Invalid multibyte sequence in argument
// > htmlspecialchars(): Invalid multibyte sequence in argument
// @see FeedsCSVParserTestCase::testMbstringExtensionDisabled()
if (variable_get('feeds_tests_feeds_after_parse_empty_items', FALSE)) {
// Remove all items. No items will be processed.
......@@ -617,11 +634,11 @@ function feeds_tests_feeds_after_save() {
*/
class FeedsTestsPreprocess {
/**
* Preprocess callback for test_target.
*
* @see feeds_tests_feeds_processor_targets()
*/
/**
* Preprocess callback for test_target.
*
* @see feeds_tests_feeds_processor_targets()
*/
public static function callback(array $target, array &$mapping) {
$mapping['required_value'] = TRUE;
}
......
......@@ -5,6 +5,18 @@
* Includes any rules integration provided by the module.
*/
/**
* Implements hook_rules_event_info().
*/
function feeds_tests_rules_event_info() {
return array(
'feeds_tests_rules_event' => array(
'label' => t('Test event'),
'group' => t('Feeds'),
),
);
}
/**
* Implements hook_rules_action_info().
*/
......
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