Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Common / AddFeedTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Common;
4
5 use Drupal\Core\Url;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Make sure that attaching feeds works correctly with various constructs.
10  *
11  * @group Common
12  */
13 class AddFeedTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['system'];
19
20   /**
21    * Tests attaching feeds with paths, URLs, and titles.
22    */
23   public function testBasicFeedAddNoTitle() {
24     $path = $this->randomMachineName(12);
25     $external_url = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
26     $fully_qualified_local_url = Url::fromUri('base:' . $this->randomMachineName(12), ['absolute' => TRUE])->toString();
27
28     $path_for_title = $this->randomMachineName(12);
29     $external_for_title = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
30     $fully_qualified_for_title = Url::fromUri('base:' . $this->randomMachineName(12), ['absolute' => TRUE])->toString();
31
32     $urls = [
33       'path without title' => [
34         'url' => Url::fromUri('base:' . $path, ['absolute' => TRUE])->toString(),
35         'title' => '',
36       ],
37       'external URL without title' => [
38         'url' => $external_url,
39         'title' => '',
40       ],
41       'local URL without title' => [
42         'url' => $fully_qualified_local_url,
43         'title' => '',
44       ],
45       'path with title' => [
46         'url' => Url::fromUri('base:' . $path_for_title, ['absolute' => TRUE])->toString(),
47         'title' => $this->randomMachineName(12),
48       ],
49       'external URL with title' => [
50         'url' => $external_for_title,
51         'title' => $this->randomMachineName(12),
52       ],
53       'local URL with title' => [
54         'url' => $fully_qualified_for_title,
55         'title' => $this->randomMachineName(12),
56       ],
57     ];
58
59     $build = [];
60     foreach ($urls as $feed_info) {
61       $build['#attached']['feed'][] = [$feed_info['url'], $feed_info['title']];
62     }
63
64     // Use the bare HTML page renderer to render our links.
65     $renderer = $this->container->get('bare_html_page_renderer');
66     $response = $renderer->renderBarePage($build, '', 'maintenance_page');
67     // Glean the content from the response object.
68     $this->setRawContent($response->getContent());
69     // Assert that the content contains the RSS links we specified.
70     foreach ($urls as $description => $feed_info) {
71       $this->assertPattern($this->urlToRSSLinkPattern($feed_info['url'], $feed_info['title']), format_string('Found correct feed header for %description', ['%description' => $description]));
72     }
73   }
74
75   /**
76    * Creates a pattern representing the RSS feed in the page.
77    */
78   public function urlToRSSLinkPattern($url, $title = '') {
79     // Escape any regular expression characters in the URL ('?' is the worst).
80     $url = preg_replace('/([+?.*])/', '[$0]', $url);
81     $generated_pattern = '%<link +href="' . $url . '" +rel="alternate" +title="' . $title . '" +type="application/rss.xml" */>%';
82     return $generated_pattern;
83   }
84
85   /**
86    * Checks that special characters are correctly escaped.
87    *
88    * @see https://www.drupal.org/node/1211668
89    */
90   public function testFeedIconEscaping() {
91     $variables = [
92       '#theme' => 'feed_icon',
93       '#url' => 'node',
94       '#title' => '<>&"\'',
95     ];
96     $text = \Drupal::service('renderer')->renderRoot($variables);
97     $this->assertEqual(trim(strip_tags($text)), 'Subscribe to &lt;&gt;&amp;&quot;&#039;', 'feed_icon template escapes reserved HTML characters.');
98   }
99
100 }