NodeRSSContentTest.php 3.99 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\node\Tests;

5 6
use Drupal\filter\Entity\FilterFormat;

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 12
 * added to the node's renderable array, then verify that the data appears on
 * the site-wide RSS feed at rss.xml.
13 14
 *
 * @group node
15 16
 */
class NodeRSSContentTest extends NodeTestBase {
17 18 19 20 21 22

  /**
   * Enable a module that implements hook_node_view().
   *
   * @var array
   */
23
  public static $modules = array('node_test', 'views');
24

25
  protected function setUp() {
26
    parent::setUp();
27 28 29 30 31 32 33 34 35

    // Use bypass node access permission here, because the test class uses
    // hook_grants_alter() to deny access to everyone on node_access
    // queries.
    $user = $this->drupalCreateUser(array('bypass node access', 'access content', 'create article content'));
    $this->drupalLogin($user);
  }

  /**
36
   * Ensures that a new node includes the custom data when added to an RSS feed.
37 38 39 40 41 42 43 44
   */
  function testNodeRSSContent() {
    // Create a node.
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));

    $this->drupalGet('rss.xml');

    // 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.', array('@nid' => $node->id()));
46
    $this->assertText($rss_only_content, 'Node content designated for RSS appear in RSS feed.');
47 48 49

    // Check that content added in view modes other than 'rss' doesn't
    // appear in RSS feed.
50
    $non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node @nid.', array('@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.', array('@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 60

    // Check that content added in 'rss' view mode doesn't appear when
    // 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  /**
   * Tests relative, root-relative, protocol-relative and absolute URLs.
   */
  public function testUrlHandling() {
    // Only the plain_text text format is available by default, which escapes
    // all HTML.
    FilterFormat::create([
      'format' => 'full_html',
      'name' => 'Full HTML',
      'filters' => [],
    ])->save();

    $defaults = [
      'type' => 'article',
      'promote' => 1,
    ];
    $this->drupalCreateNode($defaults + [
      'body' => [
        'value' => '<p><a href="' . file_url_transform_relative(file_create_url('public://root-relative')) . '">Root-relative URL</a></p>',
        'format' => 'full_html',
      ],
    ]);
    $protocol_relative_url = substr(file_create_url('public://protocol-relative'), strlen(\Drupal::request()->getScheme() . ':'));
    $this->drupalCreateNode($defaults + [
      'body' => [
        'value' => '<p><a href="' . $protocol_relative_url . '">Protocol-relative URL</a></p>',
        'format' => 'full_html',
      ],
    ]);
    $absolute_url = file_create_url('public://absolute');
    $this->drupalCreateNode($defaults + [
      'body' => [
        'value' => '<p><a href="' . $absolute_url . '">Absolute URL</a></p>',
        'format' => 'full_html',
      ],
    ]);

    $this->drupalGet('rss.xml');
    $this->assertRaw(file_create_url('public://root-relative'), 'Root-relative URL is transformed to absolute.');
    $this->assertRaw($protocol_relative_url, 'Protocol-relative URL is left untouched.');
    $this->assertRaw($absolute_url, 'Absolute URL is left untouched.');
  }

108
}