feeds_parser_csv.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsCSVParserTestCase.
  5. */
  6. /**
  7. * Tests the CSV parser using the UI.
  8. */
  9. class FeedsCSVParserTestCase extends FeedsWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'CSV parser functional tests',
  13. 'description' => 'Tests the CSV parser using the UI.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Tests parsing a CSV when the mbstring extension is not available.
  19. */
  20. public function testMbstringExtensionDisabled() {
  21. // Set "feeds_use_mbstring" to FALSE to emulate that the mbstring extension
  22. // is not loaded.
  23. variable_set('feeds_use_mbstring', FALSE);
  24. // Remove items after parsing because in < PHP 5.4 processing items with
  25. // encoding issues leads to test failures because check_plain() can only
  26. // handle UTF-8 encoded strings.
  27. // @see feeds_tests_feeds_after_parse()
  28. variable_set('feeds_tests_feeds_after_parse_empty_items', TRUE);
  29. // Create node type.
  30. $node_type = $this->drupalCreateContentType();
  31. // Create and configure importer.
  32. $this->createImporterConfiguration('Content CSV', 'csv');
  33. $this->setPlugin('csv', 'FeedsFileFetcher');
  34. $this->setPlugin('csv', 'FeedsCSVParser');
  35. $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
  36. $this->addMappings('csv', array(
  37. 0 => array(
  38. 'source' => 'id',
  39. 'target' => 'guid',
  40. ),
  41. 1 => array(
  42. 'source' => 'text',
  43. 'target' => 'title',
  44. ),
  45. ));
  46. // Ensure that on the CSV parser settings page a message is shown about that
  47. // the mbstring extension is not available.
  48. $this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
  49. $this->assertNoField('encoding');
  50. $this->assertText('PHP mbstring extension must be available for character encoding conversion.');
  51. // Try to import a CSV file that is not UTF-8 encoded. No encoding warning
  52. // should be shown, but import should fail.
  53. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
  54. $this->assertNoText('Source file is not in UTF-8 encoding.');
  55. }
  56. /**
  57. * Tests an encoding failure during parsing a CSV.
  58. */
  59. public function testEncodingFailure() {
  60. // Create node type.
  61. $node_type = $this->drupalCreateContentType();
  62. // Create and configure importer.
  63. $this->createImporterConfiguration('Content CSV', 'csv');
  64. $this->setPlugin('csv', 'FeedsFileFetcher');
  65. $this->setPlugin('csv', 'FeedsCSVParser');
  66. $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
  67. $this->addMappings('csv', array(
  68. 0 => array(
  69. 'source' => 'id',
  70. 'target' => 'guid',
  71. ),
  72. 1 => array(
  73. 'source' => 'text',
  74. 'target' => 'title',
  75. ),
  76. ));
  77. // Ensure that on the CSV parser settings page a setting for encoding is
  78. // shown.
  79. $this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
  80. $this->assertField('encoding');
  81. $this->assertNoText('PHP mbstring extension must be available for character encoding conversion.');
  82. // Try to import a CSV file that is not UTF-8 encoded. Import should be
  83. // halted and an encoding warning should be shown.
  84. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
  85. $this->assertNoText('Failed importing 4 nodes.');
  86. $this->assertText('Source file is not in UTF-8 encoding.');
  87. }
  88. }