View source
<?php
if (class_exists('TUnit')) {
class FeedsExXmlUnitTests extends FeedsExUnitTestBase {
protected $source;
public static function getInfo() {
return array(
'name' => 'XML parser unit tests',
'description' => 'Unit tests for FeedsExXml.',
'group' => 'Feeds EX',
);
}
public function setUp() {
parent::setUp();
require_once $this->moduleDir . '/src/FeedsExXml.inc';
$this->source = $this
->getMockFeedsSource();
}
public function testSimpleParsing() {
$parser = $this
->getParserInstance();
$contents = file_get_contents($this->moduleDir . '/tests/resources/test.xml');
$contents = str_replace('I am a description', chr(0) . 'I am a description' . chr(11), $contents);
$fetcher_result = new FeedsFetcherResult($contents);
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('I am a title' . $delta, $item['title']);
$this
->assertEqual('I am a description' . $delta, $item['description']);
}
}
public function testRaw() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
'raw' => TRUE,
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('I am a title' . $delta, $item['title']);
$this
->assertEqual('<description><text>I am a description' . $delta . '</text></description>', $item['description']);
}
}
public function testInner() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
'raw' => TRUE,
'inner' => TRUE,
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('I am a title' . $delta, $item['title']);
$this
->assertEqual('<text>I am a description' . $delta . '</text>', $item['description']);
}
}
public function testCp866Encoded() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_ru.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('Я название' . $delta, $item['title']);
$this
->assertEqual('Я описание' . $delta, $item['description']);
}
}
public function testEucJpEncodedNoDeclaration() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_jp.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
'source_encoding' => array(
'EUC-JP',
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('私はタイトルです' . $delta, $item['title']);
$this
->assertEqual('私が説明してい' . $delta, $item['description']);
}
}
public function testBatching() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
$this
->variableSet('feeds_process_limit', 1);
foreach (range(0, 2) as $delta) {
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 1);
$this
->assertEqual('I am a title' . $delta, $result->items[0]['title']);
$this
->assertEqual('I am a description' . $delta, $result->items[0]['description']);
}
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 0);
}
public function testLinkIsSet() {
$this
->setProperty($this->source, 'config', array(
'FeedsFileFetcher' => array(
'source' => 'file fetcher source path',
),
));
$parser = $this
->getParserInstance();
$parser
->setConfig(array(
'context' => array(
'value' => '/beep',
),
));
$result = $parser
->parse($this->source, new FeedsFetcherResult('<?xml version="1.0" encoding="UTF-8"?><item></item>'));
$this
->assertEqual($result->link, 'file fetcher source path');
}
public function testValidateExpression() {
$parser = $this
->getParserInstance();
$expression = '!! ';
$this
->assertEqual('Invalid expression', $this
->invokeMethod($parser, 'validateExpression', array(
&$expression,
)));
$this
->assertEqual($expression, '!!', 'Value was trimmed.');
$unknown = 'thing:asdf';
$this
->assertEqual(NULL, $this
->invokeMethod($parser, 'validateExpression', array(
&$unknown,
)));
$empty = '';
$this
->assertEqual(NULL, $this
->invokeMethod($parser, 'validateExpression', array(
&$empty,
)));
}
public function testEmptyFeed() {
$parser = $this
->getParserInstance();
$parser
->parse($this->source, new FeedsFetcherResult(' '));
$messages = $parser
->getMessenger()
->getMessages();
$this
->assertEqual(1, count($messages), 'The expected number of messages.');
$this
->assertEqual($messages[0]['message'], 'The feed is empty.', 'Message text is correct.');
$this
->assertEqual($messages[0]['type'], 'warning', 'Message type is warning.');
$this
->assertFalse($messages[0]['repeat'], 'Repeat is set to false.');
}
public function testDecodeNamedHtmlEntities() {
$xml = '<root>Ã&<></root>';
$xml = FeedsExXmlUtility::decodeNamedHtmlEntities($xml);
$this
->assertEqual('<root>Ã&<></root>', $xml);
}
protected function getParserInstance() {
$parser = FeedsPlugin::instance('FeedsExXml', strtolower($this
->randomName()));
$parser
->setMessenger(new FeedsExTestMessenger());
return $parser;
}
}
}