You are here

function BackupMigrateFunctionalityTest::testDownloadBackup in Backup and Migrate 5

File

tests/BackupMigrateFunctionalityTest.test, line 235

Class

BackupMigrateFunctionalityTest
Unit tests for Backup and Migrate module.

Code

function testDownloadBackup() {
  $types = array(
    'manual' => "admin/content/backup_migrate/files",
    'scheduled' => "admin/content/backup_migrate/files/scheduled",
  );
  foreach ($types as $type => $url) {
    $directory = _backup_migrate_check_destination_dir($type);

    // add a file
    $file = $this
      ->randomName(rand(1, 64), '') . ".sql";
    $path = $directory . "/" . $file;
    $size = rand(10, 100);
    $contents = $this
      ->randomName($size, '');
    file_put_contents($path, $contents);
    $direct_url = $GLOBALS['base_url'] . '/' . $path;
    $private_url = url('system/files/' . $path, NULL, NULL, TRUE);

    // check logged out user
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PRIVATE);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));

    // check no access perms
    $permissions = array();
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PRIVATE);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));
    $permissions = array(
      'access backup files',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url($url, NULL, NULL, TRUE));
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "200",
    ), t("Checking that the user was not access denied"));
    $this
      ->assertHeader("Content-Disposition", 'attachment; filename="' . $file . '"');
    $this
      ->assertText($contents);
    $this
      ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PRIVATE);
    $this
      ->drupalGet(url($direct_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->drupalGet(url($private_url, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "200",
    ), t("Checking that the user was not access denied"));
    $this
      ->assertHeader("Content-Disposition", 'attachment; filename="' . $file . '"');
    $this
      ->assertText($contents);
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));
  }
}