3 namespace Drupal\Tests\node\Functional;
5 use Drupal\filter\Entity\FilterFormat;
8 * Ensures that data added to nodes by other modules appears in RSS feeds.
10 * Create a node, enable the node_test module to ensure that extra data is
11 * added to the node's renderable array, then verify that the data appears on
12 * the site-wide RSS feed at rss.xml.
16 class NodeRSSContentTest extends NodeTestBase {
19 * Enable a module that implements hook_node_view().
23 public static $modules = ['node_test', 'views'];
25 protected function setUp() {
28 // Use bypass node access permission here, because the test class uses
29 // hook_grants_alter() to deny access to everyone on node_access
31 $user = $this->drupalCreateUser(['bypass node access', 'access content', 'create article content']);
32 $this->drupalLogin($user);
36 * Ensures that a new node includes the custom data when added to an RSS feed.
38 public function testNodeRSSContent() {
40 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
42 $this->drupalGet('rss.xml');
44 // Check that content added in 'rss' view mode appear in RSS feed.
45 $rss_only_content = t('Extra data that should appear only in the RSS feed for node @nid.', ['@nid' => $node->id()]);
46 $this->assertText($rss_only_content, 'Node content designated for RSS appear in RSS feed.');
48 // Check that content added in view modes other than 'rss' doesn't
49 // appear in RSS feed.
50 $non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node @nid.', ['@nid' => $node->id()]);
51 $this->assertNoText($non_rss_content, 'Node content not designed for RSS does not appear in RSS feed.');
53 // Check that extra RSS elements and namespaces are added to RSS feed.
54 $test_element = '<testElement>' . t('Value of testElement RSS element for node @nid.', ['@nid' => $node->id()]) . '</testElement>';
55 $test_ns = 'xmlns:drupaltest="http://example.com/test-namespace"';
56 $this->assertRaw($test_element, 'Extra RSS elements appear in RSS feed.');
57 $this->assertRaw($test_ns, 'Extra namespaces appear in RSS feed.');
59 // Check that content added in 'rss' view mode doesn't appear when
61 $this->drupalGet('node/' . $node->id());
62 $this->assertNoText($rss_only_content, 'Node content designed for RSS does not appear when viewing node.');
66 * Tests relative, root-relative, protocol-relative and absolute URLs.
68 public function testUrlHandling() {
69 // Only the plain_text text format is available by default, which escapes
71 FilterFormat::create([
72 'format' => 'full_html',
73 'name' => 'Full HTML',
81 $this->drupalCreateNode($defaults + [
83 'value' => '<p><a href="' . file_url_transform_relative(file_create_url('public://root-relative')) . '">Root-relative URL</a></p>',
84 'format' => 'full_html',
87 $protocol_relative_url = substr(file_create_url('public://protocol-relative'), strlen(\Drupal::request()->getScheme() . ':'));
88 $this->drupalCreateNode($defaults + [
90 'value' => '<p><a href="' . $protocol_relative_url . '">Protocol-relative URL</a></p>',
91 'format' => 'full_html',
94 $absolute_url = file_create_url('public://absolute');
95 $this->drupalCreateNode($defaults + [
97 'value' => '<p><a href="' . $absolute_url . '">Absolute URL</a></p>',
98 'format' => 'full_html',
102 $this->drupalGet('rss.xml');
103 $this->assertRaw(file_create_url('public://root-relative'), 'Root-relative URL is transformed to absolute.');
104 $this->assertRaw($protocol_relative_url, 'Protocol-relative URL is left untouched.');
105 $this->assertRaw($absolute_url, 'Absolute URL is left untouched.');