parser_csv.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Tests for ParserCSV library.
  5. */
  6. /**
  7. * Test aggregating a feed as node items.
  8. *
  9. * Using DrupalWebTestCase as DrupalUnitTestCase is broken in SimpleTest 2.8.
  10. * Not inheriting from Feeds base class as ParserCSV should be moved out of
  11. * Feeds at some time.
  12. */
  13. class ParserCSVTest extends DrupalWebTestCase {
  14. protected $profile = 'testing';
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'CSV Parser unit tests',
  18. 'description' => 'Base level test for Feeds\' built in CSV parser.',
  19. 'group' => 'Feeds',
  20. );
  21. }
  22. /**
  23. * Test method.
  24. */
  25. public function test() {
  26. drupal_load('module', 'feeds');
  27. feeds_include_library('ParserCSV.inc', 'ParserCSV');
  28. $this->_testSimple();
  29. $this->_testBatching();
  30. $this->_testEncodingConversion();
  31. $this->_testEncodingConversionFailure();
  32. }
  33. /**
  34. * Simple test of parsing functionality.
  35. */
  36. protected function _testSimple() {
  37. // Pull in the $control_result array.
  38. include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
  39. $delimiters = $this->getDelimiters();
  40. foreach($delimiters as $delimiterType => $delimiter) {
  41. $file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
  42. $iterator = new ParserCSVIterator($file);
  43. $parser = new ParserCSV();
  44. $parser->setDelimiter($delimiter);
  45. $rows = $parser->parse($iterator);
  46. $this->assertFalse($parser->lastLinePos(), t('CSV reports all lines parsed, with delimiter: ') . $delimiterType);
  47. $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
  48. }
  49. }
  50. /**
  51. * Simple test of encoding conversion prior to parsing.
  52. */
  53. protected function _testEncodingConversion() {
  54. // Pull in the $control_result array.
  55. include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
  56. $encodings = $this->getEncodings();
  57. foreach ($encodings as $encoding) {
  58. $file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
  59. $iterator = new ParserCSVIterator($file);
  60. $parser = new ParserCSV();
  61. $parser->setDelimiter(',');
  62. $parser->setEncoding($encoding);
  63. $rows = $parser->parse($iterator);
  64. $this->assertFalse($parser->lastLinePos(), format_string('CSV reports all lines parsed, with encoding: %encoding', array('%encoding' => $encoding)));
  65. $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), 'Converted and parsed result matches control result.');
  66. }
  67. }
  68. /**
  69. * Simple test of failed encoding conversion prior to parsing.
  70. */
  71. protected function _testEncodingConversionFailure() {
  72. // Pull in the $control_result array.
  73. include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
  74. $encodings = $this->getEncodings();
  75. foreach ($encodings as $encoding) {
  76. $file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
  77. $iterator = new ParserCSVIterator($file);
  78. $parser = new ParserCSV();
  79. $parser->setDelimiter(',');
  80. // Attempt to read file as UTF-8.
  81. $parser->setEncoding('UTF-8');
  82. try {
  83. $rows = $parser->parse($iterator);
  84. $this->fail('Incorrect conversion attempt throws exception.');
  85. }
  86. catch (ParserCSVEncodingException $e) {
  87. $this->assertNotNull($e->getMessage(), 'Incorrect conversion attempt throws exception.');
  88. }
  89. }
  90. }
  91. /**
  92. * Test batching.
  93. */
  94. protected function _testBatching() {
  95. // Pull in the $control_result array
  96. include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
  97. $delimiters = $this->getDelimiters();
  98. foreach($delimiters as $delimiterType => $delimiter) {
  99. $file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
  100. // Set up parser with 2 lines to parse per call.
  101. $iterator = new ParserCSVIterator($file);
  102. $parser = new ParserCSV();
  103. $parser->setDelimiter($delimiter);
  104. $parser->setLineLimit(2);
  105. $rows = array();
  106. $pos = 0;
  107. // Call parser until all lines are parsed, then compare to control result.
  108. do {
  109. $parser->setStartByte($pos);
  110. $rows = array_merge($rows, $parser->parse($iterator));
  111. $pos = $parser->lastLinePos();
  112. $this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
  113. }
  114. while ($pos = $parser->lastLinePos());
  115. $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Batch parsed result matches control result for delimiter: ') . $delimiterType);
  116. }
  117. }
  118. /**
  119. * Absolute path to feeds.
  120. */
  121. public function absolutePath() {
  122. return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
  123. }
  124. static function getDelimiters() {
  125. return array(
  126. 'comma' => ',',
  127. 'pipe' => '|',
  128. 'semicolon' => ';',
  129. 'plus' => '+',
  130. 'tab' => "\t",
  131. );
  132. }
  133. static function getEncodings() {
  134. return array(
  135. 'SJIS-win',
  136. 'SJIS',
  137. );
  138. }
  139. }