Skip to content
Snippets Groups Projects
Unverified Commit 24e1692a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3016814 by jhedstrom, peximo, alexpott, jungle, scuba_fly, mr.baileys,...

Issue #3016814 by jhedstrom, peximo, alexpott, jungle, scuba_fly, mr.baileys, FrancescoQ, cgoffin, Petr Illek, fabio84: Don't trigger hook_file_download when no file is requested
parent 81c6dc62
Branches
Tags
8 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards,!16Draft: Resolve #2081585 "History storage",!13Resolve #2903456
......@@ -72,11 +72,14 @@ protected function doPrivateFileTransferTest() {
$url = file_create_url($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);
$this->assertEqual($this->drupalGetHeader('x-foo'), 'Bar', 'Found header set by file_test module on private download.');
$this->assertNull($this->drupalGetHeader('x-drupal-cache'), 'Page cache is disabled on private file download.');
$this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
// 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.');
......@@ -88,9 +91,19 @@ protected function doPrivateFileTransferTest() {
$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 = file_create_url('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']);
}
/**
......
......@@ -71,7 +71,7 @@ public function download(Request $request, $scheme = 'private') {
// Merge remaining path arguments into relative file path.
$uri = $scheme . '://' . $target;
if ($this->streamWrapperManager->isValidScheme($scheme) && file_exists($uri)) {
if ($this->streamWrapperManager->isValidScheme($scheme) && is_file($uri)) {
// Let other modules provide headers and controls access to the file.
$headers = $this->moduleHandler()->invokeAll('file_download', [$uri]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment