You are here

protected function DownloadTest::doPrivateFileTransferTest in Drupal 8

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

Test the private file transfer system.

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

File

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

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(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 = 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
    ->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 = 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']);
}