function FileUrlGeneratorTest::testPublicManagedFileURL

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php \Drupal\KernelTests\Core\File\FileUrlGeneratorTest::testPublicManagedFileURL()
  2. 10 core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php \Drupal\KernelTests\Core\File\FileUrlGeneratorTest::testPublicManagedFileURL()

Tests the rewriting of public managed file URLs by hook_file_url_alter().

@covers ::generateAbsoluteString

File

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

Class

FileUrlGeneratorTest
@coversDefaultClass \Drupal\Core\File\FileUrlGenerator

Namespace

Drupal\KernelTests\Core\File

Code

public function testPublicManagedFileURL() : void {
    // Test generating a URL to a managed file.
    // Test alteration of file URLs to use a CDN.
    \Drupal::state()->set('file_test.hook_file_url_alter', 'cdn');
    $uri = $this->createUri();
    $url = $this->fileUrlGenerator
        ->generateAbsoluteString($uri);
    $public_directory_path = \Drupal::service('stream_wrapper_manager')->getViaScheme('public')
        ->getDirectoryPath();
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $this->assertEquals(FILE_URL_TEST_CDN_2 . '/' . $public_directory_path . '/' . $file_system->basename($uri), $url, 'Correctly generated a CDN URL for a created file.');
    // Test alteration of file URLs to use root-relative URLs.
    \Drupal::state()->set('file_test.hook_file_url_alter', 'root-relative');
    $uri = $this->createUri();
    $url = $this->fileUrlGenerator
        ->generateAbsoluteString($uri);
    $this->assertEquals(base_path() . '/' . $public_directory_path . '/' . $file_system->basename($uri), $url, 'Correctly generated a root-relative URL for a created file.');
    // Test alteration of file URLs to use a protocol-relative URLs.
    \Drupal::state()->set('file_test.hook_file_url_alter', 'protocol-relative');
    $uri = $this->createUri();
    $url = $this->fileUrlGenerator
        ->generateAbsoluteString($uri);
    $this->assertEquals('/' . base_path() . '/' . $public_directory_path . '/' . $file_system->basename($uri), $url, 'Correctly generated a protocol-relative URL for a created file.');
}

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