You are here

function BackupMigrateUnitTest::testRemoveExpiredBackups in Backup and Migrate 5

File

tests/BackupMigrateUnitTest.test, line 294

Class

BackupMigrateUnitTest
Unit tests for Backup and Migrate module.

Code

function testRemoveExpiredBackups() {
  $dir = file_directory_path() . "/backup_migrate/scheduled/";
  _backup_migrate_check_destination_dir('scheduled');
  for ($i = 0; $i < 10; $i++) {
    touch($dir . $this
      ->randomName(5, 'somefile_') . '.sql');
  }

  // reality check
  $this
    ->assertEqual($this
    ->countFiles($dir), 10, t('Reality checking the initial number of files in the scheduled dir'));

  // check infinity setting does not delete any
  $this
    ->drupalVariableSet("backup_migrate_schedule_backup_keep", 0);
  _backup_migrate_remove_expired_backups();
  $this
    ->assertEqual($this
    ->countFiles($dir), 10, t('Checking the initial number of files in the scheduled dir'));
  $this
    ->drupalVariableSet("backup_migrate_schedule_backup_keep", 5);
  _backup_migrate_remove_expired_backups();
  $this
    ->assertEqual($this
    ->countFiles($dir), 5, t('Checking the number of files in the scheduled dir'));
  $this
    ->drupalVariableSet("backup_migrate_schedule_backup_keep", 10);
  _backup_migrate_remove_expired_backups();
  $this
    ->assertEqual($this
    ->countFiles($dir), 5, t('Checking the number of files in the scheduled dir'));
  $this
    ->drupalVariableSet("backup_migrate_schedule_backup_keep", 1);
  _backup_migrate_remove_expired_backups();
  $this
    ->assertEqual($this
    ->countFiles($dir), 1, t('Checking the number of files in the scheduled dir'));
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");

  // make sure the files being removed are the oldest ones
  _backup_migrate_check_destination_dir('scheduled');
  for ($i = 0; $i < 5; $i++) {
    touch($dir . $this
      ->randomName(5, 'somefile_') . '.sql', $i * 86400);
  }
  for ($i = 0; $i < 5; $i++) {
    $this
      ->drupalVariableSet("backup_migrate_schedule_backup_keep", 5 - $i);
    _backup_migrate_remove_expired_backups();
    $time = $this
      ->getOldestFiletime($dir);
    $this
      ->assertEqual($time, $i * 86400, t('Checking the oldest file in the dir'));
  }
  $this
    ->delete_directory(file_directory_path() . "/backup_migrate");
}