Projet

Général

Profil

Paste
Télécharger (19,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper_field.test @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsMapperFieldTestCase.
6
 */
7

    
8
/**
9
 * Test case for simple CCK field mapper mappers/content.inc.
10
 */
11
class FeedsMapperFieldTestCase extends FeedsMapperTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Mapper: Fields',
19
      'description' => 'Test Feeds Mapper support for fields.',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp() {
28
    parent::setUp(array('number'));
29
  }
30

    
31
  /**
32
   * Basic test loading a double entry CSV file.
33
   */
34
  public function test() {
35
    // Create content type.
36
    $typename = $this->createContentType(array(), array(
37
      'alpha' => 'text',
38
      'beta' => 'number_integer',
39
      'gamma' => 'number_decimal',
40
      'delta' => 'number_float',
41
    ));
42

    
43
    // Create and configure importer.
44
    $this->createImporterConfiguration('Content CSV', 'csv');
45
    $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
46
    $this->setPlugin('csv', 'FeedsFileFetcher');
47
    $this->setPlugin('csv', 'FeedsCSVParser');
48
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
49
    $this->addMappings('csv', array(
50
      0 => array(
51
        'source' => 'title',
52
        'target' => 'title',
53
      ),
54
      1 => array(
55
        'source' => 'created',
56
        'target' => 'created',
57
      ),
58
      2 => array(
59
        'source' => 'body',
60
        'target' => 'body',
61
      ),
62
      3 => array(
63
        'source' => 'alpha',
64
        'target' => 'field_alpha',
65
      ),
66
      4 => array(
67
        'source' => 'beta',
68
        'target' => 'field_beta',
69
      ),
70
      5 => array(
71
        'source' => 'gamma',
72
        'target' => 'field_gamma',
73
      ),
74
      6 => array(
75
        'source' => 'delta',
76
        'target' => 'field_delta',
77
      ),
78
    ));
79

    
80
    // Import CSV file.
81
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
82
    $this->assertText('Created 2 nodes');
83

    
84
    // Check the two imported files.
85
    $this->drupalGet('node/1/edit');
86
    $this->assertNodeFieldValue('alpha', 'Lorem');
87
    $this->assertNodeFieldValue('beta', '42');
88
    $this->assertNodeFieldValue('gamma', '4.20');
89
    $this->assertNodeFieldValue('delta', '3.14159');
90

    
91
    $this->drupalGet('node/2/edit');
92
    $this->assertNodeFieldValue('alpha', 'Ut wisi');
93
    $this->assertNodeFieldValue('beta', '32');
94
    $this->assertNodeFieldValue('gamma', '1.20');
95
    $this->assertNodeFieldValue('delta', '5.62951');
96
  }
97

    
98
  /**
99
   * Tests if values are cleared out when an empty value is provided.
100
   */
101
  public function testClearOutValues() {
102
    // Create content type.
103
    $typename = $this->createContentType(array(), array(
104
      'alpha' => 'text',
105
      'beta' => 'number_integer',
106
      'gamma' => 'number_decimal',
107
      'delta' => 'number_float',
108
    ));
109

    
110
    // Create and configure importer.
111
    $this->createImporterConfiguration('Content CSV', 'csv');
112
    $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
113
    $this->setPlugin('csv', 'FeedsFileFetcher');
114
    $this->setPlugin('csv', 'FeedsCSVParser');
115
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename, 'update_existing' => 1));
116
    $this->addMappings('csv', array(
117
      array(
118
        'source' => 'guid',
119
        'target' => 'guid',
120
        'unique' => TRUE,
121
      ),
122
      array(
123
        'source' => 'title',
124
        'target' => 'title',
125
      ),
126
      array(
127
        'source' => 'created',
128
        'target' => 'created',
129
      ),
130
      array(
131
        'source' => 'body',
132
        'target' => 'body',
133
      ),
134
      array(
135
        'source' => 'alpha',
136
        'target' => 'field_alpha',
137
      ),
138
      array(
139
        'source' => 'beta',
140
        'target' => 'field_beta',
141
      ),
142
      array(
143
        'source' => 'gamma',
144
        'target' => 'field_gamma',
145
      ),
146
      array(
147
        'source' => 'delta',
148
        'target' => 'field_delta',
149
      ),
150
    ));
