You are here

public function UrlRewritingTest::testPublicManagedFileURL in Drupal 8

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

File

core/tests/Drupal/KernelTests/Core/File/UrlRewritingTest.php, line 71

Class

UrlRewritingTest
Tests for file URL rewriting.

Namespace

Drupal\KernelTests\Core\File

Code

public function testPublicManagedFileURL() {

  // 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 = file_create_url($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
    ->assertEqual(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 = file_create_url($uri);
  $this
    ->assertEqual(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 = file_create_url($uri);
  $this
    ->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . $file_system
    ->basename($uri), $url, 'Correctly generated a protocol-relative URL for a created file.');
}