3 namespace Drupal\Tests\aggregator\Functional;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\views\Entity\View;
9 * Tests display of aggregator items on the page.
13 class AggregatorRenderingTest extends AggregatorTestBase {
20 public static $modules = ['block', 'test_page_test'];
22 protected function setUp() {
25 $this->drupalPlaceBlock('page_title_block');
29 * Adds a feed block to the page and checks its links.
31 public function testBlockLinks() {
33 $this->createSampleNodes();
34 $feed = $this->createFeed();
35 $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
37 // Need admin user to be able to access block admin.
38 $admin_user = $this->drupalCreateUser([
40 'access administration pages',
41 'administer news feeds',
44 $this->drupalLogin($admin_user);
46 $block = $this->drupalPlaceBlock("aggregator_feed_block", ['label' => 'feed-' . $feed->label()]);
48 // Configure the feed that should be displayed.
49 $block->getPlugin()->setConfigurationValue('feed', $feed->id());
50 $block->getPlugin()->setConfigurationValue('block_count', 2);
53 // Confirm that the block is now being displayed on pages.
54 $this->drupalGet('test-page');
55 $this->assertText($block->label(), 'Feed block is displayed on the page.');
57 // Confirm items appear as links.
58 $items = $this->container->get('entity.manager')->getStorage('aggregator_item')->loadByFeed($feed->id(), 1);
59 $links = $this->xpath('//a[@href = :href]', [':href' => reset($items)->getLink()]);
60 $this->assert(isset($links[0]), 'Item link found.');
62 // Find the expected read_more link.
64 $links = $this->xpath('//a[@href = :href]', [':href' => $href]);
65 $this->assert(isset($links[0]), format_string('Link to href %href found.', ['%href' => $href]));
66 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
67 $cache_tags = explode(' ', $cache_tags_header);
68 $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
71 $this->drupalGet($feed->urlInfo()->getInternalPath());
72 $correct_titles = $this->xpath('//h1[normalize-space(text())=:title]', [':title' => $feed->label()]);
73 $this->assertFalse(empty($correct_titles), 'Aggregator feed page is available and has the correct title.');
74 $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
75 $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
76 $this->assertTrue(in_array('aggregator_feed_view', $cache_tags));
77 $this->assertTrue(in_array('aggregator_item_view', $cache_tags));
79 // Set the number of news items to 0 to test that the block does not show
81 $block->getPlugin()->setConfigurationValue('block_count', 0);
83 // Check that the block is no longer displayed.
84 $this->drupalGet('test-page');
85 $this->assertNoText($block->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
89 * Creates a feed and checks that feed's page.
91 public function testFeedPage() {
92 // Increase the number of items published in the rss.xml feed so we have
93 // enough articles to test paging.
94 $view = View::load('frontpage');
95 $display = &$view->getDisplay('feed_1');
96 $display['display_options']['pager']['options']['items_per_page'] = 30;
99 // Create a feed with 30 items.
100 $this->createSampleNodes(30);
101 $feed = $this->createFeed();
102 $this->updateFeedItems($feed, 30);
104 // Check for presence of an aggregator pager.
105 $this->drupalGet('aggregator');
106 $elements = $this->xpath("//ul[contains(@class, :class)]", [':class' => 'pager__items']);
107 $this->assertTrue(!empty($elements), 'Individual source page contains a pager.');
109 // Check for sources page title.
110 $this->drupalGet('aggregator/sources');
111 $titles = $this->xpath('//h1[normalize-space(text())=:title]', [':title' => 'Sources']);
112 $this->assertTrue(!empty($titles), 'Source page contains correct title.');
114 // Find the expected read_more link on the sources page.
115 $href = $feed->url();
116 $links = $this->xpath('//a[@href = :href]', [':href' => $href]);
117 $this->assertTrue(isset($links[0]), SafeMarkup::format('Link to href %href found.', ['%href' => $href]));
118 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
119 $cache_tags = explode(' ', $cache_tags_header);
120 $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
122 // Check the rss aggregator page as anonymous user.
123 $this->drupalLogout();
124 $this->drupalGet('aggregator/rss');
125 $this->assertResponse(403);
127 // Check the rss aggregator page as admin.
128 $this->drupalLogin($this->adminUser);
129 $this->drupalGet('aggregator/rss');
130 $this->assertResponse(200);
131 $this->assertEqual($this->drupalGetHeader('Content-type'), 'application/rss+xml; charset=utf-8');
133 // Check the opml aggregator page.
134 $this->drupalGet('aggregator/opml');
135 $content = $this->getSession()->getPage()->getContent();
136 // We can't use Mink xpath queries here because it only supports HTML pages,
137 // but we are dealing with XML here.
138 $xml = simplexml_load_string($content);
139 $attributes = $xml->xpath('//outline[1]')[0]->attributes();
140 $this->assertEquals('rss', $attributes->type);
141 $this->assertEquals($feed->label(), $attributes->text);
142 $this->assertEquals($feed->getUrl(), $attributes->xmlUrl);
144 // Check for the presence of a pager.
145 $this->drupalGet('aggregator/sources/' . $feed->id());
146 $elements = $this->xpath("//ul[contains(@class, :class)]", [':class' => 'pager__items']);
147 $this->assertTrue(!empty($elements), 'Individual source page contains a pager.');
148 $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
149 $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
150 $this->assertTrue(in_array('aggregator_feed_view', $cache_tags));
151 $this->assertTrue(in_array('aggregator_item_view', $cache_tags));