de7e401058b23eb403ab9afc19fe3fdb15c829fb
[yaffs-website] / web / core / modules / aggregator / src / Tests / AggregatorRenderingTest.php
1 <?php
2
3 namespace Drupal\aggregator\Tests;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\views\Entity\View;
7
8 /**
9  * Tests display of aggregator items on the page.
10  *
11  * @group aggregator
12  */
13 class AggregatorRenderingTest extends AggregatorTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['block', 'test_page_test'];
21
22   protected function setUp() {
23     parent::setUp();
24
25     $this->drupalPlaceBlock('page_title_block');
26   }
27
28   /**
29    * Adds a feed block to the page and checks its links.
30    */
31   public function testBlockLinks() {
32     // Create feed.
33     $this->createSampleNodes();
34     $feed = $this->createFeed();
35     $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
36
37     // Need admin user to be able to access block admin.
38     $admin_user = $this->drupalCreateUser([
39       'administer blocks',
40       'access administration pages',
41       'administer news feeds',
42       'access news feeds',
43     ]);
44     $this->drupalLogin($admin_user);
45
46     $block = $this->drupalPlaceBlock("aggregator_feed_block", ['label' => 'feed-' . $feed->label()]);
47
48     // Configure the feed that should be displayed.
49     $block->getPlugin()->setConfigurationValue('feed', $feed->id());
50     $block->getPlugin()->setConfigurationValue('block_count', 2);
51     $block->save();
52
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.');
56
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.');
61
62     // Find the expected read_more link.
63     $href = $feed->url();
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));
69
70     // Visit that page.
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));
78
79     // Set the number of news items to 0 to test that the block does not show
80     // up.
81     $block->getPlugin()->setConfigurationValue('block_count', 0);
82     $block->save();
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.');
86   }
87
88   /**
89    * Creates a feed and checks that feed's page.
90    */
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;
97     $view->save();
98
99     // Create a feed with 30 items.
100     $this->createSampleNodes(30);
101     $feed = $this->createFeed();
102     $this->updateFeedItems($feed, 30);
103
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.');
108
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.');
113
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));
121
122     // Check the rss aggregator page as anonymous user.
123     $this->drupalLogout();
124     $this->drupalGet('aggregator/rss');
125     $this->assertResponse(403);
126
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');
132
133     // Check the opml aggregator page.
134     $this->drupalGet('aggregator/opml');
135     $outline = $this->xpath('//outline[1]');
136     $this->assertEqual($outline[0]['type'], 'rss', 'The correct type attribute is used for rss OPML.');
137     $this->assertEqual($outline[0]['text'], $feed->label(), 'The correct text attribute is used for rss OPML.');
138     $this->assertEqual($outline[0]['xmlurl'], $feed->getUrl(), 'The correct xmlUrl attribute is used for rss OPML.');
139
140     // Check for the presence of a pager.
141     $this->drupalGet('aggregator/sources/' . $feed->id());
142     $elements = $this->xpath("//ul[contains(@class, :class)]", [':class' => 'pager__items']);
143     $this->assertTrue(!empty($elements), 'Individual source page contains a pager.');
144     $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
145     $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
146     $this->assertTrue(in_array('aggregator_feed_view', $cache_tags));
147     $this->assertTrue(in_array('aggregator_item_view', $cache_tags));
148   }
149
150 }