3 namespace Drupal\views\Tests\Plugin;
5 use Drupal\views\Views;
8 * Tests the feed display plugin.
11 * @see \Drupal\views\Plugin\views\display\Feed
13 class DisplayFeedTest extends PluginTestBase {
16 * Views used by this test.
20 public static $testViews = ['test_display_feed', 'test_attached_disabled', 'test_feed_icon'];
27 public static $modules = ['block', 'node', 'views'];
29 protected function setUp($import_test_views = TRUE) {
30 parent::setUp($import_test_views);
32 $this->enableViewsTestModule();
34 $admin_user = $this->drupalCreateUser(['administer site configuration']);
35 $this->drupalLogin($admin_user);
39 * Tests the rendered output.
41 public function testFeedOutput() {
42 $this->drupalCreateContentType(['type' => 'page']);
44 // Verify a title with HTML entities is properly escaped.
45 $node_title = 'This "cool" & "neat" article\'s title';
46 $node = $this->drupalCreateNode([
47 'title' => $node_title,
50 'value' => 'A paragraph',
51 'format' => filter_default_format(),
56 // Test the site name setting.
57 $site_name = $this->randomMachineName();
58 $this->config('system.site')->set('name', $site_name)->save();
60 $this->drupalGet('test-feed-display.xml');
61 $result = $this->xpath('//title');
62 $this->assertEqual($result[0], $site_name, 'The site title is used for the feed title.');
63 $this->assertEqual($result[1], $node_title, 'Node title with HTML entities displays correctly.');
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 $result = $this->xpath('//title');
74 $this->assertEqual($result[0], 'test_display_feed', 'The display title is used for the feed title.');
76 // Add a block display and attach the feed.
77 $view->getExecutable()->newDisplay('block', NULL, 'test');
78 $display = &$view->getDisplay('feed_1');
79 $display['display_options']['displays']['test'] = 'test';
81 // Test the feed display adds a feed icon to the block display.
82 $this->drupalPlaceBlock('views_block:test_display_feed-test');
83 $this->drupalGet('<front>');
84 $feed_icon = $this->cssSelect('div.view-id-test_display_feed a.feed-icon');
85 $this->assertTrue(strpos($feed_icon[0]['href'], 'test-feed-display.xml'), 'The feed icon was found.');
87 // Test feed display attached to page display with arguments.
88 $this->drupalGet('test-feed-icon/' . $node->id());
89 $page_url = $this->getUrl();
90 $icon_href = $this->cssSelect('a.feed-icon[href *= "test-feed-icon"]')[0]['href'];
91 $this->assertEqual($icon_href, $page_url . '/feed', 'The feed icon was found.');
92 $link_href = $this->cssSelect('link[type = "application/rss+xml"][href *= "test-feed-icon"]')[0]['href'];
93 $this->assertEqual($link_href, $page_url . '/feed', 'The RSS link was found.');
94 $feed_link = simplexml_load_string($this->drupalGet($icon_href))->channel->link;
95 $this->assertEqual($feed_link, $page_url, 'The channel link was found.');
99 * Tests the rendered output for fields display.
101 public function testFeedFieldOutput() {
102 $this->drupalCreateContentType(['type' => 'page']);
104 // Verify a title with HTML entities is properly escaped.
105 $node_title = 'This "cool" & "neat" article\'s title';
106 $this->drupalCreateNode([
107 'title' => $node_title,
110 'value' => 'A paragraph',
111 'format' => filter_default_format(),
116 $this->drupalGet('test-feed-display-fields.xml');
117 $result = $this->xpath('//title/a');
118 $this->assertEqual($result[0], $node_title, 'Node title with HTML entities displays correctly.');
119 // Verify HTML is properly escaped in the description field.
120 $this->assertRaw('<p>A paragraph</p>');
124 * Tests that nothing is output when the feed display is disabled.
126 public function testDisabledFeed() {
127 $this->drupalCreateContentType(['type' => 'page']);
128 $this->drupalCreateNode();
130 // Ensure that the feed_1 display is attached to the page_1 display.
131 $view = Views::getView('test_attached_disabled');
132 $view->setDisplay('page_1');
133 $attached_displays = $view->display_handler->getAttachedDisplays();
134 $this->assertTrue(in_array('feed_1', $attached_displays), 'The feed display is attached to the page display.');
136 // Check that the rss header is output on the page display.
137 $this->drupalGet('/test-attached-disabled');
138 $feed_header = $this->xpath('//link[@rel="alternate"]');
139 $this->assertEqual($feed_header[0]['type'], 'application/rss+xml', 'The feed link has the type application/rss+xml.');
140 $this->assertTrue(strpos($feed_header[0]['href'], 'test-attached-disabled.xml'), 'Page display contains the correct feed URL.');
142 // Disable the feed display.
143 $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
146 // Ensure there is no link rel present on the page.
147 $this->drupalGet('/test-attached-disabled');
148 $result = $this->xpath('//link[@rel="alternate"]');
149 $this->assertTrue(empty($result), 'Page display does not contain a feed header.');
151 // Ensure the feed attachment returns 'Not found'.
152 $this->drupalGet('/test-attached-disabled.xml');
153 $this->assertResponse(404);
157 * Tests that the feed display works when the linked display is disabled.
159 public function testDisabledLinkedDisplay() {
160 $view = Views::getView('test_attached_disabled');
162 // Disable the page and link the feed to the page.
163 $view->displayHandlers->get('feed_1')->setOption('link_display', 'page_1');
164 $view->displayHandlers->get('page_1')->setOption('enabled', FALSE);
167 \Drupal::service('router.builder')->rebuild();
169 $this->drupalGet('test-attached-disabled');
170 $this->assertResponse(404);
171 // Ensure the feed can still be reached.
172 $this->drupalGet('test-attached-disabled.xml');
173 $this->assertResponse(200);