You are here

function BackupMigrateFunctionalityTest::testListSavedBackups in Backup and Migrate 5

File

tests/BackupMigrateFunctionalityTest.test, line 126

Class

BackupMigrateFunctionalityTest
Unit tests for Backup and Migrate module.

Code

function testListSavedBackups() {
  $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 some files
    $valid_extensions = array(
      ".sql",
      ".gz",
      ".bz",
      ".zip",
    );
    $invalid_extensions = array(
      ".txt",
      ".doc",
      ".svn",
      ".abc",
      ".xyz",
      "",
    );
    $files = array();
    for ($i = 0; $i < 4; $i++) {
      $valid = $i % 2 == 0;
      if ($valid) {
        $file = $this
          ->randomName(rand(1, 64), '') . $valid_extensions[array_rand($valid_extensions)];
      }
      else {
        $file = $this
          ->randomName(rand(1, 64), '') . $invalid_extensions[array_rand($invalid_extensions)];
      }
      $path = $directory . "/" . $file;
      $size = rand(10, 100);
      file_put_contents($path, $this
        ->randomName($size, ''));
      $files[] = array(
        'name' => $file,
        'path' => $directory . "/" . $file,
        'size' => format_size($size),
        'time' => date("m/d/Y h:i a", filectime($path)),
        'valid' => $valid,
      );
    }

    // check access only permissions
    $permissions = array(
      'access backup files',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url($url, NULL, NULL, TRUE));
    foreach ($files as $file) {
      if (!$file['valid']) {
        $this
          ->assertNoText($file['name'], t("checking that an invalid file is not in the file list"));
      }
      else {
        $this
          ->assertText($file['name'], t("checking that a valid file name (!text) is in the file list", array(
          "!text" => $file['name'],
        )));
        $this
          ->assertText($file['size'], t("checking that a valid file size (!text) is in the file list", array(
          "!text" => $file['size'],
        )));
        $this
          ->assertText($file['time'], t("checking that a valid file time (!text) is in the file list", array(
          "!text" => $file['time'],
        )));
        $this
          ->assertWantedRaw(l("download", "system/files/" . $file['path']), t("checking that the download link is in the file list"));
        $this
          ->assertNoUnwantedRaw(l("restore", "admin/content/backup_migrate/restorefile/" . $file['path']), t("checking that the restore link is not in the file list"));
        $this
          ->assertNoUnwantedRaw(l("delete", "admin/content/backup_migrate/delete/" . $file['path']), t("checking that the delete link is not in the file list"));
      }
    }
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));

    // check restore permissions
    $permissions = array(
      'access backup files',
      'restore from backup',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url($url, NULL, NULL, TRUE));
    foreach ($files as $file) {
      if (!$file['valid']) {
        $this
          ->assertNoText($file['name'], t("checking that an invalid file is not in the file list"));
      }
      else {
        $this
          ->assertText($file['name'], t("checking that a valid file name (!text) is in the file list", array(
          "!text" => $file['name'],
        )));
        $this
          ->assertText($file['size'], t("checking that a valid file size (!text) is in the file list", array(
          "!text" => $file['size'],
        )));
        $this
          ->assertText($file['time'], t("checking that a valid file time (!text) is in the file list", array(
          "!text" => $file['time'],
        )));
        $this
          ->assertWantedRaw(l("download", "system/files/" . $file['path']), t("checking that the download link is in the file list"));
        $this
          ->assertWantedRaw(l("restore", "admin/content/backup_migrate/restorefile/" . $file['path']), t("checking that the restore link is not in the file list"));
        $this
          ->assertNoUnwantedRaw(l("delete", "admin/content/backup_migrate/delete/" . $file['path']), t("checking that the delete link is not in the file list"));
      }
    }
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));

    // check delete permissions
    $permissions = array(
      'access backup files',
      'delete backup files',
    );
    $user = $this
      ->drupalCreateUserRolePerm($permissions);
    $this
      ->drupalLoginUser($user);
    $this
      ->drupalGet(url($url, NULL, NULL, TRUE));
    foreach ($files as $file) {
      if (!$file['valid']) {
        $this
          ->assertNoText($file['name'], t("checking that an invalid file is not in the file list"));
      }
      else {
        $this
          ->assertText($file['name'], t("checking that a valid file name (!text) is in the file list", array(
          "!text" => $file['name'],
        )));
        $this
          ->assertText($file['size'], t("checking that a valid file size (!text) is in the file list", array(
          "!text" => $file['size'],
        )));
        $this
          ->assertText($file['time'], t("checking that a valid file time (!text) is in the file list", array(
          "!text" => $file['time'],
        )));
        $this
          ->assertWantedRaw(l("download", "system/files/" . $file['path']), t("checking that the download link is in the file list"));
        $this
          ->assertNoUnwantedRaw(l("restore", "admin/content/backup_migrate/restorefile/" . $file['path']), t("checking that the restore link is not in the file list"));
        $this
          ->assertWantedRaw(l("delete", "admin/content/backup_migrate/delete/" . $file['path']), t("checking that the delete link is not in the file list"));
      }
    }
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));
  }
  $this
    ->delete_directory(_backup_migrate_get_save_path());
}