Newer
Older
Alex Barth
committed
<?php
/**
* @file
* Contains FeedsMapperFieldTestCase.
Alex Barth
committed
*/
/**
* Test case for simple CCK field mapper mappers/content.inc.
Alex Barth
committed
*/
class FeedsMapperFieldTestCase extends FeedsMapperTestCase {
Alex Barth
committed
public static function getInfo() {
return array(
Dave Reid
committed
'name' => 'Mapper: Fields',
'description' => 'Test Feeds Mapper support for fields.',
'group' => 'Feeds',
Alex Barth
committed
);
}
Dave Reid
committed
public function setUp() {
parent::setUp(array('number'));
}
Alex Barth
committed
/**
* Basic test loading a double entry CSV file.
Alex Barth
committed
*/
public function test() {
// Create content type.
$typename = $this->createContentType(array(), array(
Alex Barth
committed
'alpha' => 'text',
'beta' => 'number_integer',
'gamma' => 'number_decimal',
'delta' => 'number_float',
Alex Barth
committed
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
Alex Barth
committed
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
Alex Barth
committed
$this->addMappings('csv', array(
niklas
committed
0 => array(
Alex Barth
committed
'source' => 'title',
'target' => 'title',
),
niklas
committed
1 => array(
Alex Barth
committed
'source' => 'created',
'target' => 'created',
),
niklas
committed
2 => array(
Alex Barth
committed
'source' => 'body',
'target' => 'body',
),
niklas
committed
3 => array(
Alex Barth
committed
'source' => 'alpha',
'target' => 'field_alpha',
),
niklas
committed
4 => array(
Alex Barth
committed
'source' => 'beta',
'target' => 'field_beta',
),
niklas
committed
5 => array(
Alex Barth
committed
'source' => 'gamma',
'target' => 'field_gamma',
),
niklas
committed
6 => array(
'source' => 'delta',
'target' => 'field_delta',
),
Alex Barth
committed
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 2 nodes');
Alex Barth
committed
// Check the two imported files.
$this->drupalGet('node/1/edit');
$this->assertNodeFieldValue('alpha', 'Lorem');
$this->assertNodeFieldValue('beta', '42');
$this->assertNodeFieldValue('gamma', '4.20');
$this->assertNodeFieldValue('delta', '3.14159');
Alex Barth
committed
$this->drupalGet('node/2/edit');
$this->assertNodeFieldValue('alpha', 'Ut wisi');
$this->assertNodeFieldValue('beta', '32');
$this->assertNodeFieldValue('gamma', '1.20');
$this->assertNodeFieldValue('delta', '5.62951');
Alex Barth
committed
}
91
92
93
94
95
96
97
98
99
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
/**
* Tests if values are cleared out when an empty value is provided.
*/
public function testClearOutValues() {
// Create content type.
$typename = $this->createContentType(array(), array(
'alpha' => 'text',
'beta' => 'number_integer',
'gamma' => 'number_decimal',
'delta' => 'number_float',
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename, 'update_existing' => 1));
$this->addMappings('csv', array(
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
array(
'source' => 'title',
'target' => 'title',
),
array(
'source' => 'created',
'target' => 'created',
),
array(
'source' => 'body',
'target' => 'body',
),
array(
'source' => 'alpha',
'target' => 'field_alpha',
),
array(
'source' => 'beta',
'target' => 'field_beta',
),
array(
'source' => 'gamma',
'target' => 'field_gamma',
),
array(
'source' => 'delta',
'target' => 'field_delta',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 2 nodes');
// Check the two imported nodes.
$this->drupalGet('node/1/edit');
$this->assertNodeFieldValue('alpha', 'Lorem');
$this->assertNodeFieldValue('beta', '42');
$this->assertNodeFieldValue('gamma', '4.20');
$this->assertNodeFieldValue('delta', '3.14159');
$this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.');
$this->drupalGet('node/2/edit');
$this->assertNodeFieldValue('alpha', 'Ut wisi');
$this->assertNodeFieldValue('beta', '32');
$this->assertNodeFieldValue('gamma', '1.20');
$this->assertNodeFieldValue('delta', '5.62951');
$this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.');
// Import CSV file with empty values.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
$this->assertText('Updated 2 nodes');
// Check if all values were cleared out for node 1.
$this->drupalGet('node/1/edit');
$this->assertNoNodeFieldValue('alpha', 'Lorem');
$this->assertNoNodeFieldValue('beta', '42');
$this->assertNoNodeFieldValue('gamma', '4.20');
$this->assertNoNodeFieldValue('delta', '3.14159');
$this->assertFieldByName('body[und][0][value]', '');
// Check if labels for fields that should be cleared out are not shown.
$this->drupalGet('node/1');
$this->assertNoText('alpha_text_label');
$this->assertNoText('beta_number_integer_label');
$this->assertNoText('gamma_number_decimal_label');
$this->assertNoText('delta_number_float_label');
// Check if zero's didn't cleared out values for node 2.
$this->drupalGet('node/2/edit');
$this->assertNodeFieldValue('alpha', 0);
$this->assertNodeFieldValue('beta', 0);
$this->assertNodeFieldValue('gamma', 0);
$this->assertNodeFieldValue('delta', 0);
$this->assertFieldByName('body[und][0][value]', 0);
// Check if labels for fields of node 2 are still shown.
$this->drupalGet('node/2');
$this->assertText('alpha_text_label');
$this->assertText('beta_number_integer_label');
$this->assertText('gamma_number_decimal_label');
$this->assertText('delta_number_float_label');
// Re-import the first file again.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Updated 2 nodes');
// Check if the two imported nodes have content again.
$this->drupalGet('node/1/edit');
$this->assertNodeFieldValue('alpha', 'Lorem');
$this->assertNodeFieldValue('beta', '42');
$this->assertNodeFieldValue('gamma', '4.20');
$this->assertNodeFieldValue('delta', '3.14159');
$this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.');
$this->drupalGet('node/2/edit');
$this->assertNodeFieldValue('alpha', 'Ut wisi');
$this->assertNodeFieldValue('beta', '32');
$this->assertNodeFieldValue('gamma', '1.20');
$this->assertNodeFieldValue('delta', '5.62951');
$this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.');
// Import CSV file with non-existent values.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
$this->assertText('Updated 2 nodes');
// Check if all values were cleared out for node 1.
$this->drupalGet('node/1/edit');
$this->assertNoNodeFieldValue('alpha', 'Lorem');
$this->assertNoNodeFieldValue('beta', '42');
$this->assertNoNodeFieldValue('gamma', '4.20');
$this->assertNoNodeFieldValue('delta', '3.14159');
$this->assertFieldByName('body[und][0][value]', '');
// Check if labels for fields that should be cleared out are not shown.
$this->drupalGet('node/1');
$this->assertNoText('alpha_text_label');
$this->assertNoText('beta_number_integer_label');
$this->assertNoText('gamma_number_decimal_label');
$this->assertNoText('delta_number_float_label');
}
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* Tests text field validation.
*/
public function testTextFieldValidation() {
// Create a field with settings to validate.
$max_length = 5;
$typename = $this->createContentType(array(), array(
'alpha' => array(
'type' => 'text',
'settings' => array(
'field[settings][max_length]' => $max_length,
),
),
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array(
'content_type' => '',
'import_period' => FEEDS_SCHEDULE_NEVER,
));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'title',
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 1 node');
$this->assertText('Failed importing 1 node.');
$this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
$this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
}
/**
* Tests text field validation for nodes without a title.
*
* The error message should refer the GUID of an item if the entity has no
* label.
*/
public function testTextFieldValidationWithoutNodeTitle() {
// Create a field with settings to validate.
$max_length = 5;
$typename = $this->createContentType(array(), array(
'alpha' => array(
'type' => 'text',
'settings' => array(
'field[settings][max_length]' => $max_length,
),
),
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array(
'content_type' => '',
'import_period' => FEEDS_SCHEDULE_NEVER,
));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
$this->addMappings('csv', array(
0 => array(
'source' => 'guid',
'target' => 'guid',
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 1 node');
$this->assertText('Failed importing 1 node.');
$this->assertText("Field validation errors in item '2'");
$this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
}
/**
* Tests text field validation for taxonomy terms.
*/
public function testTextFieldValidationForTaxonomyTerms() {
// Create vocabulary.
$edit = array(
'name' => 'Addams vocabulary',
'machine_name' => 'addams',
);
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
// Create a field with settings to validate.
field_create_field(array(
'field_name' => 'field_alpha',
'type' => 'text',
'settings' => array(
'max_length' => 5,
),
));
field_create_instance(array(
'field_name' => 'field_alpha',
'entity_type' => 'taxonomy_term',
'label' => 'alpha_text_label',
'bundle' => 'addams',
'widget' => array(
'type' => 'textfield',
),
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array(
'content_type' => '',
'import_period' => FEEDS_SCHEDULE_NEVER,
));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setPlugin('csv', 'FeedsTermProcessor');
$this->setSettings('csv', 'FeedsTermProcessor', array('bundle' => 'addams'));
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'name',
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 1 taxonomy term.');
$this->assertText('Failed importing 1 taxonomy term.');
$this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
$this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
}
/**
* Tests text field validation for users.
*/
public function testTextFieldValidationForUsers() {
// Create a field with settings to validate.
field_create_field(array(
'field_name' => 'field_alpha',
'type' => 'text',
'settings' => array(
'max_length' => 5,
),
));
field_create_instance(array(
'field_name' => 'field_alpha',
'entity_type' => 'user',
'label' => 'alpha_text_label',
'bundle' => 'user',
'widget' => array(
'type' => 'textfield',
),
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array(
'content_type' => '',
'import_period' => FEEDS_SCHEDULE_NEVER,
));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setPlugin('csv', 'FeedsUserProcessor');
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'name',
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha',
),
));
// Import CSV file. Users should fail to import because mail address is
// missing.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Failed importing 2 users.');
$this->assertText('User name missing or email not valid.');
$this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
$this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
}
/**
* Tests cardinality validation.
*/
public function testCardinalityValidation() {
// Create a field with settings to validate.
$cardinality = 6;
$typename = $this->createContentType(array(), array(
'alpha' => array(
'type' => 'text',
'instance_settings' => array(
'field[cardinality]' => $cardinality,
),
),
));
// Create and configure importer.
$this->createImporterConfiguration('Syndication', 'syndication');
$this->setSettings('syndication', NULL, array(
'content_type' => '',
'import_period' => FEEDS_SCHEDULE_NEVER,
));
$this->setPlugin('syndication', 'FeedsHTTPFetcher');
$this->setPlugin('syndication', 'FeedsSyndicationParser');
$this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
$this->addMappings('syndication', array(
0 => array(
'source' => 'title',
'target' => 'title',
),
1 => array(
'source' => 'tags',
'target' => 'field_alpha',
),
));
// Import RSS.
// Three items of the RSS contain more than 6 categories. Because categories
// are mapped to field_alpha and field_alpha may only hold 6 values, they
// should fail to import.
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
);
$this->drupalPost('import/syndication', $edit, 'Import');
$this->assertText('Created 7 nodes');
$this->assertText('Failed importing 3 nodes.');
$this->assertText("Field validation errors in item 'Open Atrium Translation Workflow: Two Way Translation Updates'");
$this->assertText("Field validation errors in item 'Mapping Innovation at the World Bank with Open Atrium'");
$this->assertText("Field validation errors in item 'Open Data for Microfinance: The New MIXMarket.org'");
$this->assertText('alpha_text_label: this field cannot hold more than 6 values.');
}