Révision ed9a13f1
Ajouté par Assos Assos il y a plus de 3 ans
drupal7/sites/all/modules/feeds/tests/feeds_mapper_taxonomy.test | ||
---|---|---|
10 | 10 |
*/ |
11 | 11 |
class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase { |
12 | 12 |
|
13 |
/** |
|
14 |
* {@inheritdoc} |
|
15 |
*/ |
|
13 | 16 |
public static function getInfo() { |
14 | 17 |
return array( |
15 | 18 |
'name' => 'Mapper: Taxonomy', |
... | ... | |
18 | 21 |
); |
19 | 22 |
} |
20 | 23 |
|
24 |
/** |
|
25 |
* {@inheritdoc} |
|
26 |
*/ |
|
21 | 27 |
public function setUp() { |
22 | 28 |
parent::setUp(); |
23 | 29 |
|
24 |
// Add Tags vocabulary |
|
30 |
// Add Tags vocabulary.
|
|
25 | 31 |
$edit = array( |
26 | 32 |
'name' => 'Tags', |
27 | 33 |
'machine_name' => 'tags', |
... | ... | |
29 | 35 |
$this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save'); |
30 | 36 |
|
31 | 37 |
$edit = array( |
32 |
'name' => 'term1',
|
|
33 |
);
|
|
38 |
'name' => 'term1', |
|
39 |
); |
|
34 | 40 |
$this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save')); |
35 | 41 |
$this->assertText('Created new term term1.'); |
36 | 42 |
|
... | ... | |
50 | 56 |
); |
51 | 57 |
field_create_field($field); |
52 | 58 |
|
53 |
// Add term reference field to feed item bundle. |
|
54 |
$this->instance = array( |
|
59 |
// Add a term reference field to the "article" node bundle. Tests use this |
|
60 |
// content type as "feed item": tests imports nodes of this type. |
|
61 |
$this->article_tags = array( |
|
55 | 62 |
'field_name' => 'field_tags', |
56 | 63 |
'bundle' => 'article', |
57 | 64 |
'entity_type' => 'node', |
... | ... | |
64 | 71 |
), |
65 | 72 |
), |
66 | 73 |
); |
67 |
field_create_instance($this->instance);
|
|
74 |
field_create_instance($this->article_tags);
|
|
68 | 75 |
|
69 |
// Add term reference field to feed node bundle. |
|
70 |
$this->instance = array( |
|
76 |
// Add a term reference field to the "page" node bundle. Tests use this |
|
77 |
// content type as "feed node type": this type is used to attach importers |
|
78 |
// to. |
|
79 |
$this->page_tags = array( |
|
71 | 80 |
'field_name' => 'field_tags', |
72 | 81 |
'bundle' => 'page', |
73 | 82 |
'entity_type' => 'node', |
... | ... | |
80 | 89 |
), |
81 | 90 |
), |
82 | 91 |
); |
83 |
field_create_instance($this->instance);
|
|
92 |
field_create_instance($this->page_tags);
|
|
84 | 93 |
|
85 | 94 |
// Create an importer configuration with basic mapping. |
86 | 95 |
$this->createImporterConfiguration('Syndication', 'syndication'); |
... | ... | |
116 | 125 |
* Tests inheriting taxonomy from the feed node. |
117 | 126 |
*/ |
118 | 127 |
public function testInheritTaxonomy() { |
119 |
// Adjust importer settings |
|
128 |
// Adjust importer settings.
|
|
120 | 129 |
$this->setSettings('syndication', NULL, array('import_period' => FEEDS_SCHEDULE_NEVER)); |
121 | 130 |
$this->setSettings('syndication', NULL, array('import_on_create' => FALSE)); |
122 | 131 |
$this->assertText('Do not import on submission'); |
... | ... | |
305 | 314 |
} |
306 | 315 |
|
307 | 316 |
/** |
308 |
* Tests importing empty values |
|
317 |
* Tests that only term references are added from allowed vocabularies. |
|
318 |
*/ |
|
319 |
public function testAllowedVocabularies() { |
|
320 |
// Create a second vocabulary. |
|
321 |
$vocabulary = new stdClass(); |
|
322 |
$vocabulary->name = 'Foo'; |
|
323 |
$vocabulary->machine_name = 'foo'; |
|
324 |
taxonomy_vocabulary_save($vocabulary); |
|
325 |
|
|
326 |
// Add a term to this vocabulary. |
|
327 |
$term1 = new stdClass(); |
|
328 |
$term1->name = 'Foo1'; |
|
329 |
$term1->vid = $vocabulary->vid; |
|
330 |
taxonomy_term_save($term1); |
|
331 |
|
|
332 |
// Add a term to the tags vocabulary. |
|
333 |
$term2 = new stdClass(); |
|
334 |
$term2->name = 'Bar1'; |
|
335 |
$term2->vid = 1; |
|
336 |
taxonomy_term_save($term2); |
|
337 |
|
|
338 |
FeedsPlugin::loadMappers(); |
|
339 |
|
|
340 |
$entity = new stdClass(); |
|
341 |
$target = 'field_tags'; |
|
342 |
$terms = array( |
|
343 |
'Foo1', |
|
344 |
'Bar1', |
|
345 |
); |
|
346 |
$mapping = array( |
|
347 |
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME, |
|
348 |
'language' => LANGUAGE_NONE, |
|
349 |
); |
|
350 |
|
|
351 |
$source = FeedsSource::instance('tmp', 0); |
|
352 |
|
|
353 |
taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping); |
|
354 |
|
|
355 |
// Assert that only the term 'Bar1' was set. |
|
356 |
$this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 1); |
|
357 |
$this->assertEqual($term2->tid, $entity->field_tags[LANGUAGE_NONE][0]['tid']); |
|
358 |
} |
|
359 |
|
|
360 |
/** |
|
361 |
* Tests how term caching works across multiple term reference fields. |
|
362 |
* |
|
363 |
* This specifically verifies that terms added to a vocabulary by one mapping |
|
364 |
* are available for use by other fields that are mapping to the same |
|
365 |
* vocabulary. |
|
366 |
* |
|
367 |
* @see https://www.drupal.org/project/feeds/issues/3091682 |
|
368 |
*/ |
|
369 |
public function testAutoCreateUpdatesAllCaches() { |
|
370 |
// Create a second term reference field base. |
|
371 |
$field = array( |
|
372 |
'field_name' => 'field_tags2', |
|
373 |
'type' => 'taxonomy_term_reference', |
|
374 |
'cardinality' => FIELD_CARDINALITY_UNLIMITED, |
|
375 |
'settings' => array( |
|
376 |
'allowed_values' => array( |
|
377 |
array( |
|
378 |
'vocabulary' => 'tags', |
|
379 |
'parent' => 0, |
|
380 |
), |
|
381 |
), |
|
382 |
), |
|
383 |
); |
|
384 |
field_create_field($field); |
|
385 |
|
|
386 |
// Add a second term reference field instance to to feed node bundle. |
|
387 |
$this->article_tags2 = array( |
|
388 |
'field_name' => 'field_tags2', |
|
389 |
'bundle' => 'article', |
|
390 |
'entity_type' => 'node', |
|
391 |
'widget' => array( |
|
392 |
'type' => 'options_select', |
|
393 |
), |
|
394 |
'display' => array( |
|
395 |
'default' => array( |
|
396 |
'type' => 'taxonomy_term_reference_link', |
|
397 |
), |
|
398 |
), |
|
399 |
); |
|
400 |
field_create_instance($this->article_tags2); |
|
401 |
|
|
402 |
// Create a CSV importer configuration. |
|
403 |
$this->createImporterConfiguration('Node import from CSV', 'node'); |
|
404 |
|
|
405 |
$this->setSettings('node', NULL, array( |
|
406 |
'content_type' => '', |
|
407 |
)); |
|
408 |
|
|
409 |
$this->setPlugin('node', 'FeedsFileFetcher'); |
|
410 |
$this->setPlugin('node', 'FeedsCSVParser'); |
|
411 |
|
|
412 |
$this->setSettings('node', 'FeedsNodeProcessor', array( |
|
413 |
'bundle' => 'article', |
|
414 |
'update_existing' => TRUE, |
|
415 |
)); |
|
416 |
|
|
417 |
$this->addMappings('node', array( |
|
418 |
0 => array( |
|
419 |
'source' => 'guid', |
|
420 |
'target' => 'guid', |
|
421 |
'unique' => TRUE, |
|
422 |
), |
|
423 |
1 => array( |
|
424 |
'source' => 'title', |
|
425 |
'target' => 'title', |
|
426 |
), |
|
427 |
2 => array( |
|
428 |
'source' => 'tag1', |
|
429 |
'target' => 'field_tags', |
|
430 |
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME, |
|
431 |
'autocreate' => TRUE, |
|
432 |
), |
|
433 |
3 => array( |
|
434 |
'source' => 'tag2', |
|
435 |
'target' => 'field_tags2', |
|
436 |
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME, |
|
437 |
'autocreate' => TRUE, |
|
438 |
), |
|
439 |
)); |
|
440 |
|
|
441 |
$this->importFile( |
|
442 |
'node', |
|
443 |
$this->absolutePath() . '/tests/feeds/taxonomy_multiple_tag_fields.csv'); |
|
444 |
|
|
445 |
$this->assertText('Created 2 nodes'); |
|
446 |
|
|
447 |
$term_names = array( |
|
448 |
'Alpha', |
|
449 |
'Beta', |
|
450 |
'Kiwi', |
|
451 |
); |
|
452 |
|
|
453 |
foreach ($term_names as $term_name) { |
|
454 |
$loaded_term = taxonomy_get_term_by_name($term_name); |
|
455 |
$terms[$term_name] = reset($loaded_term); |
|
456 |
} |
|
457 |
|
|
458 |
$expected_node_values = array( |
|
459 |
1 => array( |
|
460 |
'field_tags' => $terms['Alpha']->tid, |
|
461 |
'field_tags2' => $terms['Beta']->tid, |
|
462 |
), |
|
463 |
2 => array( |
|
464 |
'field_tags' => $terms['Kiwi']->tid, |
|
465 |
'field_tags2' => $terms['Kiwi']->tid, |
|
466 |
), |
|
467 |
); |
|
468 |
|
|
469 |
foreach ($expected_node_values as $nid => $node_values) { |
|
470 |
$this->drupalGet("node/{$nid}/edit"); |
|
471 |
|
|
472 |
foreach ($node_values as $field_name => $field_value) { |
|
473 |
$this->assertFieldByName("{$field_name}[und][]", $field_value); |
|
474 |
} |
|
475 |
} |
|
476 |
} |
|
477 |
|
|
478 |
/** |
|
479 |
* Tests if terms can be mapped when term access modules are enabled. |
|
480 |
*/ |
|
481 |
public function testTermAccess() { |
|
482 |
FeedsPlugin::loadMappers(); |
|
483 |
|
|
484 |
// Set acting user. |
|
485 |
// @see feeds_tests_query_term_access_alter() |
|
486 |
variable_set('feeds_tests_term_reference_allowed_user', $this->admin_user->uid); |
|
487 |
|
|
488 |
// Set to import using cron. |
|
489 |
$this->setSettings('syndication', NULL, array( |
|
490 |
'import_period' => 0, |
|
491 |
'import_on_create' => FALSE, |
|
492 |
'process_in_background' => TRUE, |
|
493 |
)); |
|
494 |
|
|
495 |
// Add target to taxonomy reference field. |
|
496 |
$this->addMappings('syndication', array( |
|
497 |
5 => array( |
|
498 |
'source' => 'tags', |
|
499 |
'target' => 'field_tags', |
|
500 |
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME, |
|
501 |
), |
|
502 |
)); |
|
503 |
|
|
504 |
// Create a few terms used in developmentseed.rss2. |
|
505 |
$term1 = new stdClass(); |
|
506 |
$term1->name = 'Drupal'; |
|
507 |
$term1->vid = 1; |
|
508 |
taxonomy_term_save($term1); |
|
509 |
$term2 = new stdClass(); |
|
510 |
$term2->name = 'translation'; |
|
511 |
$term2->vid = 1; |
|
512 |
taxonomy_term_save($term2); |
|
513 |
|
|
514 |
// Create feed node and initiate import. |
|
515 |
$nid = $this->createFeedNode('syndication', NULL, 'Syndication'); |
|
516 |
|
|
517 |
// Log out to ensure cron is ran as anonymous user. |
|
518 |
$this->drupalLogout(); |
|
519 |
// Run cron to run the import and assert 11 created nodes in total. |
|
520 |
$this->cronRun(); |
|
521 |
$node_count = db_select('node') |
|
522 |
->fields('node', array('nid')) |
|
523 |
->countQuery() |
|
524 |
->execute() |
|
525 |
->fetchField(); |
|
526 |
$this->assertEqual(11, $node_count, format_string('There are @expected nodes (actual: @actual).', array( |
|
527 |
'@expected' => 11, |
|
528 |
'@actual' => $node_count, |
|
529 |
))); |
|
530 |
|
|
531 |
// Assert that node 2 got two terms assigned. |
|
532 |
$node = node_load(2); |
|
533 |
$this->assertEqual($term1->tid, $node->field_tags[LANGUAGE_NONE][0]['tid']); |
|
534 |
$this->assertEqual($term2->tid, $node->field_tags[LANGUAGE_NONE][1]['tid']); |
|
535 |
} |
|
536 |
|
|
537 |
/** |
|
538 |
* Tests importing empty values. |
|
309 | 539 |
*/ |
310 | 540 |
public function testBlankSourceValues() { |
311 | 541 |
// Create a CSV importer configuration. |
... | ... | |
336 | 566 |
$this->importFile('node', $this->absolutePath() . '/tests/feeds/taxonomy_empty_terms.csv'); |
337 | 567 |
$this->assertText('Created 5 nodes'); |
338 | 568 |
|
339 |
// Make sure only two terms were added |
|
569 |
// Make sure only two terms were added.
|
|
340 | 570 |
$names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol(); |
341 | 571 |
$this->assertEqual(count($names), 2, 'Found correct number of terms in the database.'); |
342 | 572 |
|
343 |
// Make sure the correct terms were created |
|
573 |
// Make sure the correct terms were created.
|
|
344 | 574 |
$terms = array( |
345 | 575 |
'term1', |
346 | 576 |
'0', |
Formats disponibles : Unified diff
Weekly update of contrib modules