diff --git a/tests/feeds_fetcher_http.test b/tests/feeds_fetcher_http.test index d532f31..0db782f 100644 --- a/tests/feeds_fetcher_http.test +++ b/tests/feeds_fetcher_http.test @@ -561,6 +561,120 @@ class FeedsFileHTTPTestCase extends FeedsWebTestCase { } /** + * Tests that the temporary file is not removed when the import has not + * completed yet. + * + * When an import takes multiple cron runs to complete, a file is saved in the + * feeds 'in progress' directory. This file should remain there when it is + * known that the Feeds import has not yet completed. + */ + public function testTemporaryFileNoDelete() { + $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/many_nodes_ordered.csv'; + $this->setUpMultipleCronRuns($source_url); + + // Run the first cron. + $this->cronRun(); + + // Assert that five nodes have been created now. + $this->assertNodeCount(5); + + // Assert that a file exists in the in_progress dir. + $file = $this->getInProgressFile(); + + // Change the created time of all managed files to be 24 hours in the past. + // Drupal deletes temporary files after six hours. + db_update('file_managed') + ->fields(array( + 'timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE - 1, + )) + ->execute(); + + // Run cron again. + $this->cronRun(); + + // Assert that 10 nodes have been created in total. + $this->assertNodeCount(10); + + // Assert that the file still exist as the import has not completed yet. + $this->assertTrue(file_exists($file->uri), format_string('The file @uri exists.', array( + '@uri' => $file->uri, + ))); + } + + /** + * Tests that the temporary file gets cleaned up after the import has + * completed. + */ + public function testTemporaryFileCleanUpAfterImport() { + $source_url = url('testing/feeds/nodes.csv', array('absolute' => TRUE)); + $this->setUpMultipleCronRuns($source_url); + + // Run the first cron. + $this->cronRun(); + + // Assert that a file exists in the in_progress dir. + $file = $this->getInProgressFile(); + + // Change the created time of all managed files to be 24 hours in the past. + // Drupal deletes temporary files after six hours. + db_update('file_managed') + ->fields(array('timestamp' => REQUEST_TIME - 86400)) + ->execute(); + + // Run cron again twice. First one to complete the import, second one to + // ensure that the file is cleaned up. + $this->cronRun(); + $this->cronRun(); + + // Assert that 9 nodes have been created in total. + $this->assertNodeCount(9); + + // Assert that the temporary file no longer exists. + $this->assertFalse(file_exists($file->uri), format_string('The file @uri no longer exists.', array( + '@uri' => $file->uri, + ))); + + $this->getInProgressFile(); + } + + /** + * Tests that the temporary file gets cleaned up if the import gets aborted. + */ + public function testTemporaryFileCleanUpWhenAbortingImport() { + $source_url = url('testing/feeds/nodes.csv', array('absolute' => TRUE)); + $this->setUpMultipleCronRuns($source_url); + + // Run the first cron. + $this->cronRun(); + + // Assert that a file exists in the in_progress dir. + $file = $this->getInProgressFile(); + + // Change the created time of all managed files to be 24 hours in the past. + // Drupal deletes temporary files after six hours. + db_update('file_managed') + ->fields(array('timestamp' => REQUEST_TIME - 86400)) + ->execute(); + + // Abort the import. + db_update('feeds_source') + ->condition('id', 'node') + ->fields(array('state' => FALSE)) + ->execute(); + + // Run cron again. + $this->cronRun(); + + // Assert that only 5 nodes have been created, since the import got aborted. + $this->assertNodeCount(5); + + // Assert that the temporary file no longer exists. + $this->assertFalse(file_exists($file->uri), format_string('The file @uri no longer exists.', array( + '@uri' => $file->uri, + ))); + } + + /** * Tests that FeedsHTTPFetcherResult::getRaw() always returns the same result * for the same instance, even when caches are cleared in between. *