comparison core/modules/views/src/Tests/Plugin/DisplayFeedTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\views\Tests\Plugin;
4
5 use Drupal\views\Views;
6
7 /**
8 * Tests the feed display plugin.
9 *
10 * @group views
11 * @see \Drupal\views\Plugin\views\display\Feed
12 */
13 class DisplayFeedTest extends PluginTestBase {
14
15 /**
16 * Views used by this test.
17 *
18 * @var array
19 */
20 public static $testViews = ['test_display_feed', 'test_attached_disabled', 'test_feed_icon'];
21
22 /**
23 * Modules to enable.
24 *
25 * @var array
26 */
27 public static $modules = ['block', 'node', 'views'];
28
29 protected function setUp() {
30 parent::setUp();
31
32 $this->enableViewsTestModule();
33
34 $admin_user = $this->drupalCreateUser(['administer site configuration']);
35 $this->drupalLogin($admin_user);
36 }
37
38 /**
39 * Tests the rendered output.
40 */
41 public function testFeedOutput() {
42 $this->drupalCreateContentType(['type' => 'page']);
43
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,
48 'body' => [
49 0 => [
50 'value' => 'A paragraph',
51 'format' => filter_default_format(),
52 ],
53 ],
54 ]);
55
56 // Test the site name setting.
57 $site_name = $this->randomMachineName();
58 $this->config('system.site')->set('name', $site_name)->save();
59
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('&lt;p&gt;A paragraph&lt;/p&gt;');
66
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;
70 $view->save();
71
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.');
75
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';
80 $view->save();
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.');
86
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.');
96 }
97
98 /**
99 * Tests the rendered output for fields display.
100 */
101 public function testFeedFieldOutput() {
102 $this->drupalCreateContentType(['type' => 'page']);
103
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,
108 'body' => [
109 0 => [
110 'value' => 'A paragraph',
111 'format' => filter_default_format(),
112 ],
113 ],
114 ]);
115
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('&lt;p&gt;A paragraph&lt;/p&gt;');
121 }
122
123 /**
124 * Tests that nothing is output when the feed display is disabled.
125 */
126 public function testDisabledFeed() {
127 $this->drupalCreateContentType(['type' => 'page']);
128 $this->drupalCreateNode();
129
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.');
135
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.');
141
142 // Disable the feed display.
143 $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
144 $view->save();
145
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.');
150
151 // Ensure the feed attachment returns 'Not found'.
152 $this->drupalGet('/test-attached-disabled.xml');
153 $this->assertResponse(404);
154 }
155
156 /**
157 * Tests that the feed display works when the linked display is disabled.
158 */
159 public function testDisabledLinkedDisplay() {
160 $view = Views::getView('test_attached_disabled');
161 $view->setDisplay();
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);
165 $view->save();
166
167 \Drupal::service('router.builder')->rebuild();
168
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);
174 }
175
176 }