You are here

function BackupMigrateUnitTest::testCreateBackupDirectory in Backup and Migrate 5

File

tests/BackupMigrateUnitTest.test, line 78

Class

BackupMigrateUnitTest
Unit tests for Backup and Migrate module.

Code

function testCreateBackupDirectory() {
  $directory = _backup_migrate_get_save_path();

  // we try reading and writing with a root based file path (/var/www/.../files) and a relative one (sites/default/files)
  // this is to test for this issue: http://drupal.org/node/193862
  $full_path = realpath(file_directory_path());
  $relative_path = str_replace(realpath('.') . "/", '', $full_path);

  // public file access:
  $this
    ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);

  // relative path
  $this
    ->drupalVariableSet('file_directory_path', $relative_path);
  _backup_migrate_check_destination_dir();
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate"), t('Checking for backup directory'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('manual'), file_directory_path() . "/backup_migrate/manual", t('Checking for manual backup directory create'));
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('manual'), file_directory_path() . "/backup_migrate/manual", t('Checking for manual backup directory preexisting'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual"), t('Checking for manual backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual/test.txt"), t('Checking for manual backup directory test file'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that manual backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('scheduled'), file_directory_path() . "/backup_migrate/scheduled", t('Checking for manual backup directory create'));
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('scheduled'), file_directory_path() . "/backup_migrate/scheduled", t('Checking for manual backup directory preexisting'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for scheduled backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled/test.txt"), t('Checking for scheduled backup directory test file'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that scheduled backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // absolute path
  $this
    ->drupalVariableSet('file_directory_path', $full_path);
  _backup_migrate_check_destination_dir();
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate"), t('Checking for backup directory'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('manual'), file_directory_path() . "/backup_migrate/manual", t('Checking for manual backup directory create'));
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('manual'), file_directory_path() . "/backup_migrate/manual", t('Checking for manual backup directory preexisting'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual"), t('Checking for manual backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual/test.txt"), t('Checking for manual backup directory test file'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that manual backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('scheduled'), file_directory_path() . "/backup_migrate/scheduled", t('Checking for manual backup directory create'));
  $this
    ->assertEqual(_backup_migrate_check_destination_dir('scheduled'), file_directory_path() . "/backup_migrate/scheduled", t('Checking for manual backup directory preexisting'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for scheduled backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled/test.txt"), t('Checking for scheduled backup directory test file'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that scheduled backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // private file access:
  $this
    ->drupalVariableSet('file_directory_path', $relative_path);
  $this
    ->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PRIVATE);
  _backup_migrate_check_destination_dir();
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate"), t('Checking for backup directory'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  _backup_migrate_check_destination_dir('manual');
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual"), t('Checking for manual backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/manual/test.txt"), t('Checking for manual backup directory test file'));
  $check = drupal_http_request($GLOBALS['base_url'] . '/' . file_directory_path() . "/backup_migrate/manual/test.txt");
  $this
    ->assertEqual($check->code, 403, t('Checking that test file is forbidden for access'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that manual backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  _backup_migrate_check_destination_dir('scheduled');
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for scheduled backup directory'));
  $this
    ->assertTrue(file_exists(file_directory_path() . "/backup_migrate/scheduled/test.txt"), t('Checking for scheduled backup directory test file'));
  $check = drupal_http_request($GLOBALS['base_url'] . '/' . file_directory_path() . "/backup_migrate/scheduled/test.txt");
  $this
    ->assertEqual($check->code, 403, t('Checking that test file is forbidden for access'));
  $this
    ->assertTrue(is_writable(file_directory_path() . "/backup_migrate"), t('Checking that scheduled backup directory is writable'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // we need to test failure to create the directory
  // if we create a file where the directory should be that should make the operation fail
  $error_message_main = t("Unable to create or write to the save directory '%directory'. Please check the file permissions on your files directory.", array(
    '%directory' => file_directory_path() . "/backup_migrate",
  ));
  $error_message_manual = t("Unable to create or write to the save directory '%directory'. Please check the file permissions on your files directory.", array(
    '%directory' => file_directory_path() . "/backup_migrate/manual",
  ));
  $error_message_scheduled = t("Unable to create or write to the save directory '%directory'. Please check the file permissions on your files directory.", array(
    '%directory' => file_directory_path() . "/backup_migrate/scheduled",
  ));

  // Add a file with the same name so that the directory create fails
  touch(file_directory_path() . "/backup_migrate");
  $this
    ->assertFalse(_backup_migrate_check_destination_dir(), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate"), t('Checking for lack of backup directory'));
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('manual'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('scheduled'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for lack of backup directory'));
  unlink(file_directory_path() . "/backup_migrate");
  mkdir(file_directory_path() . "/backup_migrate");
  touch(file_directory_path() . "/backup_migrate/manual");
  touch(file_directory_path() . "/backup_migrate/scheduled");
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('manual'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_manual, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_manual);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate/manual"), t('Checking for lack of backup directory'));
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('scheduled'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_scheduled, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_scheduled);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for lack of backup directory'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // check unwritable directory
  mkdir(file_directory_path() . "/backup_migrate");
  chmod($directory, 0444);
  $this
    ->assertFalse(_backup_migrate_check_destination_dir(), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('manual'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate/manual"), t('Checking for lack of backup directory'));
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('scheduled'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_main, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_main);
  $this
    ->assertFalse(is_dir(file_directory_path() . "/backup_migrate/scheduled"), t('Checking for lack of backup directory'));
  chmod(file_directory_path() . "/backup_migrate", 0777);
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
  mkdir(file_directory_path() . "/backup_migrate");
  mkdir(file_directory_path() . "/backup_migrate/manual");
  mkdir(file_directory_path() . "/backup_migrate/scheduled");
  chmod(file_directory_path() . "/backup_migrate/manual", 0444);
  chmod(file_directory_path() . "/backup_migrate/scheduled", 0444);
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('manual'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_manual, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_manual);
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('scheduled'), t('Checking for a negative response from the create function'));
  $this
    ->assertDrupalMessage('error', $error_message_scheduled, t('Checking that the user is warned about the ability to write to the backup directory'));
  $this
    ->removeDrupalMessage('error', $error_message_scheduled);
  chmod(file_directory_path() . "/backup_migrate/manual", 0644);
  chmod(file_directory_path() . "/backup_migrate/scheduled", 0644);
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // need to check that if the directory is publicly accessible for some reason the check fails
  mkdir(file_directory_path() . "/backup_migrate");
  mkdir(file_directory_path() . "/backup_migrate/manual");
  mkdir(file_directory_path() . "/backup_migrate/scheduled");
  $directory = file_directory_path() . "/backup_migrate";

  // override the htaccess with an insecure version
  $htaccess_lines = "order allow,deny\nallow from all\n";
  if (($fp = @fopen($directory . '/.htaccess', 'w')) && @fputs($fp, $htaccess_lines)) {
    fclose($fp);
    chmod($directory . '/.htaccess', 0444);
  }
  else {
    $this
      ->assertFalse(true, t('Need to be able to write to the htaccess file to complete this test'));
  }
  $this
    ->assertFalse(_backup_migrate_check_destination_dir('manual'), t('Checking for failure of check on insecure directory'));
  chmod($directory . '/.htaccess', 0644);
  $this
    ->assertTrue(_backup_migrate_check_destination_dir('manual'), t('Checking that the htaccess file is overwritten if possible.'));
  $htaccess_lines = "order allow,deny\ndeny from all\n";
  $this
    ->assertTrue(strpos(file_get_contents($directory . '/.htaccess'), $htaccess_lines) !== FALSE, t('Checking that the htaccess file contains the right value.'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
}