Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / DisplayFeedTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the feed display plugin.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\display\Feed
13  */
14 class DisplayFeedTest extends ViewTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_display_feed', 'test_attached_disabled', 'test_feed_icon'];
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['block', 'node', 'views'];
29
30   protected function setUp($import_test_views = TRUE) {
31     parent::setUp($import_test_views);
32
33     $this->enableViewsTestModule();
34
35     $admin_user = $this->drupalCreateUser(['administer site configuration']);
36     $this->drupalLogin($admin_user);
37   }
38
39   /**
40    * Tests the rendered output.
41    */
42   public function testFeedOutput() {
43     $this->drupalCreateContentType(['type' => 'page']);
44
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,
49       'body' => [
50         0 => [
51           'value' => 'A paragraph',
52           'format' => filter_default_format(),
53         ],
54       ],
55     ]);
56
57     // Test the site name setting.
58     $site_name = $this->randomMachineName();
59     $this->config('system.site')->set('name', $site_name)->save();
60
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('&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     $this->assertEquals('test_display_feed', $this->getSession()->getDriver()->getText('//title'));
74
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';
79     $view->save();
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.');
85
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.');
95   }
96
97   /**
98    * Tests the rendered output for fields display.
99    */
100   public function testFeedFieldOutput() {
101     $this->drupalCreateContentType(['type' => 'page']);
102
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,
107       'body' => [
108         0 => [
109           'value' => 'A paragraph',
110           'format' => filter_default_format(),
111         ],
112       ],
113     ]);
114
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('&lt;p&gt;A paragraph&lt;/p&gt;');
119   }
120
121   /**
122    * Tests that nothing is output when the feed display is disabled.
123    */
124   public function testDisabledFeed() {
125     $this->drupalCreateContentType(['type' => 'page']);
126     $this->drupalCreateNode();
127
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.');
133
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.');
139
140     // Disable the feed display.
141     $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
142     $view->save();
143
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.');
148
149     // Ensure the feed attachment returns 'Not found'.
150     $this->drupalGet('/test-attached-disabled.xml');
151     $this->assertResponse(404);
152   }
153
154   /**
155    * Tests that the feed display works when the linked display is disabled.
156    */
157   public function testDisabledLinkedDisplay() {
158     $view = Views::getView('test_attached_disabled');
159     $view->setDisplay();
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);
163     $view->save();
164
165     \Drupal::service('router.builder')->rebuild();
166
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);
172   }
173
174 }