Newer
Older
<?php
/**
* @file
* File fetcher tests.
*/
/**
* File fetcher test class.
*/
class FeedsFileFetcherTestCase extends FeedsWebTestCase {
Dave Reid
committed
public static function getInfo() {
Dave Reid
committed
'name' => 'File fetcher',
'description' => 'Tests for file fetcher plugin.',
'group' => 'Feeds',
drothstein
committed
* {@inheritdoc}
drothstein
committed
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');
drothstein
committed
$this->addMappings('node', array(
'0' => array(
'source' => 'title',
'target' => 'title',
),
drothstein
committed
));
}
/**
* 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',
);
$this->setSettings('node', 'FeedsFileFetcher', $settings);
// Verify that invalid paths are not accepted.
foreach (array('/tmp/') as $path) {
$edit = array(
'feeds[FeedsFileFetcher][source]' => $path,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText("The file needs to reside within the site's files directory, its path needs to start with scheme://. Available schemes:");
$count = db_query("SELECT COUNT(*) FROM {feeds_source} WHERE feed_nid = 0")->fetchField();
$this->assertEqual($count, 0);
}
// Verify batching through directories.
// Copy directory of files.
$dir = 'public://batchtest';
$this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
// Ingest directory of files. Set limit to 5 to force processor to batch,
// too.
variable_set('feeds_process_limit', 5);
$edit = array(
'feeds[FeedsFileFetcher][source]' => $dir,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 18 nodes');
/**
* Test uploading private files.
*/
public function testPrivateFiles() {
drothstein
committed
// Straight up upload is covered in other tests, focus on direct mode and
// file batching here.
$settings = array(
'direct' => TRUE,
'directory' => 'private://feeds',
);
$this->setSettings('node', 'FeedsFileFetcher', $settings);
// Verify batching through directories.
// Copy directory of files.
$dir = 'private://batchtest';
$this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
// Ingest directory of files. Set limit to 5 to force processor to batch,
// too.
variable_set('feeds_process_limit', 5);
$edit = array(
'feeds[FeedsFileFetcher][source]' => $dir,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 18 nodes');
}
drothstein
committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* 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');
}