Skip to content
Snippets Groups Projects
feeds_mapper_multilingual_fields.test 35.7 KiB
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 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 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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
<?php

/**
 * @file
 * Contains FeedsMapperMultilingualFieldsTestCase.
 */

/**
 * Tests field mapping with multiple languages.
 */
class FeedsMapperMultilingualFieldsTestCase extends FeedsMapperTestCase {

  /**
   * Name of created content type.
   *
   * @var string
   */
  private $contentType;

  /**
   * @var array
   */
  protected $fields = array();

  public static function getInfo() {
    return array(
      'name' => 'Mapper: Multilingual fields',
      'description' => 'Tests Feeds multilingual support.',
      'group' => 'Feeds',
      'dependencies' => array('date', 'entity_translation', 'i18n_taxonomy', 'link'),
    );
  }

  public function setUp() {
    $modules = array(
      'locale',
      'entity_translation',
      'date',
      'link',
      'list',
      'number',
    );

    $permissions = array(
      'administer entity translation',
      'translate any entity',
      'administer languages',
    );

    parent::setUp($modules, $permissions);

    // Include FeedsProcessor.inc so processor related constants are available.
    module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');

    // Add French language.
    $this->addLanguage('fr', 'French');

    // Add Categories vocabulary.
    $edit = array(
      'name' => 'Categories',
      'machine_name' => 'categories',
    );
    $this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');

    // Create content type.
    $this->fields = array(
      'date' => array(
        'type' => 'date',
        'settings' => array(
          'field[settings][granularity][hour]' => FALSE,
          'field[settings][granularity][minute]' => FALSE,
          'field[settings][tz_handling]' => 'none',
        ),
      ),
      'datestamp' => array(
        'type' => 'datestamp',
        'settings' => array(
          'field[settings][granularity][second]' => TRUE,
          'field[settings][tz_handling]' => 'utc',
        ),
      ),
      'datetime' => array(
        'type' => 'datetime',
        'settings' => array(
          'field[settings][granularity][second]' => TRUE,
          'field[settings][tz_handling]' => 'utc',
        ),
      ),
      'image' => array(
        'type' => 'image',
        'instance_settings' => array(
          'instance[settings][alt_field]' => 1,
          'instance[settings][title_field]' => 1,
        ),
      ),
      'link' => 'link_field',
      'list_boolean' => 'list_boolean',
      'number_integer' => 'number_integer',
      'number_decimal' => 'number_decimal',
      'number_float' => 'number_float',
      'text' => 'text',
    );
    $this->contentType = $this->createContentType(array(), $this->fields);

    // Create term reference field.
    $field = array(
      'field_name' => 'field_category',
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => 'categories',
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    // Add term reference field to article bundle.
    $this->instance = array(
      'field_name' => 'field_category',
      'bundle' => $this->contentType,
      'entity_type' => 'node',
      'widget' => array(
        'type' => 'taxonomy_autocomplete',
      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
        ),
      ),
    );
    field_create_instance($this->instance);

    // Make content type and fields multilingual.
    $field_names = array(
      'body',
      'field_category',
    );
    foreach ($this->fields as $field_name => $field_type) {
      $field_names[] = 'field_' . $field_name;
    }
    $this->setupMultilingual($this->contentType, $field_names);

    // Copy directory of source files, CSV file expects them in public://images.
    $this->copyDir($this->absolutePath() . '/tests/feeds/assets', 'public://images');

    // Create an importer configuration with basic mapping.
    $this->createImporterConfiguration('Test multilingual fields import from CSV', 'node');
    $this->setPlugin('node', 'FeedsCSVParser');
    $this->setPlugin('node', 'FeedsFileFetcher');
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'bundle' => $this->contentType,
      'language' => 'en',
    ));

    // Add language neutral mappings.
    $this->addMappings('node', array(
      0 => array(
        'source' => 'guid',
        'target' => 'guid',
        'unique' => 1,
      ),
      1 => array(
        'source' => 'title',
        'target' => 'title',
      ),
    ));
  }

  /**
   * Tests multilingual mappings to translatable fields (entity translation).
   */
  public function testMultilingualFieldMappings() {
    // Add English mappers.
    $index = 2;
    $mappings = $this->getMappingsInLanguage('en', $index);
    // Append "_en" to each source name.
    foreach ($mappings as &$mapping) {
      $mapping['source'] .= '_en';
    }
    $this->addMappings('node', $mappings);
    $index += count($mappings);

    // Add French mappers.
    $mappings = $this->getMappingsInLanguage('fr', $index);
    // Append "_fr" to each source name.
    foreach ($mappings as &$mapping) {
      $mapping['source'] .= '_fr';
    }
    $this->addMappings('node', $mappings);

    // Import file that has items with both English and French field values.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_en_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Load node.
    $node = node_load(1, NULL, TRUE);

    // Inspect availability of English values.
    $english = $this->getEnglishValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['en'][0]['tid'],
      ),
    );
    foreach ($english as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The English field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }

    // Inspect availability of French values.
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 2,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The French field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if values of fields in other languages are kept when not importing
   * in that language.
   */
  public function testChangedLanguageImport() {
    // Add Dutch language.
    $this->addLanguage('nl', 'Dutch');

    // Import an item first in the Dutch language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'nl',
    ));
    $mappings = $this->getMappingsInLanguage('nl', 2);
    $this->addMappings('node', $mappings);
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_nl.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that Dutch values were created.
    $node = node_load(1, NULL, TRUE);
    $dutch = $this->getDutchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['nl'][0]['tid'],
      ),
    );
    foreach ($dutch as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The Dutch field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }

    // Set import to update existing nodes.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'update_existing' => FEEDS_UPDATE_EXISTING,
    ));

    // Change mappers language to French.
    $path = 'admin/structure/feeds/node/mapping';
    foreach ($mappings as $i => $mapping) {
      $this->drupalPostAJAX($path, array(), 'mapping_settings_edit_' . $i);
      $edit = array("config[$i][settings][field_language]" => 'fr');
      $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
      $this->drupalPost(NULL, array(), t('Save'));
    }
    // Import French item.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Updated 1 node'));

    // Assert that French values were created.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 2,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The French field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }

    // Assert that Dutch values still exist.
    $dutch = $this->getDutchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['nl'][0]['tid'],
      ),
    );
    foreach ($dutch as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The Dutch field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if values of fields in other languages are kept when not importing
   * in that language for nodes that were not created by Feeds.
   */
  public function testChangedLanguageImportForExistingNode() {
    // Add Dutch language.
    $this->addLanguage('nl', 'Dutch');

    // Date settings.
    foreach (array('datestamp', 'datetime') as $field) {
      $field = 'field_' . $field;
      $edit = array(
        'field[settings][granularity][second]' => 1,
      );
      $this->drupalPost('admin/structure/types/manage/' . $this->contentType . '/fields/' . $field . '/field-settings', $edit, 'Save field settings');
    }

    // Hack to get date fields to not round to every 15 seconds.
    foreach (array('date', 'datestamp', 'datetime') as $field) {
      $field = 'field_' . $field;
      $edit = array(
        'widget_type' => 'date_select',
      );
      $this->drupalPost('admin/structure/types/manage/' . $this->contentType . '/fields/' . $field . '/widget-type', $edit, 'Continue');
      $edit = array(
        'instance[widget][settings][increment]' => 1,
        'field[settings][enddate_get]' => 1,
      );
      $this->drupalPost('admin/structure/types/manage/' . $this->contentType . '/fields/' . $field, $edit, 'Save settings');
      $edit = array(
        'widget_type' => 'date_text',
      );
      $this->drupalPost('admin/structure/types/manage/' . $this->contentType . '/fields/' . $field . '/widget-type', $edit, 'Continue');
    }

    // Create a node with Dutch values.
    $edit = array(
      'title' => 'Teste Feeds Multilingue 1',
      'body[und][0][value]' => 'Dit is de berichttekst',
      'field_date[und][0][value][date]' => '07/29/1985',
      'field_datestamp[und][0][value][date]' => '07/29/1985 - 04:48:12',
      'field_datetime[und][0][value][date]' => '07/29/1985 - 04:48:12',
      'field_link[und][0][url]' => 'http://google.nl',
      'field_list_boolean[und]' => '1',
      'field_number_decimal[und][0][value]' => '30.3',
      'field_number_float[und][0][value]' => '30.2795',
      'field_number_integer[und][0][value]' => '30',
      'field_text[und][0][value]' => 'Wortelen',
      'files[field_image_und_0]' => drupal_realpath('public://images/attersee.jpeg'),
      'field_category[und]' => 'Nieuws',
      'language' => 'nl',
    );
    $this->drupalPost('node/add/' . $this->contentType, $edit, t('Save'));
    // Add alt/title to the image.
    $edit = array(
      'field_image[nl][0][alt]' => 'Bij het zien',
      'field_image[nl][0][title]' => 'Bij het zien van de groene vloeistof',
    );
    $this->drupalPost('node/1/edit/nl', $edit, t('Save'));
    $this->drupalGet('node/1/edit/nl');

    // Assert that the Dutch values were put in as expected.
    $node = node_load(1, NULL, TRUE);
    $dutch = $this->getDutchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['nl'][0]['tid'],
      ),
    );
    foreach ($dutch as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The Dutch field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }

    // Change unique target from guid (0) to title (1).
    $path = 'admin/structure/feeds/node/mapping';
    $this->drupalPostAJAX($path, array(), 'mapping_settings_edit_0');
    $edit = array("config[0][settings][unique]" => FALSE);
    $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_0');
    $this->drupalPost(NULL, array(), t('Save'));
    $this->drupalPostAJAX($path, array(), 'mapping_settings_edit_1');
    $edit = array("config[1][settings][unique]" => 1);
    $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_1');
    $this->drupalPost(NULL, array(), t('Save'));

    // Update this item with Feeds.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'update_existing' => FEEDS_UPDATE_EXISTING,
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Updated 1 node'));

    // Assert that French values were created.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 2,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The French field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }

    // Assert that Dutch values still exist.
    $dutch = $this->getDutchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['nl'][0]['tid'],
      ),
    );
    foreach ($dutch as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The Dutch field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if fields still are imported in their language when the
   * entity_translation module gets disabled.
   *
   * The entity_translation module is mainly an UI module for configuring field
   * language and disabling that module should not have effect on importing
   * values in a specific language for fields.
   */
  public function testWithDisabledEntityTranslationModule() {
    module_disable(array('entity_translation'));
    // Make sure that entity info is reset.
    drupal_flush_all_caches();
    drupal_static_reset();

    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));

    // Import content.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that the fields were all created in French.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if fields are still imported in their language when the
   * entity_translation module gets uninstalled.
   *
   * @see testWithDisabledEntityTranslationModule()
   */
  public function testWithUninstalledEntityTranslationModule() {
    module_disable(array('entity_translation'));
    drupal_uninstall_modules(array('entity_translation'));
    // Make sure that entity info is reset.
    drupal_flush_all_caches();
    drupal_static_reset();

    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));

    // Import content.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that the fields were all created in French.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if fields are imported in LANGUAGE_NONE if the field's language gets
   * disabled after configuring.
   */
  public function testDisabledLanguage() {
    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));

    // Now disable the French language.
    $path = 'admin/config/regional/language';
    $edit = array(
      'enabled[fr]' => FALSE,
    );
    $this->drupalPost($path, $edit, t('Save configuration'));
    // Reset static cache to update the available languages.
    drupal_static_reset();

    // Ensure no error messages are shown on the mappings page.
    $this->drupalGet('admin/structure/feeds/node/mapping');

    // Import content.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that the fields were all created in LANGUAGE_NONE.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node, LANGUAGE_NONE) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category[LANGUAGE_NONE][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if fields are imported in LANGUAGE_NONE if the field's language gets
   * removed after configuring.
   */
  public function testRemovedLanguage() {
    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));

    // Now remove the French language.
    $path = 'admin/config/regional/language/delete/fr';
    $this->drupalPost($path, array(), t('Delete'));
    // Reset static cache to update the available languages.
    drupal_static_reset();

    // Import content.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that the fields were all created in LANGUAGE_NONE.
    $node = node_load(1, NULL, TRUE);
    $french = $this->getFrenchValues($node, LANGUAGE_NONE) + array(
      'field_category' => array(
        'expected' => 1,
        'actual' => $node->field_category[LANGUAGE_NONE][0]['tid'],
      ),
    );
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if autocreated terms are in the language that was set on the target configuration
   * in case the taxonomy is multilingual.
   */
  public function testAutocreatedTermLanguage() {
    module_enable(array('i18n_taxonomy'));
    // Make sure that entity info is reset.
    drupal_flush_all_caches();
    drupal_static_reset();

    // Enable multilingual taxonomy.
    $edit = array('i18n_mode' => 4);
    $this->drupalPost('admin/structure/taxonomy/categories/edit', $edit, 'Save');

    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', array(
      2 => array(
        'source' => 'term',
        'target' => 'field_category',
        'autocreate' => TRUE,
        'field_language' => 'fr',
      ),
    ));

    // Import French item.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Assert that the created term is in the French language.
    $term = taxonomy_term_load(1);
    $this->assertEqual('fr', entity_language('taxonomy_term', $term));
  }

  /**
   * Tests if values are cleared out when an empty value or no value is
   * provided.
   */
  public function testClearOutValues() {
    // Set to update existing nodes.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'update_existing' => FEEDS_UPDATE_EXISTING,
    ));

    // Add English mappers.
    $index = 2;
    $mappings = $this->getMappingsInLanguage('en', $index);
    // Append "_en" to each source name.
    foreach ($mappings as &$mapping) {
      $mapping['source'] .= '_en';
    }
    $this->addMappings('node', $mappings);
    $index += count($mappings);

    // Add French mappers.
    $mappings = $this->getMappingsInLanguage('fr', $index);
    // Append "_fr" to each source name.
    foreach ($mappings as &$mapping) {
      $mapping['source'] .= '_fr';
    }
    $this->addMappings('node', $mappings);

    // Import file that has items with both English and French field values.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_en_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Now import a file where the French remained, but the English values were
    // removed.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_en_fr_empty.csv');
    $this->assertText(t('Updated 1 node'));

    // Load node.
    $node = node_load(1, NULL, TRUE);

    // Check that the English values are gone, but the French values are still
    // there.
    $fields = array(
      'body',
      'field_date',
      'field_datestamp',
      'field_datetime',
      'field_image',
      'field_link',
      'field_list_boolean',
      'field_number_decimal',
      'field_number_float',
      'field_number_integer',
      'field_category',
      'field_text',
    );
    foreach ($fields as $field_name) {
      $this->assertTrue(empty($node->{$field_name}['en']), format_string('The field %field is empty.', array('%field' => $field_name)));
    }

    // Inspect availability of French values.
    $french = $this->getFrenchValues($node) + array(
      'field_category' => array(
        'expected' => 2,
        'actual' => $node->field_category['fr'][0]['tid'],
      ),
    );
    // Since the image was placed on the node again, its file name is now
    // "la fayette_0.jpeg."
    $french['field_image']['expected'] = 'la fayette_0.jpeg';
    foreach ($french as $field_name => $value) {
      $this->assertEqual($value['expected'], $value['actual'], format_string('The French field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
    }
  }

  /**
   * Tests if values are cleared out when an empty value is provided for a
   * language that got disabled.
   */
  public function testClearOutValuesWithDisabledLanguage() {
    // Set to update existing nodes.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'update_existing' => FEEDS_UPDATE_EXISTING,
    ));

    // Configure importer to import in French language.
    $this->setSettings('node', 'FeedsNodeProcessor', array(
      'language' => 'fr',
    ));
    $this->addMappings('node', $this->getMappingsInLanguage('fr'));

    // Now disable the French language.
    $path = 'admin/config/regional/language';
    $edit = array(
      'enabled[fr]' => FALSE,
    );
    $this->drupalPost($path, $edit, t('Save configuration'));

    // Ensure no error messages are shown on the mappings page.
    $this->drupalGet('admin/structure/feeds/node/mapping');

    // Import content. Since the French language was disabled, the content
    // should be imported as LANGUAGE_NONE.
    // @see ::testDisabledLanguage()
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_fr.csv');
    $this->assertText(t('Created 1 node'));

    // Now import a file with empty values.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/multilingual_empty.csv');
    $this->assertText(t('Updated 1 node'));

    // Load node.
    $node = node_load(1, NULL, TRUE);

    // Check that the values in LANGUAGE_NONE are gone.
    $fields = array(
      'body',
      'field_date',
      'field_datestamp',
      'field_datetime',
      'field_image',
      'field_link',
      'field_list_boolean',
      'field_number_decimal',
      'field_number_float',
      'field_number_integer',
      'field_category',
      'field_text',
    );
    foreach ($fields as $field_name) {
      $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE]), format_string('The field %field is empty.', array('%field' => $field_name)));
    }
  }

  /**
   * Adds a language to test with.
   *
   * @param string $langcode
   *   The language's langcode.
   * @param string $label
   *   The language human readable name.
   */
  protected function addLanguage($langcode, $label) {
    $edit = array(
      'langcode' => $langcode,
    );
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
    $this->assertText(format_string('The language @language has been created and can now be used.', array('@language' => $label)));
  }

  /**
   * Sets given content type and fields to be translatable.
   *
   * @param string $typename
   *   The machine name of the node type.
   * @param array $field_names
   *   The fields to enable multilingual support for.
   */
  protected function setupMultilingual($typename, array $field_names) {
    // Enable entity field translation for content type.
    $edit = array(
      'language_content_type' => 4,
      'entity_translation_hide_translation_links' => 1,
      'entity_translation_node_metadata' => 0,
    );
    $this->drupalPost('admin/structure/types/manage/' . $typename, $edit, t('Save content type'));

    // Enable field translation on fields.
    $edit = array(
      'field[translatable]' => 1,
    );
    foreach ($field_names as $field_name) {
      $this->drupalPost("admin/structure/types/manage/{$typename}/fields/{$field_name}", $edit, t('Save settings'));
    }

    // Reset static cache so that all languages are available when
    // field_available_languages() is called during node_load().
    drupal_static_reset();
  }

  /**
   * Adds mappings for each field in specified language.
   *
   * @param string $langcode
   *   The code of the desired language.
   * @param int $start
   *   The index number to start the array with. This must be
   *   specified in order to add mappings to the right index when
   *   calling FeedsWebTestCase::addMappings().
   */
  protected function getMappingsInLanguage($langcode, $start = 2) {
    $mappings = array(
      $start => array(
        'source' => 'body',
        'target' => 'body',
      ),
      array(
        'source' => 'date',
        'target' => 'field_date:start',
      ),
      array(
        'source' => 'datestamp',
        'target' => 'field_datestamp:start',
      ),
      array(
        'source' => 'datetime',
        'target' => 'field_datetime:start',
      ),
      array(
        'source' => 'image',
        'target' => 'field_image:uri',
      ),
      array(
        'source' => 'image_alt',
        'target' => 'field_image:alt',
      ),
      array(
        'source' => 'image_title',
        'target' => 'field_image:title',
      ),
      array(
        'source' => 'link',
        'target' => 'field_link:url',
      ),
      array(
        'source' => 'list_boolean',
        'target' => 'field_list_boolean',
      ),
      array(
        'source' => 'number_decimal',
        'target' => 'field_number_decimal',
      ),
      array(
        'source' => 'number_float',
        'target' => 'field_number_float',
      ),
      array(
        'source' => 'number_integer',
        'target' => 'field_number_integer',
      ),
      array(
        'source' => 'term',
        'target' => 'field_category',
        'autocreate' => TRUE,
      ),
      array(
        'source' => 'text',
        'target' => 'field_text',
      ),
    );
    foreach ($mappings as &$mapping) {
      $mapping['field_language'] = $langcode;
    }
    return $mappings;
  }

  /**
   * Returns expected and actual values of given node for the Dutch language.
   *
   * @param object $node
   *   The multilingual node.
   * @param string $langcode
   *   The used language code.
   *
   * @return array
   *   The expected and actual Dutch values.
   */
  protected function getDutchValues($node, $langcode = 'nl') {
    return array(
      'body' => array(
        'expected' => 'Dit is de berichttekst',
        'actual' => $node->body[$langcode][0]['value'],
      ),
      'field_date' => array(
        'expected' => '1985-07-29T00:00:00',
        'actual' => $node->field_date[$langcode][0]['value'],
      ),
      'field_datestamp' => array(
        'expected' => '491460492',
        'actual' => $node->field_datestamp[$langcode][0]['value'],
      ),
      'field_datetime' => array(
        'expected' => '1985-07-29 04:48:12',
        'actual' => $node->field_datetime[$langcode][0]['value'],
      ),
      'field_image' => array(
        'expected' => 'attersee.jpeg',
        'actual' => $node->field_image[$langcode][0]['filename'],
      ),
      'field_image:alt' => array(
        'expected' => 'Bij het zien',
        'actual' => $node->field_image[$langcode][0]['alt'],
      ),
      'field_image:title' => array(
        'expected' => 'Bij het zien van de groene vloeistof',
        'actual' => $node->field_image[$langcode][0]['title'],
      ),
      'field_link' => array(
        'expected' => 'http://google.nl',
        'actual' => $node->field_link[$langcode][0]['url'],
      ),
      'field_list_boolean' => array(
        'expected' => '1',
        'actual' => $node->field_list_boolean[$langcode][0]['value'],
      ),
      'field_number_decimal' => array(
        'expected' => 30.3,
        'actual' => $node->field_number_decimal[$langcode][0]['value'],
      ),
      'field_number_float' => array(
        'expected' => 30.2795,
        'actual' => $node->field_number_float[$langcode][0]['value'],
      ),
      'field_number_integer' => array(
        'expected' => 30,
        'actual' => $node->field_number_integer[$langcode][0]['value'],
      ),
      'field_text' => array(
        'expected' => 'Wortelen',
        'actual' => $node->field_text[$langcode][0]['value'],
      ),
    );
  }

  /**
   * Returns expected and actual values of given node for the English language.
   *
   * @param object $node
   *   The multilingual node.
   * @param string $langcode
   *   The used language code.
   *
   * @return array
   *   The expected and actual English values.
   */
  protected function getEnglishValues($node, $langcode = 'en') {
    return array(
      'body' => array(
        'expected' => 'This is the body',
        'actual' => $node->body[$langcode][0]['value'],
      ),
      'field_date' => array(
        'expected' => '2015-10-21T00:00:00',
        'actual' => $node->field_date[$langcode][0]['value'],
      ),
      'field_datestamp' => array(
        'expected' => '1445470140',
        'actual' => $node->field_datestamp[$langcode][0]['value'],
      ),
      'field_datetime' => array(
        'expected' => '2015-10-21 23:29:00',
        'actual' => $node->field_datetime[$langcode][0]['value'],
      ),
      'field_image' => array(
        'expected' => 'foosball.jpeg',
        'actual' => $node->field_image[$langcode][0]['filename'],
      ),
      'field_image:alt' => array(
        'expected' => 'Foosball',
        'actual' => $node->field_image[$langcode][0]['alt'],
      ),
      'field_image:title' => array(
        'expected' => 'Foosball played by two guys',
        'actual' => $node->field_image[$langcode][0]['title'],
      ),
      'field_link' => array(
        'expected' => 'http://google.ca',
        'actual' => $node->field_link[$langcode][0]['url'],
      ),
      'field_list_boolean' => array(
        'expected' => '0',
        'actual' => $node->field_list_boolean[$langcode][0]['value'],
      ),
      'field_number_decimal' => array(
        'expected' => 4.2,
        'actual' => $node->field_number_decimal[$langcode][0]['value'],
      ),
      'field_number_float' => array(
        'expected' => 3.1416,
        'actual' => $node->field_number_float[$langcode][0]['value'],
      ),
      'field_number_integer' => array(
        'expected' => 1000,
        'actual' => $node->field_number_integer[$langcode][0]['value'],
      ),
      'field_text' => array(
        'expected' => 'Carrots',
        'actual' => $node->field_text[$langcode][0]['value'],
      ),
    );
  }

  /**
   * Returns expected and actual values of given node for the French language.
   *
   * @param object $node
   *   The multilingual node.
   * @param string $langcode
   *   The used language code.
   *
   * @return array
   *   The expected and actual French values.
   */
  protected function getFrenchValues($node, $langcode = 'fr') {
    return array(
      'body' => array(
        'expected' => 'Ceci est la corps',