annotate core/modules/views/src/Tests/Plugin/DisplayFeedTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Tests\Plugin;
Chris@0 4
Chris@0 5 use Drupal\views\Views;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the feed display plugin.
Chris@0 9 *
Chris@0 10 * @group views
Chris@0 11 * @see \Drupal\views\Plugin\views\display\Feed
Chris@0 12 */
Chris@0 13 class DisplayFeedTest extends PluginTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Views used by this test.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $testViews = ['test_display_feed', 'test_attached_disabled', 'test_feed_icon'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Modules to enable.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 public static $modules = ['block', 'node', 'views'];
Chris@0 28
Chris@14 29 protected function setUp($import_test_views = TRUE) {
Chris@14 30 parent::setUp($import_test_views);
Chris@0 31
Chris@0 32 $this->enableViewsTestModule();
Chris@0 33
Chris@0 34 $admin_user = $this->drupalCreateUser(['administer site configuration']);
Chris@0 35 $this->drupalLogin($admin_user);
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Tests the rendered output.
Chris@0 40 */
Chris@0 41 public function testFeedOutput() {
Chris@0 42 $this->drupalCreateContentType(['type' => 'page']);
Chris@0 43
Chris@0 44 // Verify a title with HTML entities is properly escaped.
Chris@0 45 $node_title = 'This "cool" & "neat" article\'s title';
Chris@0 46 $node = $this->drupalCreateNode([
Chris@0 47 'title' => $node_title,
Chris@0 48 'body' => [
Chris@0 49 0 => [
Chris@0 50 'value' => 'A paragraph',
Chris@0 51 'format' => filter_default_format(),
Chris@0 52 ],
Chris@0 53 ],
Chris@0 54 ]);
Chris@0 55
Chris@0 56 // Test the site name setting.
Chris@0 57 $site_name = $this->randomMachineName();
Chris@0 58 $this->config('system.site')->set('name', $site_name)->save();
Chris@0 59
Chris@0 60 $this->drupalGet('test-feed-display.xml');
Chris@0 61 $result = $this->xpath('//title');
Chris@0 62 $this->assertEqual($result[0], $site_name, 'The site title is used for the feed title.');
Chris@0 63 $this->assertEqual($result[1], $node_title, 'Node title with HTML entities displays correctly.');
Chris@0 64 // Verify HTML is properly escaped in the description field.
Chris@0 65 $this->assertRaw('&lt;p&gt;A paragraph&lt;/p&gt;');
Chris@0 66
Chris@0 67 $view = $this->container->get('entity.manager')->getStorage('view')->load('test_display_feed');
Chris@0 68 $display = &$view->getDisplay('feed_1');
Chris@0 69 $display['display_options']['sitename_title'] = 0;
Chris@0 70 $view->save();
Chris@0 71
Chris@0 72 $this->drupalGet('test-feed-display.xml');
Chris@0 73 $result = $this->xpath('//title');
Chris@0 74 $this->assertEqual($result[0], 'test_display_feed', 'The display title is used for the feed title.');
Chris@0 75
Chris@0 76 // Add a block display and attach the feed.
Chris@0 77 $view->getExecutable()->newDisplay('block', NULL, 'test');
Chris@0 78 $display = &$view->getDisplay('feed_1');
Chris@0 79 $display['display_options']['displays']['test'] = 'test';
Chris@0 80 $view->save();
Chris@0 81 // Test the feed display adds a feed icon to the block display.
Chris@0 82 $this->drupalPlaceBlock('views_block:test_display_feed-test');
Chris@0 83 $this->drupalGet('<front>');
Chris@0 84 $feed_icon = $this->cssSelect('div.view-id-test_display_feed a.feed-icon');
Chris@0 85 $this->assertTrue(strpos($feed_icon[0]['href'], 'test-feed-display.xml'), 'The feed icon was found.');
Chris@0 86
Chris@0 87 // Test feed display attached to page display with arguments.
Chris@0 88 $this->drupalGet('test-feed-icon/' . $node->id());
Chris@0 89 $page_url = $this->getUrl();
Chris@0 90 $icon_href = $this->cssSelect('a.feed-icon[href *= "test-feed-icon"]')[0]['href'];
Chris@0 91 $this->assertEqual($icon_href, $page_url . '/feed', 'The feed icon was found.');
Chris@0 92 $link_href = $this->cssSelect('link[type = "application/rss+xml"][href *= "test-feed-icon"]')[0]['href'];
Chris@0 93 $this->assertEqual($link_href, $page_url . '/feed', 'The RSS link was found.');
Chris@0 94 $feed_link = simplexml_load_string($this->drupalGet($icon_href))->channel->link;
Chris@0 95 $this->assertEqual($feed_link, $page_url, 'The channel link was found.');
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Tests the rendered output for fields display.
Chris@0 100 */
Chris@0 101 public function testFeedFieldOutput() {
Chris@0 102 $this->drupalCreateContentType(['type' => 'page']);
Chris@0 103
Chris@0 104 // Verify a title with HTML entities is properly escaped.
Chris@0 105 $node_title = 'This "cool" & "neat" article\'s title';
Chris@0 106 $this->drupalCreateNode([
Chris@0 107 'title' => $node_title,
Chris@0 108 'body' => [
Chris@0 109 0 => [
Chris@0 110 'value' => 'A paragraph',
Chris@0 111 'format' => filter_default_format(),
Chris@0 112 ],
Chris@0 113 ],
Chris@0 114 ]);
Chris@0 115
Chris@0 116 $this->drupalGet('test-feed-display-fields.xml');
Chris@0 117 $result = $this->xpath('//title/a');
Chris@0 118 $this->assertEqual($result[0], $node_title, 'Node title with HTML entities displays correctly.');
Chris@0 119 // Verify HTML is properly escaped in the description field.
Chris@0 120 $this->assertRaw('&lt;p&gt;A paragraph&lt;/p&gt;');
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Tests that nothing is output when the feed display is disabled.
Chris@0 125 */
Chris@0 126 public function testDisabledFeed() {
Chris@0 127 $this->drupalCreateContentType(['type' => 'page']);
Chris@0 128 $this->drupalCreateNode();
Chris@0 129
Chris@0 130 // Ensure that the feed_1 display is attached to the page_1 display.
Chris@0 131 $view = Views::getView('test_attached_disabled');
Chris@0 132 $view->setDisplay('page_1');
Chris@0 133 $attached_displays = $view->display_handler->getAttachedDisplays();
Chris@0 134 $this->assertTrue(in_array('feed_1', $attached_displays), 'The feed display is attached to the page display.');
Chris@0 135
Chris@0 136 // Check that the rss header is output on the page display.
Chris@0 137 $this->drupalGet('/test-attached-disabled');
Chris@0 138 $feed_header = $this->xpath('//link[@rel="alternate"]');
Chris@0 139 $this->assertEqual($feed_header[0]['type'], 'application/rss+xml', 'The feed link has the type application/rss+xml.');
Chris@0 140 $this->assertTrue(strpos($feed_header[0]['href'], 'test-attached-disabled.xml'), 'Page display contains the correct feed URL.');
Chris@0 141
Chris@0 142 // Disable the feed display.
Chris@0 143 $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
Chris@0 144 $view->save();
Chris@0 145
Chris@0 146 // Ensure there is no link rel present on the page.
Chris@0 147 $this->drupalGet('/test-attached-disabled');
Chris@0 148 $result = $this->xpath('//link[@rel="alternate"]');
Chris@0 149 $this->assertTrue(empty($result), 'Page display does not contain a feed header.');
Chris@0 150
Chris@0 151 // Ensure the feed attachment returns 'Not found'.
Chris@0 152 $this->drupalGet('/test-attached-disabled.xml');
Chris@0 153 $this->assertResponse(404);
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Tests that the feed display works when the linked display is disabled.
Chris@0 158 */
Chris@0 159 public function testDisabledLinkedDisplay() {
Chris@0 160 $view = Views::getView('test_attached_disabled');
Chris@0 161 $view->setDisplay();
Chris@0 162 // Disable the page and link the feed to the page.
Chris@0 163 $view->displayHandlers->get('feed_1')->setOption('link_display', 'page_1');
Chris@0 164 $view->displayHandlers->get('page_1')->setOption('enabled', FALSE);
Chris@0 165 $view->save();
Chris@0 166
Chris@0 167 \Drupal::service('router.builder')->rebuild();
Chris@0 168
Chris@0 169 $this->drupalGet('test-attached-disabled');
Chris@0 170 $this->assertResponse(404);
Chris@0 171 // Ensure the feed can still be reached.
Chris@0 172 $this->drupalGet('test-attached-disabled.xml');
Chris@0 173 $this->assertResponse(200);
Chris@0 174 }
Chris@0 175
Chris@0 176 }