'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(); } /** * Tests simple parsing. */ public function testSimpleParsing() { $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', ), ), )); $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']); } } /** * Tests raw parsing. */ 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('I am a description' . $delta . '', $item['description']); } } /** * Tests simple parsing. */ 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('I am a description' . $delta . '', $item['description']); } } /** * Tests parsing a CP866 (Russian) encoded file. */ 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']); } } /** * Tests a EUC-JP (Japanese) encoded file without the encoding declaration. * * This implicitly tests FeedsExBase's encoding conversion. */ 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']); } } /** * Tests batching. */ 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']); } // Should be empty. $result = $parser->parse($this->source, $fetcher_result); $this->assertParserResultItemCount($result, 0); } /** * Tests that the link propery is set. */ 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('')); $this->assertEqual($result->link, 'file fetcher source path'); } /** * Tests XPath validation. */ public function testValidateExpression() { // Invalid expression. $parser = $this->getParserInstance(); $expression = array('!!'); $this->assertEqual('Invalid expression', $this->invokeMethod($parser, 'validateExpression', $expression)); // Test that value was trimmed. $this->assertEqual($expression[0], '!!', 'Value was trimmed.'); // Unknown namespace. $this->assertEqual(NULL, $this->invokeMethod($parser, 'validateExpression', array('thing:asdf'))); // Empty. $this->assertEqual(NULL, $this->invokeMethod($parser, 'validateExpression', array(''))); } /** * Tests empty feed handling. */ 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.'); } /** * Returns a new instance of the parser. * * @return FeedsExXml * A parser instance. */ protected function getParserInstance() { $parser = FeedsConfigurable::instance('FeedsExXml', strtolower($this->randomName())); $parser->setMessenger(new FeedsExTestMessenger()); return $parser; } }