You are here

FeedsExXml.test in Feeds extensible parsers 7

Same filename and directory in other branches
  1. 7.2 src/Tests/FeedsExXml.test

File

src/Tests/FeedsExXml.test
View source
<?php

/**
 * @file
 * Contains tests for FeedsExXml.
 */
if (class_exists('TUnit')) {

  /**
   * Unit tests for FeedsExXml.
   */
  class FeedsExXmlUnitTests extends FeedsExUnitTestBase {

    /**
     * The mocked FeedsSource.
     *
     * @var FeedsSource
     */
    protected $source;

    /**
     * {@inheritdoc}
     */
    public static function getInfo() {
      return array(
        'name' => 'XML parser unit tests',
        'description' => 'Unit tests for FeedsExXml.',
        'group' => 'Feeds EX',
      );
    }

    /**
     * {@inheritdoc}
     */
    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();
      $contents = file_get_contents($this->moduleDir . '/tests/resources/test.xml');

      // Implicitly test that invalid characters are ignored and null bytes are
      // stripped.
      $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']);
      }
    }

    /**
     * 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('<description><text>I am a description' . $delta . '</text></description>', $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('<text>I am a description' . $delta . '</text>', $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('<?xml version="1.0" encoding="UTF-8"?><item></item>'));
      $this
        ->assertEqual($result->link, 'file fetcher source path');
    }

    /**
     * Tests XPath validation.
     */
    public function testValidateExpression() {

      // Invalid expression.
      $parser = $this
        ->getParserInstance();
      $expression = '!! ';
      $this
        ->assertEqual('Invalid expression', $this
        ->invokeMethod($parser, 'validateExpression', array(
        &$expression,
      )));

      // Test that value was trimmed.
      $this
        ->assertEqual($expression, '!!', 'Value was trimmed.');

      // Unknown namespace.
      $unknown = 'thing:asdf';
      $this
        ->assertEqual(NULL, $this
        ->invokeMethod($parser, 'validateExpression', array(
        &$unknown,
      )));

      // Empty.
      $empty = '';
      $this
        ->assertEqual(NULL, $this
        ->invokeMethod($parser, 'validateExpression', array(
        &$empty,
      )));
    }

    /**
     * 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.');
    }

    /**
     * Tests FeedsExXmlUtility::decodeNamedHtmlEntities().
     */
    public function testDecodeNamedHtmlEntities() {
      $xml = '<root>&Atilde;&amp;&lt;&gt;</root>';
      $xml = FeedsExXmlUtility::decodeNamedHtmlEntities($xml);
      $this
        ->assertEqual('<root>Ã&amp;&lt;&gt;</root>', $xml);
    }

    /**
     * Returns a new instance of the parser.
     *
     * @return FeedsExXml
     *   A parser instance.
     */
    protected function getParserInstance() {
      $parser = FeedsPlugin::instance('FeedsExXml', strtolower($this
        ->randomName()));
      $parser
        ->setMessenger(new FeedsExTestMessenger());
      return $parser;
    }

  }
}