Skip to content
Snippets Groups Projects
Commit b35a9fee authored by drothstein's avatar drothstein Committed by MegaChriz
Browse files

Issue #1143280 by David_Rothstein, MegaChriz, dshields, Matt V., marktheshark:...

Issue #1143280 by David_Rothstein, MegaChriz, dshields, Matt V., marktheshark: Provide an option to delete the uploaded file after import is done.
parent 6b5a7ea4
No related branches found
No related tags found
No related merge requests found
......@@ -414,6 +414,7 @@ class FeedsSource extends FeedsConfigurable {
if ($result == FEEDS_BATCH_COMPLETE || isset($e)) {
$this->imported = time();
$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);
unset($this->fetcher_result, $this->state);
}
......
......@@ -222,4 +222,12 @@ abstract class FeedsFetcher extends FeedsPlugin {
* $source, NULL otherwise.
*/
public function importPeriod(FeedsSource $source) {}
/**
* Invoked after an import is finished.
*
* @param $source
* A FeedsSource object.
*/
public function afterImport(FeedsSource $source) {}
}
......@@ -213,6 +213,7 @@ class FeedsFileFetcher extends FeedsFetcher {
return array(
'allowed_extensions' => 'txt csv tsv xml opml',
'delete_uploaded_file' => FALSE,
'direct' => FALSE,
'directory' => $scheme . '://feeds',
'allowed_schemes' => $schemes,
......@@ -230,6 +231,19 @@ class FeedsFileFetcher extends FeedsFetcher {
'#description' => t('Allowed file extensions for upload.'),
'#default_value' => $this->config['allowed_extensions'],
);
$form['delete_uploaded_file'] = array(
'#type' => 'checkbox',
'#title' => t('Immediately delete uploaded file after import'),
'#description' => t('Useful if the file contains private information. If not selected, the file will remain on the server, allowing the import to be re-run without having to upload it again. This setting has no effect when the option "@direct" is enabled.', array(
'@direct' => t('Supply path to file or directory directly'),
)),
'#default_value' => $this->config['delete_uploaded_file'],
'#states' => array(
'disabled' => array(
':input[name="direct"]' => array('checked' => TRUE),
),
),
);
$form['direct'] = array(
'#type' => 'checkbox',
'#title' => t('Supply path to file or directory directly'),
......@@ -300,6 +314,20 @@ class FeedsFileFetcher extends FeedsFetcher {
}
}
/**
* Overrides FeedsFetcher::afterImport().
*/
public function afterImport(FeedsSource $source) {
// Immediately delete the file after import, if requested.
if (!empty($this->config['delete_uploaded_file'])) {
$source_config = $source->getConfigFor($this);
if (!empty($source_config['fid'])) {
$this->deleteFile($source_config['fid'], $source->feed_nid);
$source->setConfigFor($this, $this->sourceDefaults());
}
}
}
/**
* Deletes a file.
*
......
......@@ -18,24 +18,31 @@ class FeedsFileFetcherTestCase extends FeedsWebTestCase {
}
/**
* Test scheduling on cron.
* {@inheritdoc}
*/
public function testPublicFiles() {
public function setUp() {
parent::setUp();
// Set up an importer.
$this->createImporterConfiguration('Node import', 'node');
// Set and configure plugins and mappings.
$this->setSettings('node', NULL, array('content_type' => ''));
$this->setPlugin('node', 'FeedsFileFetcher');
$this->setPlugin('node', 'FeedsCSVParser');
$mappings = array(
$this->addMappings('node', array(
'0' => array(
'source' => 'title',
'target' => 'title',
),
);
$this->addMappings('node', $mappings);
// Straight up upload is covered in other tests, focus on direct mode
// and file batching here.
));
}
/**
* Test scheduling on cron.
*/
public function testPublicFiles() {
// Straight up upload is covered in other tests, focus on direct mode and
// file batching here.
$settings = array(
'direct' => TRUE,
'directory' => 'public://feeds',
......@@ -72,24 +79,8 @@ class FeedsFileFetcherTestCase extends FeedsWebTestCase {
* Test uploading private files.
*/
public function testPrivateFiles() {
// Set up an importer.
$this->createImporterConfiguration('Node import', 'node');
// Set and configure plugins and mappings.
$edit = array(
'content_type' => '',
);
$this->drupalPost('admin/structure/feeds/node/settings', $edit, 'Save');
$this->setPlugin('node', 'FeedsFileFetcher');
$this->setPlugin('node', 'FeedsCSVParser');
$mappings = array(
'0' => array(
'source' => 'title',
'target' => 'title',
),
);
$this->addMappings('node', $mappings);
// Straight up upload is covered in other tests, focus on direct mode
// and file batching here.
// Straight up upload is covered in other tests, focus on direct mode and
// file batching here.
$settings = array(
'direct' => TRUE,
'directory' => 'private://feeds',
......@@ -111,4 +102,96 @@ class FeedsFileFetcherTestCase extends FeedsWebTestCase {
$this->assertText('Created 18 nodes');
}
/**
* Tests if files can be removed after the import has finished.
*/
public function testRemoveFileAfterImport() {
$this->setSettings('node', 'FeedsFileFetcher', array(
'delete_uploaded_file' => TRUE,
'directory' => 'private://feeds',
));
// Import the file.
$this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 2 nodes');
// Assert that the file no longer exists.
$this->assertFalse(file_exists('private://feeds/content.csv'), 'The imported file no longer exists.');
// Assert that the file is no longer shown on the import form.
$this->drupalGet('import/node');
$this->assertNoText('nodes.csv');
}
/**
* Tests if files can be removed after import when running the import in
* background.
*/
public function testRemoveFileAfterImportInBackground() {
// Configure to import in background and import as often as possible.
$this->setSettings('node', NULL, array(
'import_period' => 0,
'import_on_create' => FALSE,
'process_in_background' => TRUE,
));
$this->setSettings('node', 'FeedsFileFetcher', array(
'delete_uploaded_file' => TRUE,
'directory' => 'private://feeds',
));
// Make sure that the import cannot be completed in one run.
variable_set('feeds_process_limit', 5);
// Set variable to enforce that only five items get imported per cron run.
// @see feeds_tests_cron_queue_alter()
// @see feeds_tests_feeds_after_save()
variable_set('feeds_tests_feeds_source_import_queue_time', 5);
variable_set('feeds_tests_feeds_after_save_sleep', 1);
// Import a file with 9 nodes.
$this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv');
// Assert that the file has been created.
$this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file is created.');
// Run cron and assert that five nodes have been created.
$this->cronRun();
$node_count = db_select('node')
->fields('node', array())
->countQuery()
->execute()
->fetchField();
$this->assertEqual(5, $node_count, format_string('Five nodes have been created (actual: @count).', array(
'@count' => $node_count,
)));
// Assert that the file to import still exists as the import hasn't finished
// yet.
drupal_flush_all_caches();
$this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file still exists.');
// Run cron again to import the remaining 4 nodes and assert that 9 nodes
// exist in total.
$this->cronRun();
$node_count = db_select('node')
->fields('node', array())
->countQuery()
->execute()
->fetchField();
$this->assertEqual(9, $node_count, format_string('Nine nodes have been created (actual: @count).', array(
'@count' => $node_count,
)));
// Assert that the file to import finally has been removed now.
drupal_flush_all_caches();
$this->assertFalse(file_exists('private://feeds/nodes.csv'), 'The imported file no longer exists.');
// Assert that running a second import does not result into errors.
$this->cronRun();
// Assert that the file is no longer shown on the import form.
$this->drupalGet('import/node');
$this->assertNoText('nodes.csv');
}
}
......@@ -60,6 +60,20 @@ function feeds_tests_theme() {
);
}
/**
* Implements hook_cron_queue_alter().
*
* Changes runtime limit for feeds_source_import queue.
*
* @see FeedsFileFetcherTestCase::testRemoveFileAfterImportInBackground()
*/
function feeds_tests_cron_queue_info_alter(&$queues) {
$feeds_source_import_queue_time = variable_get('feeds_tests_feeds_source_import_queue_time', FALSE);
if ($feeds_source_import_queue_time && isset($queues['feeds_source_import'])) {
$queues['feeds_source_import']['time'] = $feeds_source_import_queue_time;
}
}
/**
* Outputs flickr test feed.
*/
......@@ -383,6 +397,18 @@ function feeds_tests_feeds_after_parse(FeedsSource $source, FeedsParserResult $r
}
}
/**
* Implements hook_feeds_after_save().
*
* @see FeedsFileFetcherTestCase::testRemoveFileAfterImportInBackground()
*/
function feeds_tests_feeds_after_save() {
$sleep = variable_get('feeds_tests_feeds_after_save_sleep', FALSE);
if ($sleep) {
sleep($sleep);
}
}
/**
* Helper class to ensure callbacks can be objects.
*/
......
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