Same filename in this branch
- 10 core/modules/file/tests/src/Functional/DownloadTest.php
- 10 core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
Same filename and directory in other branches
- 8.9.x core/modules/file/tests/src/Functional/DownloadTest.php
- 9 core/modules/file/tests/src/Functional/DownloadTest.php
Namespace
Drupal\Tests\file\Functional
File
core/modules/file/tests/src/Functional/DownloadTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\file\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\File\FileSystemInterface;
class DownloadTest extends FileManagedTestBase {
protected $defaultTheme = 'stark';
protected $fileUrlGenerator;
protected function setUp() : void {
parent::setUp();
if (Database::getConnection()
->driver() === 'sqlite') {
$this
->markTestSkipped('Test frequently causes a locked database on SQLite');
}
$this->fileUrlGenerator = $this->container
->get('file_url_generator');
file_test_reset();
}
public function testPublicFileTransfer() {
$file = $this
->createFile();
$url = $this->fileUrlGenerator
->generateAbsoluteString($file
->getFileUri());
$filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')
->getViaScheme('public')
->getDirectoryPath() . '/' . rawurlencode($file
->getFilename());
$this
->assertEquals($filename, $url, 'Correctly generated a URL for a created file.');
$http_client = $this
->getHttpClient();
$response = $http_client
->head($url);
$this
->assertEquals(200, $response
->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the created file.');
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = $this->fileUrlGenerator
->generateAbsoluteString($filepath);
$this
->assertEquals($GLOBALS['base_url'] . '/' . $filepath, $url, 'Correctly generated a URL for a shipped file.');
$response = $http_client
->head($url);
$this
->assertEquals(200, $response
->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the shipped file.');
}
public function testPrivateFileTransferWithoutPageCache() {
$this
->doPrivateFileTransferTest();
}
protected function doPrivateFileTransferTest() {
$contents = $this
->randomMachineName(8);
$file = $this
->createFile($contents . '.txt', $contents, 'private');
$file
->setPermanent();
$file
->save();
$url = $this->fileUrlGenerator
->generateAbsoluteString($file
->getFileUri());
file_test_reset();
file_test_set_return('download', [
'x-foo' => 'Bar',
]);
$this
->drupalGet($url);
$this
->assertSession()
->responseHeaderEquals('x-foo', 'Bar');
$this
->assertSession()
->responseHeaderDoesNotExist('x-drupal-cache');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEquals($file
->getFileUri(), \Drupal::state()
->get('file_test.results')['download'][0][0]);
$this
->assertSame($contents, $this
->getSession()
->getPage()
->getContent(), 'Contents of the file are correct.');
$http_client = $this
->getHttpClient();
file_test_reset();
$not_found_url = $this->fileUrlGenerator
->generateAbsoluteString('private://' . $this
->randomMachineName() . '.txt');
$response = $http_client
->head($not_found_url, [
'http_errors' => FALSE,
]);
$this
->assertSame(404, $response
->getStatusCode(), 'Correctly returned 404 response for a non-existent file.');
$this
->assertEquals([], \Drupal::state()
->get('file_test.results')['download']);
file_test_reset();
file_test_set_return('download', [
'x-foo' => 'Bar',
]);
$this
->drupalGet($url);
$this
->assertSession()
->responseHeaderEquals('x-foo', 'Bar');
$this
->assertSession()
->responseHeaderDoesNotExist('x-drupal-cache');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSame($contents, $this
->getSession()
->getPage()
->getContent(), 'Contents of the file are correct.');
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.');
file_test_reset();
$this
->drupalGet('/system/files');
$this
->assertSession()
->statusCodeEquals(404);
$this
->assertEquals([], \Drupal::state()
->get('file_test.results')['download']);
}
public function testFileCreateUrl() {
$basename = " -._~!\$'\"()*@[]?&+%#,;=:\n\0" . "%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';
$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));
}
private function checkUrl($scheme, $directory, $filename, $expected_url) {
$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') {
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();
}
}
Classes