From da83212bb7cd7fd8eaeebae9432bfb789713f702 Mon Sep 17 00:00:00 2001
From: megachriz <megachriz@654114.no-reply.drupal.org>
Date: Sun, 13 Nov 2016 14:34:47 +0100
Subject: [PATCH] Issue #1886230 by MegaChriz, generalredneck: fixed invoking
 rules events "feeds_before_import" and "feeds_after_import".

---
 feeds.info                  |   1 +
 includes/FeedsSource.inc    |   6 ++
 tests/feeds_rules.test      | 119 ++++++++++++++++++++++++++++++++++++
 tests/feeds_tests.rules.inc |  30 +++++++++
 4 files changed, 156 insertions(+)
 create mode 100644 tests/feeds_rules.test
 create mode 100644 tests/feeds_tests.rules.inc

diff --git a/feeds.info b/feeds.info
index 31e2462e..4991435f 100644
--- a/feeds.info
+++ b/feeds.info
@@ -67,6 +67,7 @@ files[] = tests/feeds_processor_node.test
 files[] = tests/feeds_processor_term.test
 files[] = tests/feeds_processor_user.test
 files[] = tests/feeds_push.test
+files[] = tests/feeds_rules.test
 files[] = tests/feeds_scheduler.test
 files[] = tests/feeds_mapper_link.test
 files[] = tests/feeds_mapper_summary.test
diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc
index d558e0ba..b5577a15 100644
--- a/includes/FeedsSource.inc
+++ b/includes/FeedsSource.inc
@@ -381,6 +381,9 @@ class FeedsSource extends FeedsConfigurable {
       // If fetcher result is empty, we are starting a new import, log.
       if (empty($this->fetcher_result)) {
         module_invoke_all('feeds_before_import', $this);
+        if (module_exists('rules')) {
+          rules_invoke_event('feeds_before_import', $this);
+        }
         $this->state[FEEDS_START] = time();
       }
 
@@ -416,6 +419,9 @@ class FeedsSource extends FeedsConfigurable {
       $this->log('import', 'Imported in @s seconds.', array('@s' => $this->imported - $this->state[FEEDS_START]), WATCHDOG_INFO);
       $this->importer->fetcher->afterImport($this);
       module_invoke_all('feeds_after_import', $this);
+      if (module_exists('rules')) {
+        rules_invoke_event('feeds_after_import', $this);
+      }
       unset($this->fetcher_result, $this->state);
     }
     $this->save();
diff --git a/tests/feeds_rules.test b/tests/feeds_rules.test
new file mode 100644
index 00000000..84e2ebdf
--- /dev/null
+++ b/tests/feeds_rules.test
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * Tests for Rules integration.
+ */
+class FeedsRulesTest extends FeedsWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Rules integration',
+      'description' => 'Tests for Rules integration.',
+      'group' => 'Feeds',
+      'dependencies' => array('rules'),
+    );
+  }
+
+  /**
+   * Set up test.
+   */
+  public function setUp() {
+    parent::setUp(array('rules'));
+
+    // Create an importer configuration.
+    $this->createImporterConfiguration('Node import', 'node');
+    $this->setSettings('node', NULL, array('content_type' => ''));
+    $this->setPlugin('node', 'FeedsHTTPFetcher');
+    $this->setPlugin('node', 'FeedsCSVParser');
+    $this->addMappings('node',
+      array(
+        0 => array(
+          'source' => 'title',
+          'target' => 'title',
+          'unique' => FALSE,
+        ),
+        1 => array(
+          'source' => 'guid',
+          'target' => 'guid',
+          'unique' => TRUE,
+        ),
+      )
+    );
+  }
+
+  /**
+   * Creates a test rule.
+   *
+   * @param string $event
+   *   The event to react on.
+   * @param bool $action
+   *   If a dummy action should be executed.
+   *
+   * @return RulesReactionRule
+   *   An instance of RulesReactionRule.
+   */
+  protected function createTestRule($event, $action = TRUE) {
+    $rule = rules_reaction_rule();
+    $rule->event($event)
+      ->condition('data_is', array(
+        'data:select' => 'source:id',
+        'value' => 'node'
+      ));
+    if ($action) {
+      $rule->action('feeds_tests_create_node');
+    }
+    return $rule;
+  }
+
+  /**
+   * Tests if the Rules event 'feeds_before_import' is invoked.
+   */
+  public function testFeedsBeforeImportEvent() {
+    $rule = $this->createTestRule('feeds_before_import');
+    $rule->integrityCheck()->save();
+
+    // Set source file to import.
+    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
+    $edit = array(
+      'feeds[FeedsHTTPFetcher][source]' => $source_url,
+    );
+    $this->drupalPost('import/node', $edit, t('Import'));
+    $this->assertText('Created 2 nodes');
+
+    // Assert that a test node was created *before* the import.
+    $node = node_load(1);
+    $this->assertEqual('Test node', $node->title);
+
+    // Assert titles of imported nodes as well.
+    $node = node_load(2);
+    $this->assertEqual('Lorem ipsum', $node->title);
+    $node = node_load(3);
+    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
+  }
+
+  /**
+   * Tests if the Rules event 'feeds_after_import' is invoked.
+   */
+  public function testFeedsAfterImportEvent() {
+    $rule = $this->createTestRule('feeds_after_import');
+    $rule->integrityCheck()->save();
+
+    // Set source file to import.
+    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
+    $edit = array(
+      'feeds[FeedsHTTPFetcher][source]' => $source_url,
+    );
+    $this->drupalPost('import/node', $edit, t('Import'));
+    $this->assertText('Created 2 nodes');
+
+    // Assert that a test node was created *after* the import.
+    $node = node_load(3);
+    $this->assertEqual('Test node', $node->title);
+
+    // Assert titles of imported nodes as well.
+    $node = node_load(1);
+    $this->assertEqual('Lorem ipsum', $node->title);
+    $node = node_load(2);
+    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
+  }
+}
diff --git a/tests/feeds_tests.rules.inc b/tests/feeds_tests.rules.inc
new file mode 100644
index 00000000..ca3cf4e1
--- /dev/null
+++ b/tests/feeds_tests.rules.inc
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * @file
+ * Includes any rules integration provided by the module.
+ */
+
+/**
+ * Implements hook_rules_action_info().
+ */
+function feeds_tests_rules_action_info() {
+  $items['feeds_tests_create_node'] = array(
+    'label' => t('Create a node'),
+    'group' => t('Rules test'),
+  );
+  return $items;
+}
+
+/**
+ * Rules action callback: creates a node.
+ */
+function feeds_tests_create_node() {
+  $node = new stdClass();
+  $node->title = 'Test node';
+  $node->type = 'page';
+  $node->status = 1;
+  $node->uid = 0;
+  node_object_prepare($node);
+  node_save($node);
+}
-- 
GitLab