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