3 namespace Drupal\system\Tests\Common;
6 use Drupal\simpletest\WebTestBase;
9 * Make sure that attaching feeds works correctly with various constructs.
13 class AddFeedTest extends WebTestBase {
16 * Tests attaching feeds with paths, URLs, and titles.
18 public function testBasicFeedAddNoTitle() {
19 $path = $this->randomMachineName(12);
20 $external_url = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
21 $fully_qualified_local_url = Url::fromUri('base:' . $this->randomMachineName(12), ['absolute' => TRUE])->toString();
23 $path_for_title = $this->randomMachineName(12);
24 $external_for_title = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
25 $fully_qualified_for_title = Url::fromUri('base:' . $this->randomMachineName(12), ['absolute' => TRUE])->toString();
28 'path without title' => [
29 'url' => Url::fromUri('base:' . $path, ['absolute' => TRUE])->toString(),
32 'external URL without title' => [
33 'url' => $external_url,
36 'local URL without title' => [
37 'url' => $fully_qualified_local_url,
40 'path with title' => [
41 'url' => Url::fromUri('base:' . $path_for_title, ['absolute' => TRUE])->toString(),
42 'title' => $this->randomMachineName(12),
44 'external URL with title' => [
45 'url' => $external_for_title,
46 'title' => $this->randomMachineName(12),
48 'local URL with title' => [
49 'url' => $fully_qualified_for_title,
50 'title' => $this->randomMachineName(12),
55 foreach ($urls as $feed_info) {
56 $build['#attached']['feed'][] = [$feed_info['url'], $feed_info['title']];
59 // Use the bare HTML page renderer to render our links.
60 $renderer = $this->container->get('bare_html_page_renderer');
61 $response = $renderer->renderBarePage($build, '', 'maintenance_page');
62 // Glean the content from the response object.
63 $this->setRawContent($response->getContent());
64 // Assert that the content contains the RSS links we specified.
65 foreach ($urls as $description => $feed_info) {
66 $this->assertPattern($this->urlToRSSLinkPattern($feed_info['url'], $feed_info['title']), format_string('Found correct feed header for %description', ['%description' => $description]));
71 * Creates a pattern representing the RSS feed in the page.
73 public function urlToRSSLinkPattern($url, $title = '') {
74 // Escape any regular expression characters in the URL ('?' is the worst).
75 $url = preg_replace('/([+?.*])/', '[$0]', $url);
76 $generated_pattern = '%<link +href="' . $url . '" +rel="alternate" +title="' . $title . '" +type="application/rss.xml" */>%';
77 return $generated_pattern;
81 * Checks that special characters are correctly escaped.
83 * @see https://www.drupal.org/node/1211668
85 public function testFeedIconEscaping() {
87 '#theme' => 'feed_icon',
91 $text = \Drupal::service('renderer')->renderRoot($variables);
92 $this->assertEqual(trim(strip_tags($text)), 'Subscribe to <>&"'', 'feed_icon template escapes reserved HTML characters.');