protected function DownloadTest::doPrivateFileTransferTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::doPrivateFileTransferTest()
- 9 core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::doPrivateFileTransferTest()
Tests the private file transfer system.
File
- core/
modules/ file/ tests/ src/ Functional/ DownloadTest.php, line 67
Class
- DownloadTest
- Tests for download/file transfer functions.
Namespace
Drupal\Tests\file\FunctionalCode
protected function doPrivateFileTransferTest() {
// Set file downloads to private so handler functions get called.
// Create a file.
$contents = $this
->randomMachineName(8);
$file = $this
->createFile(NULL, $contents, 'private');
// Created private files without usage are by default not accessible
// for a user different from the owner, but createFile always uses uid 1
// as the owner of the files. Therefore make it permanent to allow access
// if a module allows it.
$file
->setPermanent();
$file
->save();
$url = $this->fileUrlGenerator
->generateAbsoluteString($file
->getFileUri());
// Set file_test access header to allow the download.
file_test_reset();
file_test_set_return('download', [
'x-foo' => 'Bar',
]);
$this
->drupalGet($url);
// Verify that header is set by file_test module on private download.
$this
->assertSession()
->responseHeaderEquals('x-foo', 'Bar');
// Verify that page cache is disabled on private file download.
$this
->assertSession()
->responseHeaderDoesNotExist('x-drupal-cache');
$this
->assertSession()
->statusCodeEquals(200);
// Ensure hook_file_download is fired correctly.
$this
->assertEquals($file
->getFileUri(), \Drupal::state()
->get('file_test.results')['download'][0][0]);
// Test that the file transferred correctly.
$this
->assertSame($contents, $this
->getSession()
->getPage()
->getContent(), 'Contents of the file are correct.');
$http_client = $this
->getHttpClient();
// Deny access to all downloads via a -1 header.
file_test_set_return('download', -1);
$response = $http_client
->head($url, [
'http_errors' => FALSE,
]);
$this
->assertSame(403, $response
->getStatusCode(), 'Correctly denied access to a file when file_test sets the header to -1.');
// Try non-existent file.
file_test_reset();
$url = $this->fileUrlGenerator
->generateAbsoluteString('private://' . $this
->randomMachineName());
$response = $http_client
->head($url, [
'http_errors' => FALSE,
]);
$this
->assertSame(404, $response
->getStatusCode(), 'Correctly returned 404 response for a non-existent file.');
// Assert that hook_file_download is not called.
$this
->assertEquals([], \Drupal::state()
->get('file_test.results')['download']);
// Try requesting the private file url without a file specified.
file_test_reset();
$this
->drupalGet('/system/files');
$this
->assertSession()
->statusCodeEquals(404);
// Assert that hook_file_download is not called.
$this
->assertEquals([], \Drupal::state()
->get('file_test.results')['download']);
}