You are here

public function FileUrlGeneratorTest::testRelativeFileURL in Drupal 9

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php \Drupal\KernelTests\Core\File\FileUrlGeneratorTest::testRelativeFileURL()

Tests generate absolute string with relative URL.

@covers ::generateAbsoluteString

File

core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php, line 151

Class

FileUrlGeneratorTest
@coversDefaultClass \Drupal\Core\File\FileUrlGenerator

Namespace

Drupal\KernelTests\Core\File

Code

public function testRelativeFileURL() {

  // Disable file_test.module's hook_file_url_alter() implementation.
  \Drupal::state()
    ->set('file_test.hook_file_url_alter', NULL);

  // Create a mock Request for transformRelative().
  $request = Request::create($GLOBALS['base_url']);
  $this->container
    ->get('request_stack')
    ->push($request);
  \Drupal::setContainer($this->container);

  // Shipped file.
  $filepath = 'core/assets/vendor/jquery/jquery.min.js';
  $url = $this->fileUrlGenerator
    ->generateAbsoluteString($filepath);
  $this
    ->assertSame(base_path() . $filepath, $this->fileUrlGenerator
    ->transformRelative($url));

  // Managed file.
  $uri = $this
    ->createUri();
  $url = $this->fileUrlGenerator
    ->generateAbsoluteString($uri);
  $public_directory_path = \Drupal::service('stream_wrapper_manager')
    ->getViaScheme('public')
    ->getDirectoryPath();
  $this
    ->assertSame(base_path() . $public_directory_path . '/' . rawurlencode(\Drupal::service('file_system')
    ->basename($uri)), $this->fileUrlGenerator
    ->transformRelative($url));
}