diff --git a/feeds.info b/feeds.info index ecb3ae7..280b488 100644 --- a/feeds.info +++ b/feeds.info @@ -79,6 +79,7 @@ files[] = tests/feeds_tokens.test files[] = tests/FeedsEnclosureTest.test files[] = tests/FeedsFetcherResultTest.test files[] = tests/FeedsHTTPCacheTest.test +files[] = tests/FeedsSourceTest.test files[] = tests/http_request.test files[] = tests/parser_csv.test diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc index b5577a1..f77170b 100644 --- a/includes/FeedsSource.inc +++ b/includes/FeedsSource.inc @@ -422,7 +422,7 @@ class FeedsSource extends FeedsConfigurable { if (module_exists('rules')) { rules_invoke_event('feeds_after_import', $this); } - unset($this->fetcher_result, $this->state); + $this->clearStates(); } $this->save(); @@ -478,7 +478,7 @@ class FeedsSource extends FeedsConfigurable { $this->imported = time(); $this->log('import', 'Imported in @s seconds.', array('@s' => $this->imported - $start), WATCHDOG_INFO); - unset($this->fetcher_result, $this->state); + $this->clearStates(); $this->save(); @@ -519,7 +519,7 @@ class FeedsSource extends FeedsConfigurable { $result = $this->progressClearing(); if ($result == FEEDS_BATCH_COMPLETE || isset($e)) { module_invoke_all('feeds_after_clear', $this); - unset($this->state); + $this->clearStates(); } $this->save(); if (isset($e)) { @@ -611,6 +611,14 @@ class FeedsSource extends FeedsConfigurable { } /** + * Clears states. + */ + protected function clearStates() { + $this->states = array(); + $this->fetcher_result = NULL; + } + + /** * Count items imported by this source. */ public function itemCount() { diff --git a/tests/FeedsSourceTest.test b/tests/FeedsSourceTest.test new file mode 100644 index 0000000..fe8ae55 --- /dev/null +++ b/tests/FeedsSourceTest.test @@ -0,0 +1,75 @@ + 'FeedsSource class test', + 'description' => 'Covers class FeedsSource.', + 'group' => 'Feeds', + ); + } + + /** + * Tests if two sources can be imported in the same request. + */ + public function testProgrammaticImport() { + // Create an importer configuration. + $this->createImporterConfiguration('Content CSV', 'csv'); + $this->setSettings('csv', NULL, array( + 'content_type' => '', + 'import_period' => FEEDS_SCHEDULE_NEVER, + )); + $this->setPlugin('csv', 'FeedsCSVParser'); + $this->addMappings('csv', + array( + 0 => array( + 'source' => 'guid', + 'target' => 'guid', + 'unique' => TRUE, + ), + 1 => array( + 'source' => 'title', + 'target' => 'title', + ), + ) + ); + + // Create a source. + $source = feeds_source('csv'); + $source->save(); + + // First source import. + $source->addConfig(array( + 'FeedsHTTPFetcher' => array( + 'source' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv', + ), + )); + + // Perform import. + while (FEEDS_BATCH_COMPLETE != $source->import()); + + // Assert two created nodes. + $this->assertNodeCount(2); + + // Set second source file. + $source->addConfig(array( + 'FeedsHTTPFetcher' => array( + 'source' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/many_nodes_ordered.csv', + ), + )); + + // And perform import again. + while (FEEDS_BATCH_COMPLETE != $source->import()); + + // Assert that there are 86 nodes now. + $this->assertNodeCount(86); + } + +} \ No newline at end of file diff --git a/tests/feeds.test b/tests/feeds.test index c486773..ff8f256 100644 --- a/tests/feeds.test +++ b/tests/feeds.test @@ -370,6 +370,30 @@ class FeedsWebTestCase extends DrupalWebTestCase { } /** + * Asserts that the given number of nodes exist. + * + * @param int $expected_node_count + * The expected number of nodes in the node table. + * @param string $message + * (optional) The message to assert. + */ + protected function assertNodeCount($expected_node_count, $message = '') { + if (!$message) { + $message = '@expected nodes have been created (actual: @count).'; + } + + $node_count = db_select('node') + ->fields('node', array()) + ->countQuery() + ->execute() + ->fetchField(); + $this->assertEqual($expected_node_count, $node_count, format_string($message, array( + '@expected' => $expected_node_count, + '@count' => $node_count, + ))); + } + + /** * Assert a feeds configuration's plugins. * * @deprecated: diff --git a/tests/feeds_fetcher_http.test b/tests/feeds_fetcher_http.test index d532f31..9db926a 100644 --- a/tests/feeds_fetcher_http.test +++ b/tests/feeds_fetcher_http.test @@ -115,30 +115,6 @@ class FeedsFileHTTPTestCase extends FeedsWebTestCase { } /** - * Asserts that the given number of nodes exist. - * - * @param int $expected_node_count - * The expected number of nodes in the node table. - * @param string $message - * (optional) The message to assert. - */ - protected function assertNodeCount($expected_node_count, $message = '') { - if (!$message) { - $message = '@expected nodes have been created (actual: @count).'; - } - - $node_count = db_select('node') - ->fields('node', array()) - ->countQuery() - ->execute() - ->fetchField(); - $this->assertEqual($expected_node_count, $node_count, format_string($message, array( - '@expected' => $expected_node_count, - '@count' => $node_count, - ))); - } - - /** * Test the Feed URL form. */ public function testFormValidation() {