1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Test case for Filefield mapper mappers/filefield.inc.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Class for testing Feeds file mapper.
|
10 |
|
|
*
|
11 |
|
|
* @todo Add a test for enclosures using a local file that is
|
12 |
|
|
* a) in place and that
|
13 |
|
|
* b) needs to be copied from one location to another.
|
14 |
|
|
*/
|
15 |
|
|
class FeedsMapperFileTestCase extends FeedsMapperTestCase {
|
16 |
|
|
public static function getInfo() {
|
17 |
|
|
return array(
|
18 |
|
|
'name' => 'Mapper: File field',
|
19 |
|
|
'description' => 'Test Feeds Mapper support for file fields. <strong>Requires SimplePie library</strong>.',
|
20 |
|
|
'group' => 'Feeds',
|
21 |
|
|
);
|
22 |
|
|
}
|
23 |
|
|
|
24 |
2c8c2b87
|
Assos Assos
|
public function setUp() {
|
25 |
|
|
parent::setUp(array('dblog'));
|
26 |
|
|
}
|
27 |
|
|
|
28 |
85ad3d82
|
Assos Assos
|
/**
|
29 |
|
|
* Basic test loading a single entry CSV file.
|
30 |
|
|
*/
|
31 |
|
|
public function test() {
|
32 |
|
|
// If this is unset (or FALSE) http_request.inc will use curl, and will generate a 404
|
33 |
|
|
// for this feel url provided by feeds_tests. However, if feeds_tests was enabled in your
|
34 |
|
|
// site before running the test, it will work fine. Since it is truly screwy, lets just
|
35 |
|
|
// force it to use drupal_http_request for this test case.
|
36 |
|
|
variable_set('feeds_never_use_curl', TRUE);
|
37 |
|
|
variable_set('clean_url', TRUE);
|
38 |
|
|
|
39 |
|
|
// Only download simplepie if the plugin doesn't already exist somewhere.
|
40 |
|
|
// People running tests locally might have it.
|
41 |
|
|
if (!feeds_simplepie_exists()) {
|
42 |
|
|
$this->downloadExtractSimplePie('1.3');
|
43 |
|
|
$this->assertTrue(feeds_simplepie_exists());
|
44 |
|
|
// Reset all the caches!
|
45 |
|
|
$this->resetAll();
|
46 |
|
|
}
|
47 |
2c8c2b87
|
Assos Assos
|
$typename = $this->createContentType(array(), array(
|
48 |
|
|
'files' => array(
|
49 |
|
|
'type' => 'file',
|
50 |
|
|
'instance_settings' => array(
|
51 |
|
|
'instance[settings][file_extensions]' => 'png, gif, jpg, jpeg',
|
52 |
|
|
),
|
53 |
|
|
),
|
54 |
|
|
));
|
55 |
85ad3d82
|
Assos Assos
|
|
56 |
|
|
// 1) Test mapping remote resources to file field.
|
57 |
|
|
|
58 |
|
|
// Create importer configuration.
|
59 |
|
|
$this->createImporterConfiguration();
|
60 |
|
|
$this->setPlugin('syndication', 'FeedsSimplePieParser');
|
61 |
|
|
$this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
|
62 |
|
|
$this->addMappings('syndication', array(
|
63 |
|
|
0 => array(
|
64 |
|
|
'source' => 'title',
|
65 |
|
|
'target' => 'title'
|
66 |
|
|
),
|
67 |
|
|
1 => array(
|
68 |
|
|
'source' => 'timestamp',
|
69 |
|
|
'target' => 'created'
|
70 |
|
|
),
|
71 |
|
|
2 => array(
|
72 |
|
|
'source' => 'enclosures',
|
73 |
|
|
'target' => 'field_files'
|
74 |
|
|
),
|
75 |
|
|
));
|
76 |
|
|
$nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml');
|
77 |
|
|
$this->assertText('Created 5 nodes');
|
78 |
|
|
|
79 |
|
|
$files = $this->_testFiles();
|
80 |
|
|
$entities = db_select('feeds_item')
|
81 |
|
|
->fields('feeds_item', array('entity_id'))
|
82 |
|
|
->condition('id', 'syndication')
|
83 |
|
|
->execute();
|
84 |
|
|
foreach ($entities as $entity) {
|
85 |
|
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
86 |
2c8c2b87
|
Assos Assos
|
$this->assertText(str_replace(' ', '_', array_shift($files)));
|
87 |
85ad3d82
|
Assos Assos
|
}
|
88 |
|
|
|
89 |
|
|
// 2) Test mapping local resources to file field.
|
90 |
|
|
|
91 |
|
|
// Copy directory of files, CSV file expects them in public://images, point
|
92 |
|
|
// file field to a 'resources' directory. Feeds should copy files from
|
93 |
|
|
// images/ to resources/ on import.
|
94 |
|
|
$this->copyDir($this->absolutePath() . '/tests/feeds/assets', 'public://images');
|
95 |
|
|
$edit = array(
|
96 |
|
|
'instance[settings][file_directory]' => 'resources',
|
97 |
|
|
);
|
98 |
|
|
$this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
|
99 |
|
|
|
100 |
|
|
// Create a CSV importer configuration.
|
101 |
|
|
$this->createImporterConfiguration('Node import from CSV', 'node');
|
102 |
|
|
$this->setPlugin('node', 'FeedsCSVParser');
|
103 |
|
|
$this->setSettings('node', 'FeedsNodeProcessor', array('bundle' => $typename));
|
104 |
|
|
$this->setSettings('node', NULL, array('content_type' => ''));
|
105 |
|
|
$this->addMappings('node', array(
|
106 |
|
|
0 => array(
|
107 |
|
|
'source' => 'title',
|
108 |
|
|
'target' => 'title'
|
109 |
|
|
),
|
110 |
|
|
1 => array(
|
111 |
|
|
'source' => 'file',
|
112 |
|
|
'target' => 'field_files'
|
113 |
|
|
),
|
114 |
|
|
));
|
115 |
|
|
|
116 |
|
|
// Import.
|
117 |
|
|
$edit = array(
|
118 |
|
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
|
119 |
|
|
);
|
120 |
|
|
$this->drupalPost('import/node', $edit, 'Import');
|
121 |
|
|
$this->assertText('Created 5 nodes');
|
122 |
|
|
|
123 |
|
|
// Assert: files should be in resources/.
|
124 |
|
|
$files = $this->_testFiles();
|
125 |
|
|
$entities = db_select('feeds_item')
|
126 |
|
|
->fields('feeds_item', array('entity_id'))
|
127 |
|
|
->condition('id', 'node')
|
128 |
|
|
->execute();
|
129 |
|
|
foreach ($entities as $entity) {
|
130 |
|
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
131 |
2c8c2b87
|
Assos Assos
|
$this->assertRaw('resources/' . rawurlencode(array_shift($files)));
|
132 |
85ad3d82
|
Assos Assos
|
}
|
133 |
|
|
|
134 |
|
|
// 3) Test mapping of local resources, this time leave files in place.
|
135 |
|
|
$this->drupalPost('import/node/delete-items', array(), 'Delete');
|
136 |
|
|
// Setting the fields file directory to images will make copying files
|
137 |
|
|
// obsolete.
|
138 |
|
|
$edit = array(
|
139 |
|
|
'instance[settings][file_directory]' => 'images',
|
140 |
|
|
);
|
141 |
|
|
$this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
|
142 |
|
|
$edit = array(
|
143 |
|
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
|
144 |
|
|
);
|
145 |
|
|
$this->drupalPost('import/node', $edit, 'Import');
|
146 |
|
|
$this->assertText('Created 5 nodes');
|
147 |
|
|
|
148 |
|
|
// Assert: files should be in images/ now.
|
149 |
|
|
$files = $this->_testFiles();
|
150 |
|
|
$entities = db_select('feeds_item')
|
151 |
|
|
->fields('feeds_item', array('entity_id'))
|
152 |
|
|
->condition('id', 'node')
|
153 |
|
|
->execute();
|
154 |
|
|
foreach ($entities as $entity) {
|
155 |
|
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
156 |
2c8c2b87
|
Assos Assos
|
$this->assertRaw('images/' . rawurlencode(array_shift($files)));
|
157 |
85ad3d82
|
Assos Assos
|
}
|
158 |
|
|
|
159 |
|
|
// Deleting all imported items will delete the files from the images/ dir.
|
160 |
|
|
// @todo: for some reason the first file does not get deleted.
|
161 |
|
|
// $this->drupalPost('import/node/delete-items', array(), 'Delete');
|
162 |
|
|
// foreach ($this->_testFiles() as $file) {
|
163 |
|
|
// $this->assertFalse(is_file("public://images/$file"));
|
164 |
|
|
// }
|
165 |
|
|
}
|
166 |
|
|
|
167 |
2c8c2b87
|
Assos Assos
|
public function testInvalidFileExtension() {
|
168 |
|
|
variable_set('feeds_never_use_curl', TRUE);
|
169 |
|
|
|
170 |
|
|
$typename = $this->createContentType(array(), array(
|
171 |
|
|
'files' => array(
|
172 |
|
|
'type' => 'file',
|
173 |
|
|
'instance_settings' => array(
|
174 |
|
|
'instance[settings][file_extensions]' => 'txt',
|
175 |
|
|
),
|
176 |
|
|
),
|
177 |
|
|
));
|
178 |
|
|
|
179 |
|
|
// Create a CSV importer configuration.
|
180 |
|
|
$this->createImporterConfiguration('Node import from CSV', 'invalid_extension');
|
181 |
|
|
$this->setPlugin('invalid_extension', 'FeedsCSVParser');
|
182 |
|
|
$this->setSettings('invalid_extension', 'FeedsNodeProcessor', array('bundle' => $typename));
|
183 |
|
|
$this->setSettings('invalid_extension', NULL, array('content_type' => ''));
|
184 |
|
|
$this->addMappings('invalid_extension', array(
|
185 |
|
|
0 => array(
|
186 |
|
|
'source' => 'title',
|
187 |
|
|
'target' => 'title',
|
188 |
|
|
),
|
189 |
|
|
1 => array(
|
190 |
|
|
'source' => 'file',
|
191 |
|
|
'target' => 'field_files',
|
192 |
|
|
),
|
193 |
|
|
));
|
194 |
|
|
|
195 |
|
|
// Import.
|
196 |
|
|
$edit = array(
|
197 |
|
|
'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files.csv', array('absolute' => TRUE)),
|
198 |
|
|
);
|
199 |
|
|
$this->drupalPost('import/invalid_extension', $edit, 'Import');
|
200 |
|
|
$this->assertText('Created 5 nodes');
|
201 |
|
|
|
202 |
|
|
foreach (range(1, 5) as $nid) {
|
203 |
|
|
$node = node_load($nid);
|
204 |
|
|
$this->assertTrue(empty($node->field_files));
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
foreach ($this->_testFiles() as $filename) {
|
208 |
|
|
$message = t('The file @file has an invalid extension.', array('@file' => $filename));
|
209 |
|
|
$this->assertTrue(db_query("SELECT 1 FROM {watchdog} WHERE message = :message", array(':message' => $message))->fetchField());
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
|
213 |
85ad3d82
|
Assos Assos
|
/**
|
214 |
|
|
* Lists test files.
|
215 |
|
|
*/
|
216 |
|
|
public function _testFiles() {
|
217 |
|
|
return array('tubing.jpeg', 'foosball.jpeg', 'attersee.jpeg', 'hstreet.jpeg', 'la fayette.jpeg');
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/**
|
221 |
|
|
* Handle file field widgets.
|
222 |
|
|
*/
|
223 |
|
|
public function selectFieldWidget($fied_name, $field_type) {
|
224 |
|
|
if ($field_type == 'file') {
|
225 |
|
|
return 'file_generic';
|
226 |
|
|
}
|
227 |
|
|
else {
|
228 |
|
|
return parent::selectFieldWidget($fied_name, $field_type);
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
} |