151

    
152
    // Import CSV file.
153
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
154
    $this->assertText('Created 2 nodes');
155

    
156
    // Check the two imported nodes.
157
    $this->drupalGet('node/1/edit');
158
    $this->assertNodeFieldValue('alpha', 'Lorem');
159
    $this->assertNodeFieldValue('beta', '42');
160
    $this->assertNodeFieldValue('gamma', '4.20');
161
    $this->assertNodeFieldValue('delta', '3.14159');
162
    $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.');
163
    $this->drupalGet('node/2/edit');
164
    $this->assertNodeFieldValue('alpha', 'Ut wisi');
165
    $this->assertNodeFieldValue('beta', '32');
166
    $this->assertNodeFieldValue('gamma', '1.20');
167
    $this->assertNodeFieldValue('delta', '5.62951');
168
    $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.');
169

    
170
    // Import CSV file with empty values.
171
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
172
    $this->assertText('Updated 2 nodes');
173

    
174
    // Check if all values were cleared out for node 1.
175
    $this->drupalGet('node/1/edit');
176
    $this->assertNoNodeFieldValue('alpha', 'Lorem');
177
    $this->assertNoNodeFieldValue('beta', '42');
178
    $this->assertNoNodeFieldValue('gamma', '4.20');
179
    $this->assertNoNodeFieldValue('delta', '3.14159');
180
    $this->assertFieldByName('body[und][0][value]', '');
181
    // Check if labels for fields that should be cleared out are not shown.
182
    $this->drupalGet('node/1');
183
    $this->assertNoText('alpha_text_label');
184
    $this->assertNoText('beta_number_integer_label');
185
    $this->assertNoText('gamma_number_decimal_label');
186
    $this->assertNoText('delta_number_float_label');
187

    
188
    // Check if zero's didn't cleared out values for node 2.
189
    $this->drupalGet('node/2/edit');
190
    $this->assertNodeFieldValue('alpha', 0);
191
    $this->assertNodeFieldValue('beta', 0);
192
    $this->assertNodeFieldValue('gamma', 0);
193
    $this->assertNodeFieldValue('delta', 0);
194
    $this->assertFieldByName('body[und][0][value]', 0);
195
    // Check if labels for fields of node 2 are still shown.
196
    $this->drupalGet('node/2');
197
    $this->assertText('alpha_text_label');
198
    $this->assertText('beta_number_integer_label');
199
    $this->assertText('gamma_number_decimal_label');
200
    $this->assertText('delta_number_float_label');
201

    
202
    // Re-import the first file again.
203
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
204
    $this->assertText('Updated 2 nodes');
205

    
206
    // Check if the two imported nodes have content again.
207
    $this->drupalGet('node/1/edit');
208
    $this->assertNodeFieldValue('alpha', 'Lorem');
209
    $this->assertNodeFieldValue('beta', '42');
210
    $this->assertNodeFieldValue('gamma', '4.20');
211
    $this->assertNodeFieldValue('delta', '3.14159');
212
    $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.');
213
    $this->drupalGet('node/2/edit');
214
    $this->assertNodeFieldValue('alpha', 'Ut wisi');
215
    $this->assertNodeFieldValue('beta', '32');
216
    $this->assertNodeFieldValue('gamma', '1.20');
217
    $this->assertNodeFieldValue('delta', '5.62951');
218
    $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.');
219

    
220
    // Import CSV file with non-existent values.
221
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
222
    $this->assertText('Updated 2 nodes');
223

    
224
    // Check if all values were cleared out for node 1.
225
    $this->drupalGet('node/1/edit');
226
    $this->assertNoNodeFieldValue('alpha', 'Lorem');
227
    $this->assertNoNodeFieldValue('beta', '42');
228
    $this->assertNoNodeFieldValue('gamma', '4.20');
229
    $this->assertNoNodeFieldValue('delta', '3.14159');
230
    $this->assertFieldByName('body[und][0][value]', '');
231
    // Check if labels for fields that should be cleared out are not shown.
