function HtmlTest::providerTestTransformRootRelativeUrlsToAbsolute

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::providerTestTransformRootRelativeUrlsToAbsolute()
  2. 8.9.x core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::providerTestTransformRootRelativeUrlsToAbsolute()
  3. 11.x 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 367

Class

HtmlTest
Tests \Drupal\Component\Utility\Html.

Namespace

Drupal\Tests\Component\Utility

Code

public static 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,
      ],
      "{$tag_name}, empty srcset" => [
        "<{$tag_name} srcset>empty 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,
        ],
      ];
    }
  }
  // Double-character carriage return should be normalized.
  $data['line break with double special character'] = [
    "Test without links but with\r\nsome special characters",
    'http://example.com',
    "Test without links but with\nsome special characters",
  ];
  $data['line break with single special character'] = [
    "Test without links but with&#13;\nsome special characters",
    'http://example.com',
    "Test without links but with\nsome special characters",
  ];
  $data['carriage return within html'] = [
    "<a\rhref='/node'>My link</a>",
    'http://example.com',
    '<a href="http://example.com/node">My link</a>',
  ];
  return $data;
}

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