You are here

function BackupMigrateFunctionalityTest::testRestoreFromSaved in Backup and Migrate 5

File

tests/BackupMigrateFunctionalityTest.test, line 361

Class

BackupMigrateFunctionalityTest
Unit tests for Backup and Migrate module.

Code

function testRestoreFromSaved() {
  foreach (array(
    'manual',
    'scheduled',
  ) as $type) {
    $directory = _backup_migrate_check_destination_dir($type);
    $file = $directory . "/" . $this
      ->randomName(rand(1, 64), '') . '.sql';
    $test_table = $this
      ->randomName(10, 'testtable_');
    file_put_contents($file, "CREATE TABLE {$test_table} (testid int(10));");
    $this
      ->assertTrue(file_exists($file), t("Reality checking that the test file was created"));

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

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

    // check that the file was imported into the db
    $this
      ->assertTrue(in_array($test_table, _backup_migrate_get_table_names()), t("Checking that the test table is present."));
    $this
      ->drupalGet(url("logout", NULL, NULL, TRUE));
    db_query("DROP TABLE {$test_table};");
  }
  $this
    ->delete_directory(_backup_migrate_get_save_path());
}