You are here

public function HtmlTest::providerTestTransformRootRelativeUrlsToAbsolute in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::providerTestTransformRootRelativeUrlsToAbsolute()

Provides test data for testTransformRootRelativeUrlsToAbsolute().

Return value

array Test data.

File

core/tests/Drupal/Tests/Component/Utility/HtmlTest.php, line 360

Class

HtmlTest
Tests \Drupal\Component\Utility\Html.

Namespace

Drupal\Tests\Component\Utility

Code

public function providerTestTransformRootRelativeUrlsToAbsolute() {
  $data = [];

  // Random generator.
  $random = new Random();

  // One random tag name.
  $tag_name = strtolower($random
    ->name(8, TRUE));

  // A site installed either in the root of a domain or a subdirectory.
  $base_paths = [
    '/',
    '/subdir/' . $random
      ->name(8, TRUE) . '/',
  ];
  foreach ($base_paths as $base_path) {

    // The only attribute that has more than just a URL as its value, is
    // 'srcset', so special-case it.
    $data += [
      "{$tag_name}, srcset, {$base_path}: root-relative" => [
        "<{$tag_name} srcset=\"http://example.com{$base_path}already-absolute 200w, {$base_path}root-relative 300w\">root-relative test</{$tag_name}>",
        'http://example.com',
        "<{$tag_name} srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}root-relative 300w\">root-relative test</{$tag_name}>",
      ],
      "{$tag_name}, srcset, {$base_path}: protocol-relative" => [
        "<{$tag_name} srcset=\"http://example.com{$base_path}already-absolute 200w, //example.com{$base_path}protocol-relative 300w\">protocol-relative test</{$tag_name}>",
        'http://example.com',
        FALSE,
      ],
      "{$tag_name}, srcset, {$base_path}: absolute" => [
        "<{$tag_name} srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}absolute 300w\">absolute test</{$tag_name}>",
        'http://example.com',
        FALSE,
      ],
    ];
    foreach ([
      'href',
      'poster',
      'src',
      'cite',
      'data',
      'action',
      'formaction',
      'about',
    ] as $attribute) {
      $data += [
        "{$tag_name}, {$attribute}, {$base_path}: root-relative" => [
          "<{$tag_name} {$attribute}=\"{$base_path}root-relative\">root-relative test</{$tag_name}>",
          'http://example.com',
          "<{$tag_name} {$attribute}=\"http://example.com{$base_path}root-relative\">root-relative test</{$tag_name}>",
        ],
        "{$tag_name}, {$attribute}, {$base_path}: protocol-relative" => [
          "<{$tag_name} {$attribute}=\"//example.com{$base_path}protocol-relative\">protocol-relative test</{$tag_name}>",
          'http://example.com',
          FALSE,
        ],
        "{$tag_name}, {$attribute}, {$base_path}: absolute" => [
          "<{$tag_name} {$attribute}=\"http://example.com{$base_path}absolute\">absolute test</{$tag_name}>",
          'http://example.com',
          FALSE,
        ],
      ];
    }
  }
  return $data;
}