function DownloadTest::testFileCreateUrl

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::testFileCreateUrl()
  2. 8.9.x core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::testFileCreateUrl()
  3. 11.x core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::testFileCreateUrl()

Test FileUrlGeneratorInterface::generateString()

File

core/modules/file/tests/src/Functional/DownloadTest.php, line 151

Class

DownloadTest
Tests for download/file transfer functions.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileCreateUrl() : void {
    // "Special" ASCII characters.
    $basename = " -._~!\$'\"()*@[]?&+%#,;=:\n\x00" . "%23%25%26%2B%2F%3F" . "éøïвβ中國書۞";
    $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 = [
        '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->assertEquals('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', $this->fileUrlGenerator
        ->generateString('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', FALSE));
}

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