You are here

private function DownloadTest::checkUrl in Drupal 9

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

Download a file from the URL generated by generateString().

Create a file with the specified scheme, directory and filename; check that the URL generated by FileUrlGeneratorInterface::generateString() for the specified file equals the specified URL; fetch the URL and then compare the contents to the file.

Parameters

string $scheme: A scheme, e.g. "public".

string $directory: A directory, possibly "".

string $filename: A filename.

string $expected_url: The expected URL.

1 call to DownloadTest::checkUrl()
DownloadTest::testFileCreateUrl in core/modules/file/tests/src/Functional/DownloadTest.php
Test FileUrlGeneratorInterface::generateString()

File

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

Class

DownloadTest
Tests for download/file transfer functions.

Namespace

Drupal\Tests\file\Functional

Code

private function checkUrl($scheme, $directory, $filename, $expected_url) {

  // Convert $filename to a valid filename, i.e. strip characters not
  // supported by the filesystem, and create the file in the specified
  // directory.
  $filepath = \Drupal::service('file_system')
    ->createFilename($filename, $directory);
  $directory_uri = $scheme . '://' . dirname($filepath);
  \Drupal::service('file_system')
    ->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY);
  $file = $this
    ->createFile($filepath, NULL, $scheme);
  $url = $this->fileUrlGenerator
    ->generateAbsoluteString($file
    ->getFileUri());
  $this
    ->assertEquals($expected_url, $url);
  if ($scheme == 'private') {

    // Tell the implementation of hook_file_download() in file_test.module
    // that this file may be downloaded.
    file_test_set_return('download', [
      'x-foo' => 'Bar',
    ]);
  }
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains(file_get_contents($file
    ->getFileUri()));
  $file
    ->delete();
}