You are here

FeedsExHtml.test in Feeds extensible parsers 7

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

File

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

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

  /**
   * Unit tests for FeedsExHtml.
   */
  class FeedsExHtmlUnitTests extends FeedsExUnitTestBase {

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

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

    /**
     * {@inheritdoc}
     */
    public function setUp() {
      parent::setUp();
      require_once $this->moduleDir . '/src/FeedsExXml.inc';
      require_once $this->moduleDir . '/src/FeedsExHtml.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.html'));
      $parser
        ->setConfig(array(
        'context' => array(
          'value' => '//div[@class="post"]',
        ),
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'h3',
          ),
          'description' => array(
            'name' => 'Description',
            'value' => 'p',
          ),
        ),
      ));
      $result = $parser
        ->parse($this->source, $fetcher_result);
      $this
        ->assertParserResultItemCount($result, 3);
      $this
        ->assertEqual('I am a title<thing>Stuff</thing>', $result->items[0]['title']);
      $this
        ->assertEqual('I am a description0', $result->items[0]['description']);
      $this
        ->assertEqual('I am a title1', $result->items[1]['title']);
      $this
        ->assertEqual('I am a description1', $result->items[1]['description']);
      $this
        ->assertEqual('I am a title2', $result->items[2]['title']);
      $this
        ->assertEqual('I am a description2', $result->items[2]['description']);
    }

    /**
     * Tests getting the raw value.
     */
    public function testRaw() {
      $parser = $this
        ->getParserInstance();
      $fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.html'));
      $parser
        ->setConfig(array(
        'context' => array(
          'value' => '//div[@class="post"]',
        ),
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'h3',
          ),
          'description' => array(
            'name' => 'Description',
            'value' => 'p',
            'raw' => TRUE,
          ),
        ),
      ));
      $result = $parser
        ->parse($this->source, $fetcher_result);
      $this
        ->assertParserResultItemCount($result, 3);
      $this
        ->assertEqual('I am a title<thing>Stuff</thing>', $result->items[0]['title']);
      $this
        ->assertEqual('<p>I am a description0</p>', $result->items[0]['description']);
      $this
        ->assertEqual('I am a title1', $result->items[1]['title']);
      $this
        ->assertEqual('<p>I am a description1</p>', $result->items[1]['description']);
      $this
        ->assertEqual('I am a title2', $result->items[2]['title']);
      $this
        ->assertEqual('<p>I am a description2</p>', $result->items[2]['description']);
    }

    /**
     * Tests innerxml.
     */
    public function testInner() {
      $parser = $this
        ->getParserInstance();
      $fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.html'));
      $parser
        ->setConfig(array(
        'context' => array(
          'value' => '//div[@class="post"]',
        ),
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'h3',
          ),
          'description' => array(
            'name' => 'Description',
            'value' => 'p',
            'raw' => TRUE,
            'inner' => TRUE,
          ),
        ),
      ));
      $result = $parser
        ->parse($this->source, $fetcher_result);
      $this
        ->assertParserResultItemCount($result, 3);
      $this
        ->assertEqual('I am a title<thing>Stuff</thing>', $result->items[0]['title']);
      $this
        ->assertEqual('I am a description0', $result->items[0]['description']);
      $this
        ->assertEqual('I am a title1', $result->items[1]['title']);
      $this
        ->assertEqual('I am a description1', $result->items[1]['description']);
      $this
        ->assertEqual('I am a title2', $result->items[2]['title']);
      $this
        ->assertEqual('I am a description2', $result->items[2]['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.html'));
      $parser
        ->setConfig(array(
        'context' => array(
          'value' => '//div[@class="post"]',
        ),
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'h3',
          ),
          'description' => array(
            'name' => 'Title',
            'value' => 'p',
          ),
        ),
      ));
      $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.html'));
      $parser
        ->setConfig(array(
        'context' => array(
          'value' => '//div[@class="post"]',
        ),
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'h3',
          ),
          'description' => array(
            'name' => 'Title',
            'value' => 'p',
          ),
        ),
        '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 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 empty feed handling.
     */
    public function testEmptyFeed() {
      $parser = $this
        ->getParserInstance();
      $parser
        ->parse($this->source, new FeedsFetcherResult(' '));
      $this
        ->assertEmptyFeedMessage($parser
        ->getMessenger()
        ->getMessages());
    }

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

  }
}

/**
 * Integration tests for FeedsExHtml.
 */
class FeedsExHtmlTests extends FeedsWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'HTML parser integration tests',
      'description' => 'Integration tests for FeedsExHtml.',
      'group' => 'Feeds EX',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('feeds_ex');
    $this
      ->createImporterConfiguration();
    $this
      ->setSettings('syndication', '', array(
      'content_type' => '',
    ));
    $this
      ->setPlugin('syndication', 'FeedsExHtml');
  }

  /**
   * Tests the full import process.
   */
  public function test() {
    $this
      ->setContext('syndication', '//div[@class="post"]');
    $this
      ->addMappings('syndication', array(
      0 => array(
        'source' => $this
          ->addExpression('syndication', 'h3'),
        'target' => 'title',
      ),
      1 => array(
        'source' => $this
          ->addExpression('syndication', 'p'),
        'target' => 'body',
      ),
    ));
    $this
      ->importUrl('syndication', file_create_url(drupal_get_path('module', 'feeds_ex') . '/tests/resources/test.html'));
    $this
      ->drupalGet('node/1/edit');
    $this
      ->assertFieldByName('title', 'I am a title<thing>Stuff</thing>');
    $this
      ->assertFieldByName('body[und][0][value]', 'I am a description0');
    $this
      ->drupalGet('node/2/edit');
    $this
      ->assertFieldByName('title', 'I am a title1');
    $this
      ->assertFieldByName('body[und][0][value]', 'I am a description1');
    $this
      ->drupalGet('node/3/edit');
    $this
      ->assertFieldByName('title', 'I am a title2');
    $this
      ->assertFieldByName('body[und][0][value]', 'I am a description2');
  }

  /**
   * Sets the form context value.
   *
   * @param string $id
   *   The importer id.
   * @param string $value
   *   The context value.
   */
  protected function setContext($id, $value) {
    $importer = feeds_importer($id);
    $config = $importer->parser
      ->getConfig();
    $config['context']['value'] = $value;
    $importer->parser
      ->setConfig($config);
    $importer
      ->save();
  }

  /**
   * Adds an expression.
   *
   * @param string $id
   *   The importer id.
   * @param string $value
   *   The expression value.
   * @param array $settings
   *   (optional) Settings to configure the expression. Defaults to an empty
   *   array.
   */
  protected function addExpression($id, $value, array $settings = array()) {
    $importer = feeds_importer($id);
    $config = $importer->parser
      ->getConfig();
    if (!isset($settings['weight'])) {
      $weight = end($config['sources']);
      $weight = $weight ? $weight['weight'] + 1 : 0;
      $settings['weight'] = $weight;
    }
    $settings += array(
      'raw' => 0,
      'debug' => 0,
    );
    $machine_name = strtolower($this
      ->randomName());
    $config['sources'][$machine_name] = array(
      'name' => $this
        ->randomString(),
      'value' => $value,
    ) + $settings;
    $importer->parser
      ->setConfig($config);
    $importer
      ->save();
    return $machine_name;
  }

}

Classes

Namesort descending Description
FeedsExHtmlTests Integration tests for FeedsExHtml.