comparison core/modules/system/src/Tests/Common/AddFeedTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9 * Make sure that attaching feeds works correctly with various constructs.
10 *
11 * @group Common
12 */
13 class AddFeedTest extends WebTestBase {
14
15 /**
16 * Tests attaching feeds with paths, URLs, and titles.
17 */
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();
22
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();
26
27 $urls = [
28 'path without title' => [
29 'url' => Url::fromUri('base:' . $path, ['absolute' => TRUE])->toString(),
30 'title' => '',
31 ],
32 'external URL without title' => [
33 'url' => $external_url,
34 'title' => '',
35 ],
36 'local URL without title' => [
37 'url' => $fully_qualified_local_url,
38 'title' => '',
39 ],
40 'path with title' => [
41 'url' => Url::fromUri('base:' . $path_for_title, ['absolute' => TRUE])->toString(),
42 'title' => $this->randomMachineName(12),
43 ],
44 'external URL with title' => [
45 'url' => $external_for_title,
46 'title' => $this->randomMachineName(12),
47 ],
48 'local URL with title' => [
49 'url' => $fully_qualified_for_title,
50 'title' => $this->randomMachineName(12),
51 ],
52 ];
53
54 $build = [];
55 foreach ($urls as $feed_info) {
56 $build['#attached']['feed'][] = [$feed_info['url'], $feed_info['title']];
57 }
58
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]));
67 }
68 }
69
70 /**
71 * Creates a pattern representing the RSS feed in the page.
72 */
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;
78 }
79
80 /**
81 * Checks that special characters are correctly escaped.
82 *
83 * @see https://www.drupal.org/node/1211668
84 */
85 public function testFeedIconEscaping() {
86 $variables = [
87 '#theme' => 'feed_icon',
88 '#url' => 'node',
89 '#title' => '<>&"\'',
90 ];
91 $text = \Drupal::service('renderer')->renderRoot($variables);
92 $this->assertEqual(trim(strip_tags($text)), 'Subscribe to &lt;&gt;&amp;&quot;&#039;', 'feed_icon template escapes reserved HTML characters.');
93 }
94
95 }