1.20.x file.test FileAccessTestCase::testFilePrivateDownloadAccess()

Test to see if we have access to download private files when granted the permissions.

File

modules/file/tests/file.test, line 3205
Tests for file.module.

Class

FileAccessTestCase
Tests the file entity access API.

Code

function testFilePrivateDownloadAccess() {
  foreach ($this->getPrivateDownloadAccessCases() as $case) {
    // Create users and login only if non-anonymous.
    $authenticated_user = !is_null($case['permissions']);
    if ($authenticated_user) {
      $account = $this->backdropCreateUser($case['permissions']);
      $this->backdropLogin($account);
    }

    // Create private, permanent files owned by this user only he's an owner.
    if (!empty($case['owner'])) {
      $file = $this->createFile(array('type' => 'document', 'uid' => $account->uid, 'scheme' => 'private'));

      // Check if the physical file is there.
      $arguments = array('%name' => $file->filename, '%username' => $account->name, '%uri' => $file->uri);
      $this->assertTrue(is_file($file->uri), format_string('File %name owned by %username successfully created at %uri.', $arguments));
      $url = file_create_url($file->uri);
      $message_file_info = ' ' . format_string('File %uri was checked.', array('%uri' => $file->uri));
    }

    // Try to download the file.
    $this->backdropGet($url);
    $this->assertResponse($case['expect'], $case['message'] . $message_file_info);

    // Logout authenticated users.
    if ($authenticated_user) {
      $this->backdropLogout();
    }
  }
}