FeedParserTest.php 4.9 KB

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