function DownloadTest::doPrivateFileTransferTest

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::doPrivateFileTransferTest()
  2. 8.9.x core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::doPrivateFileTransferTest()
  3. 11.x core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest::doPrivateFileTransferTest()

Tests the private file transfer system.

1 call to DownloadTest::doPrivateFileTransferTest()
DownloadTest::testPrivateFileTransferWithoutPageCache in core/modules/file/tests/src/Functional/DownloadTest.php
Tests the private file transfer system.

File

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

Class

DownloadTest
Tests for download/file transfer functions.

Namespace

Drupal\Tests\file\Functional

Code

protected function doPrivateFileTransferTest() {
  // Set file downloads to private so handler functions get called.
  // Create a file.
  $contents = $this->randomMachineName(8);
  $file = $this->createFile($contents . '.txt', $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();
  // Try non-existent file.
  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.');
  // Assert that hook_file_download is not called.
  $this->assertEquals([], \Drupal::state()->get('file_test.results')['download']);
  // Having tried a non-existent file, try the original file again to ensure
  // it's returned instead of a 404 response.
  // 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);
  // Test that the file transferred correctly.
  $this->assertSame($contents, $this->getSession()
    ->getPage()
    ->getContent(), 'Contents of the file are correct.');
  // 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 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']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.