3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
9 * Tests the feed display plugin.
12 * @see \Drupal\views\Plugin\views\display\Feed
14 class DisplayFeedTest extends ViewTestBase {
17 * Views used by this test.
21 public static $testViews = ['test_display_feed', 'test_attached_disabled', 'test_feed_icon'];
28 public static $modules = ['block', 'node', 'views'];
30 protected function setUp($import_test_views = TRUE) {
31 parent::setUp($import_test_views);
33 $this->enableViewsTestModule();
35 $admin_user = $this->drupalCreateUser(['administer site configuration']);
36 $this->drupalLogin($admin_user);
40 * Tests the rendered output.
42 public function testFeedOutput() {
43 $this->drupalCreateContentType(['type' => 'page']);
45 // Verify a title with HTML entities is properly escaped.
46 $node_title = 'This "cool" & "neat" article\'s title';
47 $node = $this->drupalCreateNode([
48 'title' => $node_title,
51 'value' => 'A paragraph',
52 'format' => filter_default_format(),
57 // Test the site name setting.
58 $site_name = $this->randomMachineName();
59 $this->config('system.site')->set('name', $site_name)->save();
61 $this->drupalGet('test-feed-display.xml');
62 $this->assertEquals($site_name, $this->getSession()->getDriver()->getText('//title'));
63 $this->assertEquals($node_title, $this->getSession()->getDriver()->getText('//item/title'));
64 // Verify HTML is properly escaped in the description field.
65 $this->assertRaw('<p>A paragraph</p>');
67 $view = $this->container->get('entity.manager')->getStorage('view')->load('test_display_feed');
68 $display = &$view->getDisplay('feed_1');
69 $display['display_options']['sitename_title'] = 0;
72 $this->drupalGet('test-feed-display.xml');
73 $this->assertEquals('test_display_feed', $this->getSession()->getDriver()->getText('//title'));
75 // Add a block display and attach the feed.
76 $view->getExecutable()->newDisplay('block', NULL, 'test');
77 $display = &$view->getDisplay('feed_1');
78 $display['display_options']['displays']['test'] = 'test';
80 // Test the feed display adds a feed icon to the block display.
81 $this->drupalPlaceBlock('views_block:test_display_feed-test');
82 $this->drupalGet('<front>');
83 $feed_icon = $this->cssSelect('div.view-id-test_display_feed a.feed-icon');
84 $this->assertTrue(strpos($feed_icon[0]->getAttribute('href'), 'test-feed-display.xml'), 'The feed icon was found.');
86 // Test feed display attached to page display with arguments.
87 $this->drupalGet('test-feed-icon/' . $node->id());
88 $page_url = $this->getUrl();
89 $icon_href = $this->cssSelect('a.feed-icon[href *= "test-feed-icon"]')[0]->getAttribute('href');
90 $this->assertEqual($icon_href, $page_url . '/feed', 'The feed icon was found.');
91 $link_href = $this->cssSelect('link[type = "application/rss+xml"][href *= "test-feed-icon"]')[0]->getAttribute('href');
92 $this->assertEqual($link_href, $page_url . '/feed', 'The RSS link was found.');
93 $feed_link = simplexml_load_string($this->drupalGet($icon_href))->channel->link;
94 $this->assertEqual($feed_link, $page_url, 'The channel link was found.');
98 * Tests the rendered output for fields display.
100 public function testFeedFieldOutput() {
101 $this->drupalCreateContentType(['type' => 'page']);
103 // Verify a title with HTML entities is properly escaped.
104 $node_title = 'This "cool" & "neat" article\'s title';
105 $this->drupalCreateNode([
106 'title' => $node_title,
109 'value' => 'A paragraph',
110 'format' => filter_default_format(),
115 $this->drupalGet('test-feed-display-fields.xml');
116 $this->assertEquals($node_title, $this->getSession()->getDriver()->getText('//title/a'));
117 // Verify HTML is properly escaped in the description field.
118 $this->assertRaw('<p>A paragraph</p>');
122 * Tests that nothing is output when the feed display is disabled.
124 public function testDisabledFeed() {
125 $this->drupalCreateContentType(['type' => 'page']);
126 $this->drupalCreateNode();
128 // Ensure that the feed_1 display is attached to the page_1 display.
129 $view = Views::getView('test_attached_disabled');
130 $view->setDisplay('page_1');
131 $attached_displays = $view->display_handler->getAttachedDisplays();
132 $this->assertTrue(in_array('feed_1', $attached_displays), 'The feed display is attached to the page display.');
134 // Check that the rss header is output on the page display.
135 $this->drupalGet('/test-attached-disabled');
136 $feed_header = $this->xpath('//link[@rel="alternate"]');
137 $this->assertEqual($feed_header[0]->getAttribute('type'), 'application/rss+xml', 'The feed link has the type application/rss+xml.');
138 $this->assertTrue(strpos($feed_header[0]->getAttribute('href'), 'test-attached-disabled.xml'), 'Page display contains the correct feed URL.');
140 // Disable the feed display.
141 $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
144 // Ensure there is no link rel present on the page.
145 $this->drupalGet('/test-attached-disabled');
146 $result = $this->xpath('//link[@rel="alternate"]');
147 $this->assertTrue(empty($result), 'Page display does not contain a feed header.');
149 // Ensure the feed attachment returns 'Not found'.
150 $this->drupalGet('/test-attached-disabled.xml');
151 $this->assertResponse(404);
155 * Tests that the feed display works when the linked display is disabled.
157 public function testDisabledLinkedDisplay() {
158 $view = Views::getView('test_attached_disabled');
160 // Disable the page and link the feed to the page.
161 $view->displayHandlers->get('feed_1')->setOption('link_display', 'page_1');
162 $view->displayHandlers->get('page_1')->setOption('enabled', FALSE);
165 \Drupal::service('router.builder')->rebuild();
167 $this->drupalGet('test-attached-disabled');
168 $this->assertResponse(404);
169 // Ensure the feed can still be reached.
170 $this->drupalGet('test-attached-disabled.xml');
171 $this->assertResponse(200);