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

3
namespace Drupal\Tests\aggregator\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6
use Drupal\aggregator\FeedStorageInterface;
7
use Drupal\Core\Url;
8
use Drupal\aggregator\Entity\Feed;
9
use Drupal\aggregator\Entity\Item;
10

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

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

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

    // 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');
50 51 52
  }

  /**
53
   * Tests a feed that uses the Atom format.
54
   */
55
  public function testAtomSample() {
56
    $feed = $this->createFeed($this->getAtomSample());
57
    $feed->refreshItems();
58
    $this->drupalGet('aggregator/sources/' . $feed->id());
59
    $this->assertResponse(200, new FormattableMarkup('Feed %name exists.', ['%name' => $feed->label()]));
60 61 62
    $this->assertText('Atom-Powered Robots Run Amok');
    $this->assertLinkByHref('http://example.org/2003/12/13/atom03');
    $this->assertText('Some text.');
63 64 65
    $iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/13/atom03')->execute();
    $item = Item::load(array_values($iids)[0]);
    $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
66 67 68 69 70

    // 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.');
71 72 73
    $iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/14/atom03')->execute();
    $item = Item::load(array_values($iids)[0]);
    $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
74 75 76 77 78
  }

  /**
   * Tests a feed that uses HTML entities in item titles.
   */
79
  public function testHtmlEntitiesSample() {
80
    $feed = $this->createFeed($this->getHtmlEntitiesSample());
81
    $feed->refreshItems();
82
    $this->drupalGet('aggregator/sources/' . $feed->id());
83
    $this->assertResponse(200, new FormattableMarkup('Feed %name exists.', ['%name' => $feed->label()]));
84 85
    $this->assertRaw("Quote&quot; Amp&amp;");
  }
86 87

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

    // Make sure that the feed URL was updated correctly.
97
    $this->assertEqual($feed->getUrl(), Url::fromRoute('aggregator_test.feed', [], ['absolute' => TRUE])->toString());
98 99 100 101 102
  }

  /**
   * Tests error handling when an invalid feed is added.
   */
103
  public function testInvalidFeed() {
104 105
    // Simulate a typo in the URL to force a curl exception.
    $invalid_url = 'http:/www.drupal.org';
106
    $feed = Feed::create(['url' => $invalid_url, 'title' => $this->randomMachineName()]);
107 108 109 110 111
    $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'));
112
    $this->assertRaw(t('The feed from %title seems to be broken because of error', ['%title' => $feed->label()]));
113
  }
114

115
}