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

3
namespace Drupal\Tests\aggregator\Functional;
4

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

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

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

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

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

  /**
52
   * Tests a feed that uses the Atom format.
53
   */
54
  public function testAtomSample() {
55
    $feed = $this->createFeed($this->getAtomSample());
56
    $feed->refreshItems();
57
    $this->drupalGet('aggregator/sources/' . $feed->id());
58
    $this->assertResponse(200, format_string('Feed %name exists.', ['%name' => $feed->label()]));
59 60 61
    $this->assertText('Atom-Powered Robots Run Amok');
    $this->assertLinkByHref('http://example.org/2003/12/13/atom03');
    $this->assertText('Some text.');
62 63 64
    $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.');
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.');
70 71 72
    $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.');
73 74 75 76 77
  }

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

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

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

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

114
}