function NodeRSSContentTest::testUrlHandling

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeRSSContentTest.php \Drupal\Tests\node\Functional\NodeRSSContentTest::testUrlHandling()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeRSSContentTest.php \Drupal\Tests\node\Functional\NodeRSSContentTest::testUrlHandling()
  3. 11.x 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 82

Class

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

Namespace

Drupal\Tests\node\Functional

Code

public function testUrlHandling() : void {
    // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.