FeedParserTest.php 4.8 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\aggregator\Functional;
4

5
use Drupal\Core\Url;
6
use Drupal\aggregator\Entity\Feed;
7

8
/**
9 10 11
 * Tests the built-in feed parser with valid feed samples.
 *
 * @group aggregator
12 13
 */
class FeedParserTest extends AggregatorTestBase {
14 15 16 17

  /**
   * {@inheritdoc}
   */
18
  protected function setUp() {
19
    parent::setUp();
20
    // Do not delete old aggregator items during these tests, since our sample
21 22
    // feeds have hardcoded dates in them (which may be expired when this test
    // is run).
23
    $this->config('aggregator.settings')->set('items.expire', AGGREGATOR_CLEAR_NEVER)->save();
24 25 26
  }

  /**
27
   * Tests a feed that uses the RSS 0.91 format.
28
   */
29
  public function testRSS091Sample() {
30
    $feed = $this->createFeed($this->getRSS091Sample());
31
    $feed->refreshItems();
32 33
    $this->drupalGet('aggregator/sources/' . $feed->id());
    $this->assertResponse(200, format_string('Feed %name exists.', array('%name' => $feed->label())));
34 35 36
    $this->assertText('First example feed item title');
    $this->assertLinkByHref('http://example.com/example-turns-one');
    $this->assertText('First example feed item description.');
37
    $this->assertRaw('<img src="http://example.com/images/druplicon.png"');
38 39 40 41 42 43 44 45 46

    // Several additional items that include elements over 255 characters.
    $this->assertRaw("Second example feed item title.");
    $this->assertText('Long link feed item title');
    $this->assertText('Long link feed item description');
    $this->assertLinkByHref('http://example.com/tomorrow/and/tomorrow/and/tomorrow/creeps/in/this/petty/pace/from/day/to/day/to/the/last/syllable/of/recorded/time/and/all/our/yesterdays/have/lighted/fools/the/way/to/dusty/death/out/out/brief/candle/life/is/but/a/walking/shadow/a/poor/player/that/struts/and/frets/his/hour/upon/the/stage/and/is/heard/no/more/it/is/a/tale/told/by/an/idiot/full/of/sound/and/fury/signifying/nothing');
    $this->assertText('Long author feed item title');
    $this->assertText('Long author feed item description');
    $this->assertLinkByHref('http://example.com/long/author');
47 48 49
  }

  /**
50
   * Tests a feed that uses the Atom format.
51
   */
52
  public function testAtomSample() {
53
    $feed = $this->createFeed($this->getAtomSample());
54
    $feed->refreshItems();
55 56
    $this->drupalGet('aggregator/sources/' . $feed->id());
    $this->assertResponse(200, format_string('Feed %name exists.', array('%name' => $feed->label())));
57 58 59 60
    $this->assertText('Atom-Powered Robots Run Amok');
    $this->assertLinkByHref('http://example.org/2003/12/13/atom03');
    $this->assertText('Some text.');
    $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', db_query('SELECT guid FROM {aggregator_item} WHERE link = :link', array(':link' => 'http://example.org/2003/12/13/atom03'))->fetchField(), 'Atom entry id element is parsed correctly.');
61 62 63 64 65 66 67 68 69

    // Check for second feed entry.
    $this->assertText('We tried to stop them, but we failed.');
    $this->assertLinkByHref('http://example.org/2003/12/14/atom03');
    $this->assertText('Some other text.');
    $db_guid = db_query('SELECT guid FROM {aggregator_item} WHERE link = :link', array(
      ':link' => 'http://example.org/2003/12/14/atom03',
    ))->fetchField();
    $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $db_guid, 'Atom entry id element is parsed correctly.');
70 71 72 73 74
  }

  /**
   * Tests a feed that uses HTML entities in item titles.
   */
75
  public function testHtmlEntitiesSample() {
76
    $feed = $this->createFeed($this->getHtmlEntitiesSample());
77
    $feed->refreshItems();
78 79
    $this->drupalGet('aggregator/sources/' . $feed->id());
    $this->assertResponse(200, format_string('Feed %name exists.', array('%name' => $feed->label())));
80 81
    $this->assertRaw("Quote&quot; Amp&amp;");
  }
82 83

  /**
84
   * Tests that a redirected feed is tracked to its target.
85
   */
86
  public function testRedirectFeed() {
87
    $redirect_url = Url::fromRoute('aggregator_test.redirect')->setAbsolute()->toString();
88
    $feed = Feed::create(array('url' => $redirect_url, 'title' => $this->randomMachineName()));
89
    $feed->save();
90
    $feed->refreshItems();
91 92

    // Make sure that the feed URL was updated correctly.
93
    $this->assertEqual($feed->getUrl(), \Drupal::url('aggregator_test.feed', array(), array('absolute' => TRUE)));
94 95 96 97 98
  }

  /**
   * Tests error handling when an invalid feed is added.
   */
99
  public function testInvalidFeed() {
100 101
    // Simulate a typo in the URL to force a curl exception.
    $invalid_url = 'http:/www.drupal.org';
102
    $feed = Feed::create(array('url' => $invalid_url, 'title' => $this->randomMachineName()));
103 104 105 106 107
    $feed->save();

    // Update the feed. Use the UI to be able to check the message easily.
    $this->drupalGet('admin/config/services/aggregator');
    $this->clickLink(t('Update items'));
108
    $this->assertRaw(t('The feed from %title seems to be broken because of error', array('%title' => $feed->label())));
109
  }
110

111
}