1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* File fetcher tests.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* File fetcher test class.
|
10
|
*/
|
11
|
class FeedsFileFetcherTestCase extends FeedsWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* {@inheritdoc}
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => 'File fetcher',
|
19
|
'description' => 'Tests for file fetcher plugin.',
|
20
|
'group' => 'Feeds',
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* {@inheritdoc}
|
26
|
*/
|
27
|
public function setUp() {
|
28
|
parent::setUp();
|
29
|
|
30
|
// Set up an importer.
|
31
|
$this->createImporterConfiguration('Node import', 'node');
|
32
|
// Set and configure plugins and mappings.
|
33
|
$this->setSettings('node', NULL, array('content_type' => ''));
|
34
|
$this->setPlugin('node', 'FeedsFileFetcher');
|
35
|
$this->setPlugin('node', 'FeedsCSVParser');
|
36
|
$this->addMappings('node', array(
|
37
|
'0' => array(
|
38
|
'source' => 'title',
|
39
|
'target' => 'title',
|
40
|
),
|
41
|
));
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Test scheduling on cron.
|
46
|
*/
|
47
|
public function testPublicFiles() {
|
48
|
// Straight up upload is covered in other tests, focus on direct mode and
|
49
|
// file batching here.
|
50
|
$settings = array(
|
51
|
'direct' => TRUE,
|
52
|
'directory' => 'public://feeds',
|
53
|
);
|
54
|
$this->setSettings('node', 'FeedsFileFetcher', $settings);
|
55
|
|
56
|
// Verify that invalid paths are not accepted.
|
57
|
foreach (array('/tmp/') as $path) {
|
58
|
$edit = array(
|
59
|
'feeds[FeedsFileFetcher][source]' => $path,
|
60
|
);
|
61
|
$this->drupalPost('import/node', $edit, t('Import'));
|
62
|
$this->assertText("The file needs to reside within the site's files directory, its path needs to start with scheme://. Available schemes:");
|
63
|
$count = db_query("SELECT COUNT(*) FROM {feeds_source} WHERE feed_nid = 0")->fetchField();
|
64
|
$this->assertEqual($count, 0);
|
65
|
}
|
66
|
|
67
|
// Verify batching through directories.
|
68
|
// Copy directory of files.
|
69
|
$dir = 'public://batchtest';
|
70
|
$this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
|
71
|
|
72
|
// Ingest directory of files. Set limit to 5 to force processor to batch,
|
73
|
// too.
|
74
|
variable_set('feeds_process_limit', 5);
|
75
|
$edit = array(
|
76
|
'feeds[FeedsFileFetcher][source]' => $dir,
|
77
|
);
|
78
|
$this->drupalPost('import/node', $edit, t('Import'));
|
79
|
$this->assertText('Created 18 nodes');
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Test uploading private files.
|
84
|
*/
|
85
|
public function testPrivateFiles() {
|
86
|
// Straight up upload is covered in other tests, focus on direct mode and
|
87
|
// file batching here.
|
88
|
$settings = array(
|
89
|
'direct' => TRUE,
|
90
|
'directory' => 'private://feeds',
|
91
|
);
|
92
|
$this->setSettings('node', 'FeedsFileFetcher', $settings);
|
93
|
|
94
|
// Verify batching through directories.
|
95
|
// Copy directory of files.
|
96
|
$dir = 'private://batchtest';
|
97
|
$this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
|
98
|
|
99
|
// Ingest directory of files. Set limit to 5 to force processor to batch,
|
100
|
// too.
|
101
|
variable_set('feeds_process_limit', 5);
|
102
|
$edit = array(
|
103
|
'feeds[FeedsFileFetcher][source]' => $dir,
|
104
|
);
|
105
|
$this->drupalPost('import/node', $edit, t('Import'));
|
106
|
$this->assertText('Created 18 nodes');
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Tests if files can be removed after the import has finished.
|
111
|
*/
|
112
|
public function testRemoveFileAfterImport() {
|
113
|
$this->setSettings('node', 'FeedsFileFetcher', array(
|
114
|
'delete_uploaded_file' => TRUE,
|
115
|
'directory' => 'private://feeds',
|
116
|
));
|
117
|
|
118
|
// Import the file.
|
119
|
$this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
|
120
|
$this->assertText('Created 2 nodes');
|
121
|
|
122
|
// Assert that the file no longer exists.
|
123
|
$this->assertFalse(file_exists('private://feeds/content.csv'), 'The imported file no longer exists.');
|
124
|
|
125
|
// Assert that the file is no longer shown on the import form.
|
126
|
$this->drupalGet('import/node');
|
127
|
$this->assertNoText('nodes.csv');
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Tests if files can be removed after import when running the import in
|
132
|
* background.
|
133
|
*/
|
134
|
public function testRemoveFileAfterImportInBackground() {
|
135
|
// Configure to import in background and import as often as possible.
|
136
|
$this->setSettings('node', NULL, array(
|
137
|
'import_period' => 0,
|
138
|
'import_on_create' => FALSE,
|
139
|
'process_in_background' => TRUE,
|
140
|
));
|
141
|
$this->setSettings('node', 'FeedsFileFetcher', array(
|
142
|
'delete_uploaded_file' => TRUE,
|
143
|
'directory' => 'private://feeds',
|
144
|
));
|
145
|
|
146
|
// Make sure that the import cannot be completed in one run.
|
147
|
variable_set('feeds_process_limit', 5);
|
148
|
|
149
|
// Set variable to enforce that only five items get imported per cron run.
|
150
|
// @see feeds_tests_cron_queue_alter()
|
151
|
// @see feeds_tests_feeds_after_save()
|
152
|
variable_set('feeds_tests_feeds_source_import_queue_time', 5);
|
153
|
variable_set('feeds_tests_feeds_after_save_sleep', 1);
|
154
|
|
155
|
// Import a file with 9 nodes.
|
156
|
$this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv', 'Schedule import');
|
157
|
|
158
|
// Assert that the file has been created.
|
159
|
$this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file is created.');
|
160
|
|
161
|
// Run cron and assert that five nodes have been created.
|
162
|
$this->cronRun();
|
163
|
$node_count = db_select('node')
|
164
|
->fields('node', array())
|
165
|
->countQuery()
|
166
|
->execute()
|
167
|
->fetchField();
|
168
|
$this->assertEqual(5, $node_count, format_string('Five nodes have been created (actual: @count).', array(
|
169
|
'@count' => $node_count,
|
170
|
)));
|
171
|
|
172
|
// Assert that the file to import still exists as the import hasn't finished
|
173
|
// yet.
|
174
|
drupal_flush_all_caches();
|
175
|
$this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file still exists.');
|
176
|
|
177
|
// Run cron again to import the remaining 4 nodes and assert that 9 nodes
|
178
|
// exist in total.
|
179
|
$this->cronRun();
|
180
|
$node_count = db_select('node')
|
181
|
->fields('node', array())
|
182
|
->countQuery()
|
183
|
->execute()
|
184
|
->fetchField();
|
185
|
$this->assertEqual(9, $node_count, format_string('Nine nodes have been created (actual: @count).', array(
|
186
|
'@count' => $node_count,
|
187
|
)));
|
188
|
|
189
|
// Assert that the file to import finally has been removed now.
|
190
|
drupal_flush_all_caches();
|
191
|
$this->assertFalse(file_exists('private://feeds/nodes.csv'), 'The imported file no longer exists.');
|
192
|
|
193
|
// Assert that running a second import does not result into errors.
|
194
|
$this->cronRun();
|
195
|
|
196
|
// Assert that the file is no longer shown on the import form.
|
197
|
$this->drupalGet('import/node');
|
198
|
$this->assertNoText('nodes.csv');
|
199
|
}
|
200
|
|
201
|
}
|