1
|
<?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
|
/**
|
25
|
* Basic test loading a single entry CSV file.
|
26
|
*/
|
27
|
public function test() {
|
28
|
// If this is unset (or FALSE) http_request.inc will use curl, and will generate a 404
|
29
|
// for this feel url provided by feeds_tests. However, if feeds_tests was enabled in your
|
30
|
// site before running the test, it will work fine. Since it is truly screwy, lets just
|
31
|
// force it to use drupal_http_request for this test case.
|
32
|
variable_set('feeds_never_use_curl', TRUE);
|
33
|
variable_set('clean_url', TRUE);
|
34
|
|
35
|
// Only download simplepie if the plugin doesn't already exist somewhere.
|
36
|
// People running tests locally might have it.
|
37
|
if (!feeds_simplepie_exists()) {
|
38
|
$this->downloadExtractSimplePie('1.3');
|
39
|
$this->assertTrue(feeds_simplepie_exists());
|
40
|
// Reset all the caches!
|
41
|
$this->resetAll();
|
42
|
}
|
43
|
$typename = $this->createContentType(array(), array('files' => 'file'));
|
44
|
|
45
|
// 1) Test mapping remote resources to file field.
|
46
|
|
47
|
// Create importer configuration.
|
48
|
$this->createImporterConfiguration();
|
49
|
$this->setPlugin('syndication', 'FeedsSimplePieParser');
|
50
|
$this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
|
51
|
$this->addMappings('syndication', array(
|
52
|
0 => array(
|
53
|
'source' => 'title',
|
54
|
'target' => 'title'
|
55
|
),
|
56
|
1 => array(
|
57
|
'source' => 'timestamp',
|
58
|
'target' => 'created'
|
59
|
),
|
60
|
2 => array(
|
61
|
'source' => 'enclosures',
|
62
|
'target' => 'field_files'
|
63
|
),
|
64
|
));
|
65
|
$nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml');
|
66
|
$this->assertText('Created 5 nodes');
|
67
|
|
68
|
$files = $this->_testFiles();
|
69
|
$entities = db_select('feeds_item')
|
70
|
->fields('feeds_item', array('entity_id'))
|
71
|
->condition('id', 'syndication')
|
72
|
->execute();
|
73
|
foreach ($entities as $entity) {
|
74
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
75
|
$f = new FeedsEnclosure(array_shift($files), NULL);
|
76
|
$this->assertText($f->getLocalValue());
|
77
|
}
|
78
|
|
79
|
// 2) Test mapping local resources to file field.
|
80
|
|
81
|
// Copy directory of files, CSV file expects them in public://images, point
|
82
|
// file field to a 'resources' directory. Feeds should copy files from
|
83
|
// images/ to resources/ on import.
|
84
|
$this->copyDir($this->absolutePath() . '/tests/feeds/assets', 'public://images');
|
85
|
$edit = array(
|
86
|
'instance[settings][file_directory]' => 'resources',
|
87
|
);
|
88
|
$this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
|
89
|
|
90
|
// Create a CSV importer configuration.
|
91
|
$this->createImporterConfiguration('Node import from CSV', 'node');
|
92
|
$this->setPlugin('node', 'FeedsCSVParser');
|
93
|
$this->setSettings('node', 'FeedsNodeProcessor', array('bundle' => $typename));
|
94
|
$this->setSettings('node', NULL, array('content_type' => ''));
|
95
|
$this->addMappings('node', array(
|
96
|
0 => array(
|
97
|
'source' => 'title',
|
98
|
'target' => 'title'
|
99
|
),
|
100
|
1 => array(
|
101
|
'source' => 'file',
|
102
|
'target' => 'field_files'
|
103
|
),
|
104
|
));
|
105
|
|
106
|
// Import.
|
107
|
$edit = array(
|
108
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
|
109
|
);
|
110
|
$this->drupalPost('import/node', $edit, 'Import');
|
111
|
$this->assertText('Created 5 nodes');
|
112
|
|
113
|
// Assert: files should be in resources/.
|
114
|
$files = $this->_testFiles();
|
115
|
$entities = db_select('feeds_item')
|
116
|
->fields('feeds_item', array('entity_id'))
|
117
|
->condition('id', 'node')
|
118
|
->execute();
|
119
|
foreach ($entities as $entity) {
|
120
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
121
|
$f = new FeedsEnclosure(array_shift($files), NULL);
|
122
|
$this->assertRaw('resources/' . $f->getUrlEncodedValue());
|
123
|
}
|
124
|
|
125
|
// 3) Test mapping of local resources, this time leave files in place.
|
126
|
$this->drupalPost('import/node/delete-items', array(), 'Delete');
|
127
|
// Setting the fields file directory to images will make copying files
|
128
|
// obsolete.
|
129
|
$edit = array(
|
130
|
'instance[settings][file_directory]' => 'images',
|
131
|
);
|
132
|
$this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
|
133
|
$edit = array(
|
134
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
|
135
|
);
|
136
|
$this->drupalPost('import/node', $edit, 'Import');
|
137
|
$this->assertText('Created 5 nodes');
|
138
|
|
139
|
// Assert: files should be in images/ now.
|
140
|
$files = $this->_testFiles();
|
141
|
$entities = db_select('feeds_item')
|
142
|
->fields('feeds_item', array('entity_id'))
|
143
|
->condition('id', 'node')
|
144
|
->execute();
|
145
|
foreach ($entities as $entity) {
|
146
|
$this->drupalGet('node/' . $entity->entity_id . '/edit');
|
147
|
$f = new FeedsEnclosure(array_shift($files), NULL);
|
148
|
$this->assertRaw('images/' . $f->getUrlEncodedValue());
|
149
|
}
|
150
|
|
151
|
// Deleting all imported items will delete the files from the images/ dir.
|
152
|
// @todo: for some reason the first file does not get deleted.
|
153
|
// $this->drupalPost('import/node/delete-items', array(), 'Delete');
|
154
|
// foreach ($this->_testFiles() as $file) {
|
155
|
// $this->assertFalse(is_file("public://images/$file"));
|
156
|
// }
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Lists test files.
|
161
|
*/
|
162
|
public function _testFiles() {
|
163
|
return array('tubing.jpeg', 'foosball.jpeg', 'attersee.jpeg', 'hstreet.jpeg', 'la fayette.jpeg');
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Handle file field widgets.
|
168
|
*/
|
169
|
public function selectFieldWidget($fied_name, $field_type) {
|
170
|
if ($field_type == 'file') {
|
171
|
return 'file_generic';
|
172
|
}
|
173
|
else {
|
174
|
return parent::selectFieldWidget($fied_name, $field_type);
|
175
|
}
|
176
|
}
|
177
|
}
|