View source
<?php
namespace Drupal\Tests\feeds_ex\Unit\Feeds\Parser;
use Drupal\feeds\Result\RawFetcherResult;
use Drupal\feeds_ex\Feeds\Parser\QueryPathXmlParser;
use Drupal\feeds_ex\Messenger\TestMessenger;
use Drupal\feeds_ex\Utility\XmlUtility;
class QueryPathXmlParserTest extends ParserTestBase {
public function setUp() {
parent::setUp();
$configuration = [
'feed_type' => $this->feedType,
];
$utility = new XmlUtility();
$utility
->setStringTranslation($this
->getStringTranslationStub());
$this->parser = new QueryPathXmlParser($configuration, 'querypathxml', [], $utility);
$this->parser
->setStringTranslation($this
->getStringTranslationStub());
$this->parser
->setFeedsExMessenger(new TestMessenger());
}
public function testSimpleParsing() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('I am a title' . $delta, $item
->get('title'));
$this
->assertSame('I am a description' . $delta, $item
->get('description'));
}
}
public function testRaw() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
'raw' => TRUE,
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('I am a title' . $delta, $item
->get('title'));
$this
->assertXmlStringEqualsXmlString('<description><text>I am a description' . $delta . '</text></description>', $item
->get('description'));
}
}
public function testInner() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
'raw' => TRUE,
'inner' => TRUE,
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('I am a title' . $delta, $item
->get('title'));
$this
->assertXmlStringEqualsXmlString('<text>I am a description' . $delta . '</text>', $item
->get('description'));
}
}
public function testAttributeParsing() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => 'attr',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('attribute' . $delta, $item
->get('title'));
$this
->assertSame('I am a description' . $delta, $item
->get('description'));
}
}
public function testMultipleAttributeParsing() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items thing',
],
'sources' => [
'url' => [
'name' => 'URL',
'value' => 'img',
'attribute' => 'src',
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(1, $result);
$url = $result[0]
->get('url');
$this
->assertCount(2, $url);
$this
->assertSame($url[0], 'http://drupal.org');
$this
->assertSame($url[1], 'http://drupal.org/project/feeds_ex');
}
public function testCp866Encoded() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_ru.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
],
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('Я название' . $delta, $item
->get('title'));
$this
->assertSame('Я описание' . $delta, $item
->get('description'));
}
}
public function testEucJpEncodedNoDeclaration() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_jp.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
],
],
'source_encoding' => [
'EUC-JP',
],
];
$this->parser
->setConfiguration($config);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(3, $result);
foreach ($result as $delta => $item) {
$this
->assertSame('私はタイトルです' . $delta, $item
->get('title'));
$this
->assertSame('私が説明してい' . $delta, $item
->get('description'));
}
}
public function testBatchParsing() {
$fetcher_result = new RawFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'), $this->fileSystem);
$config = [
'context' => [
'value' => 'items item',
],
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'title',
'attribute' => '',
],
'description' => [
'name' => 'Title',
'value' => 'description',
'attribute' => '',
],
],
'line_limit' => 1,
];
$this->parser
->setConfiguration($config);
foreach (range(0, 2) as $delta) {
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(1, $result);
$this
->assertSame('I am a title' . $delta, $result[0]
->get('title'));
$this
->assertSame('I am a description' . $delta, $result[0]
->get('description'));
}
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(0, $result);
}
public function testValidateExpression() {
$expression = '!! ';
$this
->assertSame('CSS selector is not well formed.', (string) $this
->invokeMethod($this->parser, 'validateExpression', [
&$expression,
]));
$this
->assertSame($expression, '!!', 'Value was trimmed.');
$empty = '';
$this
->assertSame(NULL, $this
->invokeMethod($this->parser, 'validateExpression', [
&$empty,
]));
}
public function testEmptyFeed() {
$this->parser
->parse($this->feed, new RawFetcherResult(' ', $this->fileSystem), $this->state);
$this
->assertEmptyFeedMessage($this->parser
->getMessenger()
->getMessages());
}
}