Version 1
[yaffs-website] / web / core / modules / views / src / Tests / Plugin / DisplayFeedTest.php
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' => [0 => [
49         'value' => 'A paragraph',
50         'format' => filter_default_format(),
51       ]],
52     ]);
53
54     // Test the site name setting.
55     $site_name = $this->randomMachineName();
56     $this->config('system.site')->set('name', $site_name)->save();
57
58     $this->drupalGet('test-feed-display.xml');
59     $result = $this->xpath('//title');
60     $this->assertEqual($result[0], $site_name, 'The site title is used for the feed title.');
61     $this->assertEqual($result[1], $node_title, 'Node title with HTML entities displays correctly.');
62     // Verify HTML is properly escaped in the description field.
63     $this->assertRaw('&lt;p&gt;A paragraph&lt;/p&gt;');
64
65     $view = $this->container->get('entity.manager')->getStorage('view')->load('test_display_feed');
66     $display = &$view->getDisplay('feed_1');
67     $display['display_options']['sitename_title'] = 0;
68     $view->save();
69
70     $this->drupalGet('test-feed-display.xml');
71     $result = $this->xpath('//title');
72     $this->assertEqual($result[0], 'test_display_feed', 'The display title is used for the feed title.');
73
74     // Add a block display and attach the feed.
75     $view->getExecutable()->newDisplay('block', NULL, 'test');
76     $display = &$view->getDisplay('feed_1');
77     $display['display_options']['displays']['test'] = 'test';
78     $view->save();
79     // Test the feed display adds a feed icon to the block display.
80     $this->drupalPlaceBlock('views_block:test_display_feed-test');
81     $this->drupalGet('<front>');
82     $feed_icon = $this->cssSelect('div.view-id-test_display_feed a.feed-icon');
83     $this->assertTrue(strpos($feed_icon[0]['href'], 'test-feed-display.xml'), 'The feed icon was found.');
84
85     // Test feed display attached to page display with arguments.
86     $this->drupalGet('test-feed-icon/' . $node->id());
87     $page_url = $this->getUrl();
88     $icon_href = $this->cssSelect('a.feed-icon[href *= "test-feed-icon"]')[0]['href'];
89     $this->assertEqual($icon_href, $page_url . '/feed', 'The feed icon was found.');
90     $link_href = $this->cssSelect('link[type = "application/rss+xml"][href *= "test-feed-icon"]')[0]['href'];
91     $this->assertEqual($link_href, $page_url . '/feed', 'The RSS link was found.');
92     $feed_link = simplexml_load_string($this->drupalGet($icon_href))->channel->link;
93     $this->assertEqual($feed_link, $page_url, 'The channel link was found.');
94   }
95
96   /**
97    * Tests the rendered output for fields display.
98    */
99   public function testFeedFieldOutput() {
100     $this->drupalCreateContentType(['type' => 'page']);
101
102     // Verify a title with HTML entities is properly escaped.
103     $node_title = 'This "cool" & "neat" article\'s title';
104     $this->drupalCreateNode([
105       'title' => $node_title,
106       'body' => [0 => [
107         'value' => 'A paragraph',
108         'format' => filter_default_format(),
109       ]],
110     ]);
111
112     $this->drupalGet('test-feed-display-fields.xml');
113     $result = $this->xpath('//title/a');
114     $this->assertEqual($result[0], $node_title, 'Node title with HTML entities displays correctly.');
115     // Verify HTML is properly escaped in the description field.
116     $this->assertRaw('&lt;p&gt;A paragraph&lt;/p&gt;');
117   }
118
119   /**
120    * Tests that nothing is output when the feed display is disabled.
121    */
122   public function testDisabledFeed() {
123     $this->drupalCreateContentType(['type' => 'page']);
124     $this->drupalCreateNode();
125
126     // Ensure that the feed_1 display is attached to the page_1 display.
127     $view = Views::getView('test_attached_disabled');
128     $view->setDisplay('page_1');
129     $attached_displays = $view->display_handler->getAttachedDisplays();
130     $this->assertTrue(in_array('feed_1', $attached_displays), 'The feed display is attached to the page display.');
131
132     // Check that the rss header is output on the page display.
133     $this->drupalGet('/test-attached-disabled');
134     $feed_header = $this->xpath('//link[@rel="alternate"]');
135     $this->assertEqual($feed_header[0]['type'], 'application/rss+xml', 'The feed link has the type application/rss+xml.');
136     $this->assertTrue(strpos($feed_header[0]['href'], 'test-attached-disabled.xml'), 'Page display contains the correct feed URL.');
137
138     // Disable the feed display.
139     $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
140     $view->save();
141
142     // Ensure there is no link rel present on the page.
143     $this->drupalGet('/test-attached-disabled');
144     $result = $this->xpath('//link[@rel="alternate"]');
145     $this->assertTrue(empty($result), 'Page display does not contain a feed header.');
146
147     // Ensure the feed attachment returns 'Not found'.
148     $this->drupalGet('/test-attached-disabled.xml');
149     $this->assertResponse(404);
150   }
151
152   /**
153    * Tests that the feed display works when the linked display is disabled.
154    */
155   public function testDisabledLinkedDisplay() {
156     $view = Views::getView('test_attached_disabled');
157     $view->setDisplay();
158     // Disable the page and link the feed to the page.
159     $view->displayHandlers->get('feed_1')->setOption('link_display', 'page_1');
160     $view->displayHandlers->get('page_1')->setOption('enabled', FALSE);
161     $view->save();
162
163     \Drupal::service('router.builder')->rebuild();
164
165     $this->drupalGet('test-attached-disabled');
166     $this->assertResponse(404);
167     // Ensure the feed can still be reached.
168     $this->drupalGet('test-attached-disabled.xml');
169     $this->assertResponse(200);
170   }
171
172 }