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