NodeRSSContentTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\filter\Entity\FilterFormat;
  4. /**
  5. * Ensures that data added to nodes by other modules appears in RSS feeds.
  6. *
  7. * Create a node, enable the node_test module to ensure that extra data is
  8. * added to the node's renderable array, then verify that the data appears on
  9. * the site-wide RSS feed at rss.xml.
  10. *
  11. * @group node
  12. */
  13. class NodeRSSContentTest extends NodeTestBase {
  14. /**
  15. * Enable a module that implements hook_node_view().
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['node_test', 'views'];
  20. protected function setUp() {
  21. parent::setUp();
  22. // Use bypass node access permission here, because the test class uses
  23. // hook_grants_alter() to deny access to everyone on node_access
  24. // queries.
  25. $user = $this->drupalCreateUser(['bypass node access', 'access content', 'create article content']);
  26. $this->drupalLogin($user);
  27. }
  28. /**
  29. * Ensures that a new node includes the custom data when added to an RSS feed.
  30. */
  31. public function testNodeRSSContent() {
  32. // Create a node.
  33. $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
  34. $this->drupalGet('rss.xml');
  35. // Check that content added in 'rss' view mode appear in RSS feed.
  36. $rss_only_content = t('Extra data that should appear only in the RSS feed for node @nid.', ['@nid' => $node->id()]);
  37. $this->assertText($rss_only_content, 'Node content designated for RSS appear in RSS feed.');
  38. // Check that content added in view modes other than 'rss' doesn't
  39. // appear in RSS feed.
  40. $non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node @nid.', ['@nid' => $node->id()]);
  41. $this->assertNoText($non_rss_content, 'Node content not designed for RSS does not appear in RSS feed.');
  42. // Check that extra RSS elements and namespaces are added to RSS feed.
  43. $test_element = '<testElement>' . t('Value of testElement RSS element for node @nid.', ['@nid' => $node->id()]) . '</testElement>';
  44. $test_ns = 'xmlns:drupaltest="http://example.com/test-namespace"';
  45. $this->assertRaw($test_element, 'Extra RSS elements appear in RSS feed.');
  46. $this->assertRaw($test_ns, 'Extra namespaces appear in RSS feed.');
  47. // Check that content added in 'rss' view mode doesn't appear when
  48. // viewing node.
  49. $this->drupalGet('node/' . $node->id());
  50. $this->assertNoText($rss_only_content, 'Node content designed for RSS does not appear when viewing node.');
  51. }
  52. /**
  53. * Tests relative, root-relative, protocol-relative and absolute URLs.
  54. */
  55. public function testUrlHandling() {
  56. // Only the plain_text text format is available by default, which escapes
  57. // all HTML.
  58. FilterFormat::create([
  59. 'format' => 'full_html',
  60. 'name' => 'Full HTML',
  61. 'filters' => [],
  62. ])->save();
  63. $defaults = [
  64. 'type' => 'article',
  65. 'promote' => 1,
  66. ];
  67. $this->drupalCreateNode($defaults + [
  68. 'body' => [
  69. 'value' => '<p><a href="' . file_url_transform_relative(file_create_url('public://root-relative')) . '">Root-relative URL</a></p>',
  70. 'format' => 'full_html',
  71. ],
  72. ]);
  73. $protocol_relative_url = substr(file_create_url('public://protocol-relative'), strlen(\Drupal::request()->getScheme() . ':'));
  74. $this->drupalCreateNode($defaults + [
  75. 'body' => [
  76. 'value' => '<p><a href="' . $protocol_relative_url . '">Protocol-relative URL</a></p>',
  77. 'format' => 'full_html',
  78. ],
  79. ]);
  80. $absolute_url = file_create_url('public://absolute');
  81. $this->drupalCreateNode($defaults + [
  82. 'body' => [
  83. 'value' => '<p><a href="' . $absolute_url . '">Absolute URL</a></p>',
  84. 'format' => 'full_html',
  85. ],
  86. ]);
  87. $this->drupalGet('rss.xml');
  88. $this->assertRaw(file_create_url('public://root-relative'), 'Root-relative URL is transformed to absolute.');
  89. $this->assertRaw($protocol_relative_url, 'Protocol-relative URL is left untouched.');
  90. $this->assertRaw($absolute_url, 'Absolute URL is left untouched.');
  91. }
  92. }