232
    $this->drupalGet('node/1');
233
    $this->assertNoText('alpha_text_label');
234
    $this->assertNoText('beta_number_integer_label');
235
    $this->assertNoText('gamma_number_decimal_label');
236
    $this->assertNoText('delta_number_float_label');
237
  }
238

    
239
  /**
240
   * Tests text field validation.
241
   */
242
  public function testTextFieldValidation() {
243
    // Create a field with settings to validate.
244
    $max_length = 5;
245
    $typename = $this->createContentType(array(), array(
246
      'alpha' => array(
247
        'type' => 'text',
248
        'settings' => array(
249
          'field[settings][max_length]' => $max_length,
250
        ),
251
      ),
252
    ));
253

    
254
    // Create and configure importer.
255
    $this->createImporterConfiguration('Content CSV', 'csv');
256
    $this->setSettings('csv', NULL, array(
257
      'content_type' => '',
258
      'import_period' => FEEDS_SCHEDULE_NEVER,
259
    ));
260
    $this->setPlugin('csv', 'FeedsFileFetcher');
261
    $this->setPlugin('csv', 'FeedsCSVParser');
262
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
263
    $this->addMappings('csv', array(
264
      0 => array(
265
        'source' => 'title',
266
        'target' => 'title',
267
      ),
268
      1 => array(
269
        'source' => 'alpha',
270
        'target' => 'field_alpha',
271
      ),
272
    ));
273

    
274
    // Import CSV file.
275
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
276
    $this->assertText('Created 1 node');
277
    $this->assertText('Failed importing 1 node.');
278
    $this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
279
    $this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
280
  }
281

    
282
  /**
283
   * Tests integer field validation.
284
   */
285
  public function testIntegerFieldValidation() {
286
    // Create content type with a field with settings to validate.
287
    $typename = $this->createContentType(array(), array(
288
      'beta' => array(
289
        'type' => 'number_integer',
290
        'instance_settings' => array(
291
          'instance[settings][min]' => 30,
292
          'instance[settings][max]' => 40,
293
        ),
294
      ),
295
    ));
296

    
297
    // Create and configure importer.
298
    $this->createImporterConfiguration('CSV', 'csv');
299
    $this->setSettings('csv', NULL, array(
300
      'content_type' => '',
301
      'import_period' => FEEDS_SCHEDULE_NEVER,
302
    ));
303
    $this->setPlugin('csv', 'FeedsFileFetcher');
304
    $this->setPlugin('csv', 'FeedsCSVParser');
305
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
306
      'bundle' => $typename,
307
    ));
308
    $this->addMappings('csv', array(
309
      0 => array(
310
        'source' => 'title',
311
        'target' => 'title',
312
      ),
313
      1 => array(
314
        'source' => 'beta',
315
        'target' => 'field_beta',
316
      ),
317
    ));
318
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
319
    $this->assertText('Created 1 node');
320
    $this->assertText('Failed importing 1 node.');
321
    $this->assertText("Field validation errors in item 'Lorem ipsum'");
322
    $this->assertText('beta_number_integer_label: the value may be no greater than 40.');
323
  }
324

    
325
  /**
326
   * Tests text field validation for nodes without a title.
327
   *
328
   * The error message should refer the GUID of an item if the entity has no
329
   * label.
330
   */
331
  public function testTextFieldValidationWithoutNodeTitle() {
332
    // Create a field with settings to validate.
333
    $max_length = 5;
334
    $typename = $this->createContentType(array(), array(
335
      'alpha' => array(
336
        'type' => 'text',
337
        'settings' => array(
338
          'field[settings][max_length]' => $max_length,
339
        ),
340
      ),
341
    ));
342

    
343
    // Create and configure importer.
344
    $this->createImporterConfiguration('Content CSV', 'csv');
345
    $this->setSettings('csv', NULL, array(
346
      'content_type' => '',
347
      'import_period' => FEEDS_SCHEDULE_NEVER,
348
    ));
349
    $this->setPlugin('csv', 'FeedsFileFetcher');
350
    $this->setPlugin('csv', 'FeedsCSVParser');
351
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
352
    $this->addMappings('csv', array(
353
      0 => array(
354
        'source' => 'guid',
355
        'target' => 'guid',
356
      ),
357
      1 => array(
358
        'source' => 'alpha',
359
        'target' => 'field_alpha',
360
      ),
361
    ));
362

    
363
    // Import CSV file.
364
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
365
    $this->assertText('Created 1 node');
366
    $this->assertText('Failed importing 1 node.');
367
    $this->assertText("Field validation errors in item '2'");
368
    $this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
369
  }
