You are here

public function NodeRSSContentTest::testUrlHandling in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeRSSContentTest.php \Drupal\Tests\node\Functional\NodeRSSContentTest::testUrlHandling()

Tests relative, root-relative, protocol-relative and absolute URLs.

File

core/modules/node/tests/src/Functional/NodeRSSContentTest.php, line 77

Class

NodeRSSContentTest
Ensures that data added to nodes by other modules appears in RSS feeds.

Namespace

Drupal\Tests\node\Functional

Code

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,
  ];

  /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
  $file_url_generator = \Drupal::service('file_url_generator');
  $this
    ->drupalCreateNode($defaults + [
    'body' => [
      'value' => '<p><a href="' . $file_url_generator
        ->generateString('public://root-relative') . '">Root-relative URL</a></p>',
      'format' => 'full_html',
    ],
  ]);
  $protocol_relative_url = substr($file_url_generator
    ->generateAbsoluteString('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_url_generator
    ->generateAbsoluteString('public://absolute');
  $this
    ->drupalCreateNode($defaults + [
    'body' => [
      'value' => '<p><a href="' . $absolute_url . '">Absolute URL</a></p>',
      'format' => 'full_html',
    ],
  ]);
  $this
    ->drupalGet('rss.xml');

  // Verify that root-relative URL is transformed to absolute.
  $this
    ->assertSession()
    ->responseContains($file_url_generator
    ->generateAbsoluteString('public://root-relative'));

  // Verify that protocol-relative URL is left untouched.
  $this
    ->assertSession()
    ->responseContains($protocol_relative_url);

  // Verify that absolute URL is left untouched.
  $this
    ->assertSession()
    ->responseContains($absolute_url);
}