View source  
  <?php
namespace Drupal\Tests\node\Functional;
use Drupal\filter\Entity\FilterFormat;
class NodeRSSContentTest extends NodeTestBase {
  
  public static $modules = [
    'node_test',
    'views',
  ];
  
  protected $defaultTheme = 'stark';
  protected function setUp() {
    parent::setUp();
    
    $user = $this
      ->drupalCreateUser([
      'bypass node access',
      'access content',
      'create article content',
    ]);
    $this
      ->drupalLogin($user);
  }
  
  public function testNodeRSSContent() {
    
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
    ]);
    $this
      ->drupalGet('rss.xml');
    
    $rss_only_content = t('Extra data that should appear only in the RSS feed for node @nid.', [
      '@nid' => $node
        ->id(),
    ]);
    $this
      ->assertText($rss_only_content, 'Node content designated for RSS appear in RSS feed.');
    
    $non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node @nid.', [
      '@nid' => $node
        ->id(),
    ]);
    $this
      ->assertNoText($non_rss_content, 'Node content not designed for RSS does not appear in RSS feed.');
    
    $test_element = '<testElement>' . t('Value of testElement RSS element for node @nid.', [
      '@nid' => $node
        ->id(),
    ]) . '</testElement>';
    $test_ns = 'xmlns:drupaltest="http://example.com/test-namespace"';
    $this
      ->assertRaw($test_element, 'Extra RSS elements appear in RSS feed.');
    $this
      ->assertRaw($test_ns, 'Extra namespaces appear in RSS feed.');
    
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertNoText($rss_only_content, 'Node content designed for RSS does not appear when viewing node.');
  }
  
  public function testUrlHandling() {
    
    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.');
  }
}