Pull merge.
[yaffs-website] / web / core / modules / aggregator / tests / src / Functional / FeedParserTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\aggregator\Entity\Feed;
7 use Drupal\aggregator\Entity\Item;
8
9 /**
10  * Tests the built-in feed parser with valid feed samples.
11  *
12  * @group aggregator
13  */
14 class FeedParserTest extends AggregatorTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     parent::setUp();
21     // Do not delete old aggregator items during these tests, since our sample
22     // feeds have hardcoded dates in them (which may be expired when this test
23     // is run).
24     $this->config('aggregator.settings')->set('items.expire', AGGREGATOR_CLEAR_NEVER)->save();
25   }
26
27   /**
28    * Tests a feed that uses the RSS 0.91 format.
29    */
30   public function testRSS091Sample() {
31     $feed = $this->createFeed($this->getRSS091Sample());
32     $feed->refreshItems();
33     $this->drupalGet('aggregator/sources/' . $feed->id());
34     $this->assertResponse(200, format_string('Feed %name exists.', ['%name' => $feed->label()]));
35     $this->assertText('First example feed item title');
36     $this->assertLinkByHref('http://example.com/example-turns-one');
37     $this->assertText('First example feed item description.');
38     $this->assertRaw('<img src="http://example.com/images/druplicon.png"');
39
40     // Several additional items that include elements over 255 characters.
41     $this->assertRaw("Second example feed item title.");
42     $this->assertText('Long link feed item title');
43     $this->assertText('Long link feed item description');
44     $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');
45     $this->assertText('Long author feed item title');
46     $this->assertText('Long author feed item description');
47     $this->assertLinkByHref('http://example.com/long/author');
48   }
49
50   /**
51    * Tests a feed that uses the Atom format.
52    */
53   public function testAtomSample() {
54     $feed = $this->createFeed($this->getAtomSample());
55     $feed->refreshItems();
56     $this->drupalGet('aggregator/sources/' . $feed->id());
57     $this->assertResponse(200, format_string('Feed %name exists.', ['%name' => $feed->label()]));
58     $this->assertText('Atom-Powered Robots Run Amok');
59     $this->assertLinkByHref('http://example.org/2003/12/13/atom03');
60     $this->assertText('Some text.');
61     $iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/13/atom03')->execute();
62     $item = Item::load(array_values($iids)[0]);
63     $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
64
65     // Check for second feed entry.
66     $this->assertText('We tried to stop them, but we failed.');
67     $this->assertLinkByHref('http://example.org/2003/12/14/atom03');
68     $this->assertText('Some other text.');
69     $iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/14/atom03')->execute();
70     $item = Item::load(array_values($iids)[0]);
71     $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
72   }
73
74   /**
75    * Tests a feed that uses HTML entities in item titles.
76    */
77   public function testHtmlEntitiesSample() {
78     $feed = $this->createFeed($this->getHtmlEntitiesSample());
79     $feed->refreshItems();
80     $this->drupalGet('aggregator/sources/' . $feed->id());
81     $this->assertResponse(200, format_string('Feed %name exists.', ['%name' => $feed->label()]));
82     $this->assertRaw("Quote&quot; Amp&amp;");
83   }
84
85   /**
86    * Tests that a redirected feed is tracked to its target.
87    */
88   public function testRedirectFeed() {
89     $redirect_url = Url::fromRoute('aggregator_test.redirect')->setAbsolute()->toString();
90     $feed = Feed::create(['url' => $redirect_url, 'title' => $this->randomMachineName()]);
91     $feed->save();
92     $feed->refreshItems();
93
94     // Make sure that the feed URL was updated correctly.
95     $this->assertEqual($feed->getUrl(), \Drupal::url('aggregator_test.feed', [], ['absolute' => TRUE]));
96   }
97
98   /**
99    * Tests error handling when an invalid feed is added.
100    */
101   public function testInvalidFeed() {
102     // Simulate a typo in the URL to force a curl exception.
103     $invalid_url = 'http:/www.drupal.org';
104     $feed = Feed::create(['url' => $invalid_url, 'title' => $this->randomMachineName()]);
105     $feed->save();
106
107     // Update the feed. Use the UI to be able to check the message easily.
108     $this->drupalGet('admin/config/services/aggregator');
109     $this->clickLink(t('Update items'));
110     $this->assertRaw(t('The feed from %title seems to be broken because of error', ['%title' => $feed->label()]));
111   }
112
113 }