contrib modules security updates
This commit is contained in:
@@ -32,48 +32,103 @@ class ParserCSVTest extends DrupalWebTestCase {
|
||||
|
||||
$this->_testSimple();
|
||||
$this->_testBatching();
|
||||
$this->_testEncodingConversion();
|
||||
$this->_testEncodingConversionFailure();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test of parsing functionality.
|
||||
*/
|
||||
protected function _testSimple() {
|
||||
$file = $this->absolutePath() . '/tests/feeds/nodes.csv';
|
||||
// Pull in the $control_result array.
|
||||
include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
|
||||
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter(',');
|
||||
$rows = $parser->parse($iterator);
|
||||
$this->assertFalse($parser->lastLinePos(), t('Parser reports all lines parsed'));
|
||||
$this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
|
||||
$delimiters = $this->getDelimiters();
|
||||
foreach($delimiters as $delimiterType => $delimiter) {
|
||||
$file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter($delimiter);
|
||||
$rows = $parser->parse($iterator);
|
||||
$this->assertFalse($parser->lastLinePos(), t('CSV reports all lines parsed, with delimiter: ') . $delimiterType);
|
||||
$this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test of encoding conversion prior to parsing.
|
||||
*/
|
||||
protected function _testEncodingConversion() {
|
||||
// Pull in the $control_result array.
|
||||
include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
|
||||
|
||||
$encodings = $this->getEncodings();
|
||||
foreach ($encodings as $encoding) {
|
||||
$file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter(',');
|
||||
$parser->setEncoding($encoding);
|
||||
$rows = $parser->parse($iterator);
|
||||
$this->assertFalse($parser->lastLinePos(), format_string('CSV reports all lines parsed, with encoding: %encoding', array('%encoding' => $encoding)));
|
||||
$this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), 'Converted and parsed result matches control result.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test of failed encoding conversion prior to parsing.
|
||||
*/
|
||||
protected function _testEncodingConversionFailure() {
|
||||
// Pull in the $control_result array.
|
||||
include $this->absolutePath() . '/tests/feeds/encoding.csv.php';
|
||||
|
||||
$encodings = $this->getEncodings();
|
||||
foreach ($encodings as $encoding) {
|
||||
$file = $this->absolutePath() . "/tests/feeds/encoding_{$encoding}.csv";
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter(',');
|
||||
// Attempt to read file as UTF-8.
|
||||
$parser->setEncoding('UTF-8');
|
||||
try {
|
||||
$rows = $parser->parse($iterator);
|
||||
$this->fail('Incorrect conversion attempt throws exception.');
|
||||
}
|
||||
catch (ParserCSVEncodingException $e) {
|
||||
$this->assertNotNull($e->getMessage(), 'Incorrect conversion attempt throws exception.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batching.
|
||||
*/
|
||||
protected function _testBatching() {
|
||||
$file = $this->absolutePath() . '/tests/feeds/nodes.csv';
|
||||
// Pull in the $control_result array
|
||||
include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
|
||||
|
||||
// Set up parser with 2 lines to parse per call.
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter(',');
|
||||
$parser->setLineLimit(2);
|
||||
$rows = array();
|
||||
$pos = 0;
|
||||
$delimiters = $this->getDelimiters();
|
||||
foreach($delimiters as $delimiterType => $delimiter) {
|
||||
$file = $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
|
||||
// Set up parser with 2 lines to parse per call.
|
||||
$iterator = new ParserCSVIterator($file);
|
||||
$parser = new ParserCSV();
|
||||
$parser->setDelimiter($delimiter);
|
||||
$parser->setLineLimit(2);
|
||||
$rows = array();
|
||||
$pos = 0;
|
||||
|
||||
// Call parser until all lines are parsed, then compare to control result.
|
||||
do {
|
||||
$parser->setStartByte($pos);
|
||||
$rows = array_merge($rows, $parser->parse($iterator));
|
||||
$pos = $parser->lastLinePos();
|
||||
$this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
|
||||
// Call parser until all lines are parsed, then compare to control result.
|
||||
do {
|
||||
$parser->setStartByte($pos);
|
||||
$rows = array_merge($rows, $parser->parse($iterator));
|
||||
$pos = $parser->lastLinePos();
|
||||
$this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
|
||||
}
|
||||
while ($pos = $parser->lastLinePos());
|
||||
|
||||
$this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Batch parsed result matches control result for delimiter: ') . $delimiterType);
|
||||
}
|
||||
while ($pos = $parser->lastLinePos());
|
||||
|
||||
$this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,4 +137,21 @@ class ParserCSVTest extends DrupalWebTestCase {
|
||||
public function absolutePath() {
|
||||
return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
|
||||
}
|
||||
|
||||
static function getDelimiters() {
|
||||
return array(
|
||||
'comma' => ',',
|
||||
'pipe' => '|',
|
||||
'semicolon' => ';',
|
||||
'plus' => '+',
|
||||
'tab' => "\t",
|
||||
);
|
||||
}
|
||||
|
||||
static function getEncodings() {
|
||||
return array(
|
||||
'SJIS-win',
|
||||
'SJIS',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user