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
Loading
Loading
Loading
Loading
+13 −0
Changes for core/modules/file/tests/src/Functional/DownloadTest.php: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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']);
  }

  /**
+1 −1
Changes for core/modules/system/src/FileDownloadController.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -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]);