370

    
371
  /**
372
   * Tests text field validation for taxonomy terms.
373
   */
374
  public function testTextFieldValidationForTaxonomyTerms() {
375
    // Create vocabulary.
376
    $edit = array(
377
      'name' => 'Addams vocabulary',
378
      'machine_name' => 'addams',
379
    );
380
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
381

    
382
    // Create a field with settings to validate.
383
    field_create_field(array(
384
      'field_name' => 'field_alpha',
385
      'type' => 'text',
386
      'settings' => array(
387
        'max_length' => 5,
388
      ),
389
    ));
390
    field_create_instance(array(
391
      'field_name' => 'field_alpha',
392
      'entity_type' => 'taxonomy_term',
393
      'label' => 'alpha_text_label',
394
      'bundle' => 'addams',
395
      'widget' => array(
396
        'type' => 'text_textfield',
397
      ),
398
    ));
399

    
400
    // Create and configure importer.
401
    $this->createImporterConfiguration('Content CSV', 'csv');
402
    $this->setSettings('csv', NULL, array(
403
      'content_type' => '',
404
      'import_period' => FEEDS_SCHEDULE_NEVER,
405
    ));
406
    $this->setPlugin('csv', 'FeedsFileFetcher');
407
    $this->setPlugin('csv', 'FeedsCSVParser');
408
    $this->setPlugin('csv', 'FeedsTermProcessor');
409
    $this->setSettings('csv', 'FeedsTermProcessor', array('bundle' => 'addams'));
410
    $this->addMappings('csv', array(
411
      0 => array(
412
        'source' => 'title',
413
        'target' => 'name',
414
      ),
415
      1 => array(
416
        'source' => 'alpha',
417
        'target' => 'field_alpha',
418
      ),
419
    ));
420

    
421
    // Import CSV file.
422
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
423
    $this->assertText('Created 1 taxonomy term.');
424
    $this->assertText('Failed importing 1 taxonomy term.');
425
    $this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
426
    $this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
427
  }
428

    
429
  /**
430
   * Tests text field validation for users.
431
   */
432
  public function testTextFieldValidationForUsers() {
433
    // Create a field with settings to validate.
434
    field_create_field(array(
435
      'field_name' => 'field_alpha',
436
      'type' => 'text',
437
      'settings' => array(
438
        'max_length' => 5,
439
      ),
440
    ));
441
    field_create_instance(array(
442
      'field_name' => 'field_alpha',
443
      'entity_type' => 'user',
444
      'label' => 'alpha_text_label',
445
      'bundle' => 'user',
446
      'widget' => array(
447
        'type' => 'text_textfield',
448
      ),
449
    ));
450

    
451
    // Create and configure importer.
452
    $this->createImporterConfiguration('Content CSV', 'csv');
453
    $this->setSettings('csv', NULL, array(
454
      'content_type' => '',
455
      'import_period' => FEEDS_SCHEDULE_NEVER,
456
    ));
457
    $this->setPlugin('csv', 'FeedsFileFetcher');
458
    $this->setPlugin('csv', 'FeedsCSVParser');
459
    $this->setPlugin('csv', 'FeedsUserProcessor');
460
    $this->addMappings('csv', array(
461
      0 => array(
462
        'source' => 'title',
463
        'target' => 'name',
464
      ),
465
      1 => array(
466
        'source' => 'alpha',
467
        'target' => 'field_alpha',
468
      ),
469
    ));
470

    
471
    // Import CSV file. Users should fail to import because mail address is
472
    // missing.
473
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
474
    $this->assertText('Failed importing 2 users.');
475
    $this->assertText('User name missing or email not valid.');
476
    $this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
477
    $this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
478
  }
479

    
480
  /**
481
   * Tests cardinality validation.
482
   */
483
  public function testCardinalityValidation() {
484
    // Create a field with settings to validate.
485
    $cardinality = 6;
486
    $typename = $this->createContentType(array(), array(
487
      'alpha' => array(
488
        'type' => 'text',
489
        'instance_settings' => array(
490
          'field[cardinality]' => $cardinality,
491
        ),
492
      ),
493
    ));
494

    
495
    // Create and configure importer.
496
    $this->createImporterConfiguration('Syndication', 'syndication');
497
    $this->setSettings('syndication', NULL, array(
498
      'content_type' => '',
499
      'import_period' => FEEDS_SCHEDULE_NEVER,
500
    ));
501
    $this->setPlugin('syndication', 'FeedsHTTPFetcher');
502
    $this->setPlugin('syndication', 'FeedsSyndicationParser');
503
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
504
    $this->addMappings('syndication', array(
505
      0 => array(
506
        'source' => 'title',
507
        'target' => 'title',
508
      ),
509
      1 => array(
510
        'source' => 'tags',
511
        'target' => 'field_alpha',
512
      ),
513
    ));
514

    
515
    // Import RSS.
516
    // Three items of the RSS contain more than 6 categories. Because categories
517
    // are mapped to field_alpha and field_alpha may only hold 6 values, they
518
    // should fail to import.
519
    $edit = array(
520
      'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
521
    );
522
    $this->drupalPost('import/syndication', $edit, 'Import');
523
    $this->assertText('Created 7 nodes');
524
    $this->assertText('Failed importing 3 nodes.');
525
    $this->assertText("Field validation errors in item 'Open Atrium Translation Workflow: Two Way Translation Updates'");
526
    $this->assertText("Field validation errors in item 'Mapping Innovation at the World Bank with Open Atrium'");
527
    $this->assertText("Field validation errors in item 'Open Data for Microfinance: The New MIXMarket.org'");
528
    $this->assertText('alpha_text_label: this field cannot hold more than 6 values.');
529
  }
530

    
531
  /**
532
   * Tests that Feeds ignores validation of fields not mapped to.
533
   */
534
  public function testIgnoreUnmappedFieldsValidation() {
535
    // Include FeedsProcessor.inc so processor related constants are available.
536
    module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
537

    
538
    // Create content type with a field with settings to validate.
539
    $typename = $this->createContentType(array(), array(
540
      'alpha' => 'text',
541
      'beta' => array(
542
        'type' => 'number_integer',
543
        'instance_settings' => array(
544
          'instance[settings][min]' => 30,
545
          'instance[settings][max]' => 50,
546
        ),
547
      ),
548
    ));
549

    
550
    // Create a node of this type with an out of range integer value.
551
    $this->drupalCreateNode(array(
552
      'type' => $typename,
553
      'title' => 'Lorem ipsum',
554
      'field_beta' => array(
555
        LANGUAGE_NONE => array(
556
          0 => array(
557
            'value' => 25,
558
          ),
559
        ),
560
      ),
561
    ));
562

    
563
    // Create and configure importer.
564
    $this->createImporterConfiguration('CSV', 'csv');
565
    $this->setSettings('csv', NULL, array(
566
      'content_type' => '',
567
      'import_period' => FEEDS_SCHEDULE_NEVER,
568
    ));
569
    $this->setPlugin('csv', 'FeedsFileFetcher');
570
    $this->setPlugin('csv', 'FeedsCSVParser');
571
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
572
      'bundle' => $typename,
573
      'update_existing' => FEEDS_UPDATE_EXISTING,
574
    ));
575
    $this->addMappings('csv', array(
576
      0 => array(
577
        'source' => 'title',
578
        'target' => 'title',
579
        'unique' => TRUE,
580
      ),
581
      1 => array(
582
        'source' => 'alpha',
583
        'target' => 'field_alpha',
584
      ),
585
    ));
586
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
587
    $this->assertText('Updated 1 node');
588
    $this->assertNoText('Field validation errors');
589
  }
590

    
591
}