You are here

function DownloadTest::testFileCreateUrl in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Tests/DownloadTest.php \Drupal\file\Tests\DownloadTest::testFileCreateUrl()

Test file_create_url().

File

core/modules/file/src/Tests/DownloadTest.php, line 94
Contains \Drupal\file\Tests\DownloadTest.

Class

DownloadTest
Tests for download/file transfer functions.

Namespace

Drupal\file\Tests

Code

function testFileCreateUrl() {

  // Tilde (~) is excluded from this test because it is encoded by
  // rawurlencode() in PHP 5.2 but not in PHP 5.3, as per RFC 3986.
  // @see http://www.php.net/manual/function.rawurlencode.php#86506
  $basename = " -._!\$'\"()*@[]?&+%#,;=:\n\0" . "%23%25%26%2B%2F%3F" . "éøïвβ中國書۞";

  // Characters from various non-ASCII alphabets.
  $basename_encoded = '%20-._%21%24%27%22%28%29%2A%40%5B%5D%3F%26%2B%25%23%2C%3B%3D%3A__' . '%2523%2525%2526%252B%252F%253F' . '%C3%A9%C3%B8%C3%AF%D0%B2%CE%B2%E4%B8%AD%E5%9C%8B%E6%9B%B8%DB%9E';

  // Public files should not be served by Drupal, so their URLs should not be
  // routed through Drupal, whereas private files should be served by Drupal,
  // so they need to be. The difference is most apparent when $script_path
  // is not empty (i.e., when not using clean URLs).
  $clean_url_settings = array(
    'clean' => '',
    'unclean' => 'index.php/',
  );
  $public_directory_path = \Drupal::service('stream_wrapper_manager')
    ->getViaScheme('public')
    ->getDirectoryPath();
  foreach ($clean_url_settings as $clean_url_setting => $script_path) {
    $clean_urls = $clean_url_setting == 'clean';
    $request = $this
      ->prepareRequestForGenerator($clean_urls);
    $base_path = $request
      ->getSchemeAndHttpHost() . $request
      ->getBasePath();
    $this
      ->checkUrl('public', '', $basename, $base_path . '/' . $public_directory_path . '/' . $basename_encoded);
    $this
      ->checkUrl('private', '', $basename, $base_path . '/' . $script_path . 'system/files/' . $basename_encoded);
  }
  $this
    ->assertEqual(file_create_url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', t('Generated URL matches expected URL.'));

  // Test public files with a different host name from settings.
  $test_base_url = 'http://www.example.com/cdn';
  $this
    ->settingsSet('file_public_base_url', $test_base_url);
  $filepath = file_create_filename('test.txt', '');
  $directory_uri = 'public://' . dirname($filepath);
  file_prepare_directory($directory_uri, FILE_CREATE_DIRECTORY);
  $file = $this
    ->createFile($filepath, NULL, 'public');
  $url = file_create_url($file
    ->getFileUri());
  $expected_url = $test_base_url . '/' . basename($filepath);
  $this
    ->assertEqual($url, $expected_url);
}