Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* @file
* Contains FeedsCSVParserTestCase.
*/
/**
* Tests the CSV parser using the UI.
*/
class FeedsCSVParserTestCase extends FeedsWebTestCase {
public static function getInfo() {
return array(
'name' => 'CSV parser functional tests',
'description' => 'Tests the CSV parser using the UI.',
'group' => 'Feeds',
);
}
/**
* Tests parsing a CSV when the mbstring extension is not available.
*/
public function testMbstringExtensionDisabled() {
// Set "feeds_use_mbstring" to FALSE to emulate that the mbstring extension
// is not loaded.
variable_set('feeds_use_mbstring', FALSE);
// Remove items after parsing because in < PHP 5.4 processing items with
// encoding issues leads to test failures because check_plain() can only
// handle UTF-8 encoded strings.
// @see feeds_tests_feeds_after_parse()
variable_set('feeds_tests_feeds_after_parse_empty_items', TRUE);
// Create node type.
$node_type = $this->drupalCreateContentType();
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
$this->addMappings('csv', array(
0 => array(
'source' => 'id',
'target' => 'guid',
),
1 => array(
'source' => 'text',
'target' => 'title',
),
));
// Ensure that on the CSV parser settings page a message is shown about that
// the mbstring extension is not available.
$this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
$this->assertNoField('encoding');
$this->assertText('PHP mbstring extension must be available for character encoding conversion.');
// Try to import a CSV file that is not UTF-8 encoded. No encoding warning
// should be shown, but import should fail.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
$this->assertNoText('Source file is not in UTF-8 encoding.');
}
/**
* Tests an encoding failure during parsing a CSV.
*/
public function testEncodingFailure() {
// Create node type.
$node_type = $this->drupalCreateContentType();
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
$this->addMappings('csv', array(
0 => array(
'source' => 'id',
'target' => 'guid',
),
1 => array(
'source' => 'text',
'target' => 'title',
),
));
// Ensure that on the CSV parser settings page a setting for encoding is
// shown.
$this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
$this->assertField('encoding');
$this->assertNoText('PHP mbstring extension must be available for character encoding conversion.');
// Try to import a CSV file that is not UTF-8 encoded. Import should be
// halted and an encoding warning should be shown.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
$this->assertNoText('Failed importing 4 nodes.');
$this->assertText('Source file is not in UTF-8 encoding.');
}
100
101
102
103
104
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Tests if a CSV template is generated properly using various settings.
*
* @see ::getTemplateDataProvider()
*/
public function testGetTemplate() {
// Create node type.
$node_type = $this->drupalCreateContentType();
foreach ($this->getTemplateDataProvider() as $key => $testdata) {
// Prepend 'csv' to importer machine name as '0' is not a valid machine
// name.
$key = 'csv' . $key;
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', $key);
$this->setPlugin($key, 'FeedsCSVParser');
$this->setSettings($key, 'FeedsCSVParser', array(
'delimiter' => $testdata['delimiter'],
));
$this->setSettings($key, 'FeedsNodeProcessor', array('bundle' => $node_type->type));
$this->addMappings($key, $testdata['mapping']);
// Get CSV template and assert result.
$this->drupalGet('import/' . $key . '/template');
$this->assertRaw($testdata['expected']);
}
}
/**
* Data provider for ::testGetTemplate().
*/
protected function getTemplateDataProvider() {
return array(
// Delimiter ',' test. Source keys containing a ',' should be wrapped in
// quotes.
array(
'delimiter' => ',',
'mapping' => array(
array(
'source' => 'title+;|',
'target' => 'title',
),
array(
'source' => 'alpha, beta + gamma',
'target' => 'body',
),
array(
'source' => 'guid',
'target' => 'guid',
),
),
'expected' => 'title+;|,"alpha, beta + gamma",guid',
),
// Delimiter ';' test. Source keys containing a ';' should be wrapped in
// quotes.
array(
'delimiter' => ';',
'mapping' => array(
array(
'source' => 'title;)',
'target' => 'title',
),
array(
'source' => 'alpha, beta + gamma',
'target' => 'body',
),
array(
'source' => 'guid',
'target' => 'guid',
),
),
'expected' => '"title;)";alpha, beta + gamma;guid',
),
// Delimiter 'TAB' test.
array(
'delimiter' => 'TAB',
'mapping' => array(
array(
'source' => 'title,;|',
'target' => 'title',
),
array(
'source' => 'alpha, beta + gamma',
'target' => 'body',
),
array(
'source' => 'guid',
'target' => 'guid',
),
),
'expected' => 'title,;| alpha, beta + gamma guid',
),
// Delimiter '|' test. Source keys containing a '|' should be wrapped in
// quotes.
array(
'delimiter' => '|',
'mapping' => array(
array(
'source' => 'title+;,',
'target' => 'title',
),
array(
'source' => 'alpha|beta|gamma',
'target' => 'body',
),
array(
'source' => 'guid',
'target' => 'guid',
),
),
'expected' => 'title+;,|"alpha|beta|gamma"|guid',
),
// Delimiter '+' test. Source keys containing a '+' should be wrapped in
// quotes.
array(
'delimiter' => '+',
'mapping' => array(
array(
'source' => 'title,;|',
'target' => 'title',
),
array(
'source' => 'alpha, beta + gamma',
'target' => 'body',
),
array(
'source' => 'guid',
'target' => 'guid',
),
),
'expected' => 'title,;|+"alpha, beta + gamma"+guid',
),
// Ensure that when a source key is used multiple times in mapping, the
// key is only printed once in the CSV template.
array(
'delimiter' => ',',
'mapping' => array(
array(
'source' => 'text',
'target' => 'title',
),
array(
'source' => 'guid',
'target' => 'guid',
),
array(
'source' => 'date',
'target' => 'created',
),
array(
'source' => 'date',
'target' => 'changed',
),
array(
'source' => 'text',
'target' => 'body',
),
),
'expected' => 'text,guid,date',
),
// Special characters. Things like '&' shouldn't be converted to '&'
// for example.
array(
'delimiter' => ',',
'mapping' => array(
array(
'source' => '&',
'target' => 'title',
),
array(
'source' => 'alpha&beta',
'target' => 'body',
),
array(
'source' => '<created>',
'target' => 'created',
),
array(
'source' => '\'guid\'',
'target' => 'guid',
),
),
'expected' => '&,alpha&beta,<created>,\'guid\'',
),
);
}