You are here

function BackupMigrateFunctionalityTest::testDeleteBackup in Backup and Migrate 5

File

tests/BackupMigrateFunctionalityTest.test, line 312

Class

BackupMigrateFunctionalityTest
Unit tests for Backup and Migrate module.

Code

function testDeleteBackup() {
  foreach (array(
    'manual',
    'scheduled',
  ) as $type) {
    $directory = _backup_migrate_check_destination_dir($type);
    $file = $directory . "/" . $this
      ->randomName(rand(1, 64), '') . '.sql';
    file_put_contents($file, $this
      ->randomName(100, ''));
    $this
      ->assertTrue(file_exists($file), t("Reality checking that the test file was created"));

    // user without delete access
    // check access only permissions
    $permissions = array(
      'access backup files',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url("admin/content/backup_migrate/delete/" . $file, NULL, NULL, TRUE));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->assertTrue(file_exists($file), t("Checking that the backup file was not deleted"));
    $this
      ->post(url("admin/content/backup_migrate/delete/" . $file, NULL, NULL, TRUE), array(
      'confirm' => 1,
    ));
    $this
      ->assertResponse(array(
      "401",
      "403",
    ), t("Checking that the user was access denied"));
    $this
      ->assertTrue(file_exists($file), t("Checking that the backup file was not deleted"));
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));

    // user with delete access
    // check access only permissions
    $permissions = array(
      'access backup files',
      'delete backup files',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url("admin/content/backup_migrate/delete/" . $file, NULL, NULL, TRUE));
    $this
      ->assertWantedRaw(t('Are you sure you want to delete the backup file at %path?', array(
      '%path' => $file,
    )), t("checking a confirmation message is displayed"));
    $this
      ->assertSubmit(t("Delete"), t('Checking that the delete button is there'));
    $this
      ->assertTrue(file_exists($file), t("Checking that the backup file was not deleted"));
    $this
      ->drupalPostRequest("admin/content/backup_migrate/delete/" . $file, array(), t('Delete'));

    // we should check that the module notifies the user that the file has been deleted.
    // only we can't, because the module doesn't actually do that... oops
    $this
      ->assertFalse(file_exists($file), t("Checking that the backup file was deleted"));
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));
  }
  $this
    ->delete_directory(_backup_migrate_get_save_path());
}