3 namespace Drupal\Tests\system\Kernel\Common;
6 use Drupal\KernelTests\KernelTestBase;
9 * Make sure that attaching feeds works correctly with various constructs.
13 class AddFeedTest extends KernelTestBase {
18 public static $modules = ['system'];
21 * Tests attaching feeds with paths, URLs, and titles.
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();
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();
33 'path without title' => [
34 'url' => Url::fromUri('base:' . $path, ['absolute' => TRUE])->toString(),
37 'external URL without title' => [
38 'url' => $external_url,
41 'local URL without title' => [
42 'url' => $fully_qualified_local_url,
45 'path with title' => [
46 'url' => Url::fromUri('base:' . $path_for_title, ['absolute' => TRUE])->toString(),
47 'title' => $this->randomMachineName(12),
49 'external URL with title' => [
50 'url' => $external_for_title,
51 'title' => $this->randomMachineName(12),
53 'local URL with title' => [
54 'url' => $fully_qualified_for_title,
55 'title' => $this->randomMachineName(12),
60 foreach ($urls as $feed_info) {
61 $build['#attached']['feed'][] = [$feed_info['url'], $feed_info['title']];
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]));
76 * Creates a pattern representing the RSS feed in the page.
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;
86 * Checks that special characters are correctly escaped.
88 * @see https://www.drupal.org/node/1211668
90 public function testFeedIconEscaping() {
92 '#theme' => 'feed_icon',
96 $text = \Drupal::service('renderer')->renderRoot($variables);
97 $this->assertEqual(trim(strip_tags($text)), 'Subscribe to <>&"'', 'feed_icon template escapes reserved HTML characters.');