You are here

protected function DownloadTest::doPrivateFileTransferTest in Zircon Profile 8

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

Test the private file transfer system.

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

File

core/modules/file/src/Tests/DownloadTest.php, line 55
Contains \Drupal\file\Tests\DownloadTest.

Class

DownloadTest
Tests for download/file transfer functions.

Namespace

Drupal\file\Tests

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_set_return('download', array(
    'x-foo' => 'Bar',
  ));
  $this
    ->drupalGet($url);
  $this
    ->assertEqual($this
    ->drupalGetHeader('x-foo'), 'Bar', 'Found header set by file_test module on private download.');
  $this
    ->assertFalse($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.');

  // Test that the file transferred correctly.
  $this
    ->assertEqual($contents, $this->content, 'Contents of the file are correct.');

  // Deny access to all downloads via a -1 header.
  file_test_set_return('download', -1);
  $this
    ->drupalHead($url);
  $this
    ->assertResponse(403, 'Correctly denied access to a file when file_test sets the header to -1.');

  // Try non-existent file.
  $url = file_create_url('private://' . $this
    ->randomMachineName());
  $this
    ->drupalHead($url);
  $this
    ->assertResponse(404, 'Correctly returned 404 response for a non-existent file.');
}