You are here

function FileDownloadTest::testPrivateFileTransfer in SimpleTest 7

Test the private file transfer system.

File

tests/file.test, line 1907
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileDownloadTest
Tests for download/file transfer functions.

Code

function testPrivateFileTransfer() {

  // Set file downloads to private so handler functions get called.
  // Create a file.
  $file = $this
    ->createFile(NULL, NULL, 'private');
  $url = file_create_url($file->uri);

  // Set file_test access header to allow the download.
  file_test_set_return('download', array(
    'x-foo' => 'Bar',
  ));
  $this
    ->drupalHead($url);
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['x-foo'], 'Bar', t('Found header set by file_test module on private download.'));
  $this
    ->assertResponse(200, t('Correctly allowed access to a file when file_test provides headers.'));

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

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