View source
<?php
namespace Drupal\Tests\system\Kernel\Common;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
class AddFeedTest extends KernelTestBase {
protected static $modules = [
'system',
];
public function testBasicFeedAddNoTitle() {
$path = $this
->randomMachineName(12);
$external_url = 'http://' . $this
->randomMachineName(12) . '/' . $this
->randomMachineName(12);
$fully_qualified_local_url = Url::fromUri('base:' . $this
->randomMachineName(12), [
'absolute' => TRUE,
])
->toString();
$path_for_title = $this
->randomMachineName(12);
$external_for_title = 'http://' . $this
->randomMachineName(12) . '/' . $this
->randomMachineName(12);
$fully_qualified_for_title = Url::fromUri('base:' . $this
->randomMachineName(12), [
'absolute' => TRUE,
])
->toString();
$urls = [
'path without title' => [
'url' => Url::fromUri('base:' . $path, [
'absolute' => TRUE,
])
->toString(),
'title' => '',
],
'external URL without title' => [
'url' => $external_url,
'title' => '',
],
'local URL without title' => [
'url' => $fully_qualified_local_url,
'title' => '',
],
'path with title' => [
'url' => Url::fromUri('base:' . $path_for_title, [
'absolute' => TRUE,
])
->toString(),
'title' => $this
->randomMachineName(12),
],
'external URL with title' => [
'url' => $external_for_title,
'title' => $this
->randomMachineName(12),
],
'local URL with title' => [
'url' => $fully_qualified_for_title,
'title' => $this
->randomMachineName(12),
],
];
$build = [];
foreach ($urls as $feed_info) {
$build['#attached']['feed'][] = [
$feed_info['url'],
$feed_info['title'],
];
}
$renderer = $this->container
->get('bare_html_page_renderer');
$response = $renderer
->renderBarePage($build, '', 'maintenance_page');
$this
->setRawContent($response
->getContent());
foreach ($urls as $description => $feed_info) {
$this
->assertPattern($this
->urlToRSSLinkPattern($feed_info['url'], $feed_info['title']));
}
}
public function urlToRSSLinkPattern($url, $title = '') {
$url = preg_replace('/([+?.*])/', '[$0]', $url);
$generated_pattern = '%<link +href="' . $url . '" +rel="alternate" +title="' . $title . '" +type="application/rss.xml" */>%';
return $generated_pattern;
}
public function testFeedIconEscaping() {
$variables = [
'#theme' => 'feed_icon',
'#url' => 'node',
'#title' => '<>&"\'',
];
$text = \Drupal::service('renderer')
->renderRoot($variables);
$this
->assertEquals('Subscribe to <>&"'', trim(strip_tags($text)), 'feed_icon template escapes reserved HTML characters.');
}
}