Projet

Général

Profil

Paste
Télécharger (5,06 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for ParserCSV library.
6
 */
7

    
8
/**
9
 * Test aggregating a feed as node items.
10
 *
11
 * Using DrupalWebTestCase as DrupalUnitTestCase is broken in SimpleTest 2.8.
12
 * Not inheriting from Feeds base class as ParserCSV should be moved out of
13
 * Feeds at some time.
14
 */
15
class ParserCSVTest extends DrupalWebTestCase {
16
  protected $profile = 'testing';
17

    
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public static function getInfo() {
22
    return array(
23
      'name' => 'CSV Parser unit tests',
24
      'description' => 'Base level test for Feeds\' built in CSV parser.',
25
      'group' => 'Feeds',
26
    );
27
  }
28

    
29
  /**
30
   * Test method.
31
   */
32
  public function test() {
33
    drupal_load('module', 'feeds');
34
    feeds_include_library('ParserCSV.inc', 'ParserCSV');
35

    
36
    $this->_testSimple();
37
    $this->_testBatching();
38
    $this->_testEncodingConversion();
39
    $this->_testEncodingConversionFailure();
40
  }
41

    
42
  /**
43
   * Simple test of parsing functionality.
44
   */
45
  protected function _testSimple() {
46
    // Pull in the $control_result array.
47
    include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
48

    
49
    $delimiters = $this->getDelimiters();
50
    foreach ($delimiters as $delimiterType => $delimiter) {
51
      $file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
52
      $iterator = new ParserCSVIterator($file);
53
      $parser = new ParserCSV();
54
      $parser->setDelimiter($delimiter);
55
      $rows = $parser->parse($iterator);
56
      $this->assertFalse($parser->lastLinePos(), t('CSV reports all lines parsed, with delimiter: ') . $delimiterType);
57
      $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
58
    }
59
  }
60

    
61
  /**
62
   * Simple test of encoding conversion prior to parsing.
63
   */
64
  protected function _testEncodingConversion() {
65
    // Pull in the $control_result array.
66
    include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
67

    
68
    $encodings = $this->getEncodings();
69
    foreach ($encodings as $encoding) {
70
      $file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
71
      $iterator = new ParserCSVIterator($file);
72
      $parser = new ParserCSV();
73
      $parser->setDelimiter(',');
74
      $parser->setEncoding($encoding);
75
      $rows = $parser->parse($iterator);
76
      $this->assertFalse($parser->lastLinePos(), format_string('CSV reports all lines parsed, with encoding: %encoding', array('%encoding' => $encoding)));
77
      $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), 'Converted and parsed result matches control result.');
78
    }
79
  }
80

    
81
  /**
82
   * Simple test of failed encoding conversion prior to parsing.
83
   */
84
  protected function _testEncodingConversionFailure() {
85
    // Pull in the $control_result array.
86
    include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
87

    
88
    $encodings = $this->getEncodings();
89
    foreach ($encodings as $encoding) {
90
      $file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
91
      $iterator = new ParserCSVIterator($file);
92
      $parser = new ParserCSV();
93
      $parser->setDelimiter(',');
94
      // Attempt to read file as UTF-8.
95
      $parser->setEncoding('UTF-8');
96
      try {
97
        $rows = $parser->parse($iterator);
98
        $this->fail('Incorrect conversion attempt throws exception.');
99
      }
100
      catch (ParserCSVEncodingException $e) {
101
        $this->assertNotNull($e->getMessage(), 'Incorrect conversion attempt throws exception.');
102
      }
103
    }
104
  }
105

    
106
  /**
107
   * Test batching.
108
   */
109
  protected function _testBatching() {
110
    // Pull in the $control_result array.
111
    include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
112

    
113
    $delimiters = $this->getDelimiters();
114
    foreach ($delimiters as $delimiterType => $delimiter) {
115
      $file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
116
      // Set up parser with 2 lines to parse per call.
117
      $iterator = new ParserCSVIterator($file);
118
      $parser = new ParserCSV();
119
      $parser->setDelimiter($delimiter);
120
      $parser->setLineLimit(2);
121
      $rows = array();
122
      $pos = 0;
123

    
124
      // Call parser until all lines are parsed, then compare to control result.
125
      do {
126
        $parser->setStartByte($pos);
127
        $rows = array_merge($rows, $parser->parse($iterator));
128
        $pos = $parser->lastLinePos();
129
        $this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
130
      } while ($pos = $parser->lastLinePos());
131

    
132
      $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Batch parsed result matches control result for delimiter: ') . $delimiterType);
133
    }
134
  }
135

    
136
  /**
137
   * Absolute path to feeds.
138
   */
139
  public function absolutePath() {
140
    return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
141
  }
142

    
143
  /**
144
   * {@inheritdoc}
145
   */
146
  public static function getDelimiters() {
147
    return array(
148
      'comma' => ',',
149
      'pipe' => '|',
150
      'semicolon' => ';',
151
      'plus' => '+',
152
      'tab' => "\t",
153
    );
154
  }
155

    
156
  /**
157
   * {@inheritdoc}
158
   */
159
  public static function getEncodings() {
160
    return array(
161
      'SJIS-win',
162
      'SJIS',
163
    );
164
  }
165

    
166
}