0efa336adecb8d71cf4b74986cc4e920b58efc4a
[yaffs-website] / src / Functional / NodeRSSContentTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\filter\Entity\FilterFormat;
6
7 /**
8  * Ensures that data added to nodes by other modules appears in RSS feeds.
9  *
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.
13  *
14  * @group node
15  */
16 class NodeRSSContentTest extends NodeTestBase {
17
18   /**
19    * Enable a module that implements hook_node_view().
20    *
21    * @var array
22    */
23   public static $modules = ['node_test', 'views'];
24
25   protected function setUp() {
26     parent::setUp();
27
28     // Use bypass node access permission here, because the test class uses
29     // hook_grants_alter() to deny access to everyone on node_access
30     // queries.
31     $user = $this->drupalCreateUser(['bypass node access', 'access content', 'create article content']);
32     $this->drupalLogin($user);
33   }
34
35   /**
36    * Ensures that a new node includes the custom data when added to an RSS feed.
37    */
38   public function testNodeRSSContent() {
39     // Create a node.
40     $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
41
42     $this->drupalGet('rss.xml');
43
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.');
47
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.');
52
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.');
58
59     // Check that content added in 'rss' view mode doesn't appear when
60     // viewing node.
61     $this->drupalGet('node/' . $node->id());
62     $this->assertNoText($rss_only_content, 'Node content designed for RSS does not appear when viewing node.');
63   }
64
65   /**
66    * Tests relative, root-relative, protocol-relative and absolute URLs.
67    */
68   public function testUrlHandling() {
69     // Only the plain_text text format is available by default, which escapes
70     // all HTML.
71     FilterFormat::create([
72       'format' => 'full_html',
73       'name' => 'Full HTML',
74       'filters' => [],
75     ])->save();
76
77     $defaults = [
78       'type' => 'article',
79       'promote' => 1,
80     ];
81     $this->drupalCreateNode($defaults + [
82       'body' => [
83         'value' => '<p><a href="' . file_url_transform_relative(file_create_url('public://root-relative')) . '">Root-relative URL</a></p>',
84         'format' => 'full_html',
85       ],
86     ]);
87     $protocol_relative_url = substr(file_create_url('public://protocol-relative'), strlen(\Drupal::request()->getScheme() . ':'));
88     $this->drupalCreateNode($defaults + [
89       'body' => [
90         'value' => '<p><a href="' . $protocol_relative_url . '">Protocol-relative URL</a></p>',
91         'format' => 'full_html',
92       ],
93     ]);
94     $absolute_url = file_create_url('public://absolute');
95     $this->drupalCreateNode($defaults + [
96       'body' => [
97         'value' => '<p><a href="' . $absolute_url . '">Absolute URL</a></p>',
98         'format' => 'full_html',
99       ],
100     ]);
101
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.');
106   }
107
108 }