You are here

function FileEntityAccessTestCase::testFileEntityPrivateDownloadAccess in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.test \FileEntityAccessTestCase::testFileEntityPrivateDownloadAccess()

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

File

./file_entity.test, line 1541
Test integration for the file_entity module.

Class

FileEntityAccessTestCase
Tests the file entity access API.

Code

function testFileEntityPrivateDownloadAccess() {
  foreach ($this
    ->getPrivateDownloadAccessCases() as $case) {

    // Create users and login only if non-anonymous.
    $authenticated_user = !is_null($case['permissions']);
    if ($authenticated_user) {
      $account = $this
        ->drupalCreateUser($case['permissions']);
      $this
        ->drupalLogin($account);
    }

    // Create private, permanent files owned by this user only he's an owner.
    if (!empty($case['owner'])) {
      $file = $this
        ->createFileEntity(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
      ->drupalGet($url);
    $this
      ->assertResponse($case['expect'], $case['message'] . $message_file_info);

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