You are here

function BackupMigrateUnitTest::testFileDownloadHook in Backup and Migrate 5

File

tests/BackupMigrateUnitTest.test, line 424

Class

BackupMigrateUnitTest
Unit tests for Backup and Migrate module.

Code

function testFileDownloadHook() {
  $dir = _backup_migrate_check_destination_dir('manual');
  $valid_extensions = array(
    'sql' => 'text/x-sql',
    'gz' => 'application/x-gzip',
    'bz' => 'application/x-bzip',
    'zip' => 'application/zip',
  );
  foreach ($valid_extensions as $ext => $mime) {
    $name = "test." . $ext;
    $file = $dir . "/" . $name;
    $size = rand(10, 100);
    file_put_contents($file, $this
      ->randomName($size, ''));
    $headers = backup_migrate_file_download($file);
    $this
      ->assertTrue(in_array("Content-Type: {$mime}", $headers), t('Checking the file headers for mime type'));
    $this
      ->assertTrue(in_array("Content-Length: {$size}", $headers), t('Checking the file headers for file size'));
    $this
      ->assertTrue(in_array("Content-Disposition: attachment; filename=\"{$name}\"", $headers), t('Checking the file headers for file size'));
  }

  // non existant file
  $file = file_directory_temp() . '/' . $this
    ->randomName(10, '');
  $info = _backup_migrate_file_info($path);
  $this
    ->assertFalse($info, t('Checking that info is not available on the non-existent file'));
  $headers = backup_migrate_file_download($file);
  $this
    ->assertEqual($headers, NULL, t('Checking that non-existent file is ignored'));

  // file not in backup dir
  $file = _backup_migrate_temp_file('sql');
  touch($file);
  $info = _backup_migrate_file_info($path);
  $this
    ->assertFalse($info, t('Checking that info is not available on the non-existent file'));
  $headers = backup_migrate_file_download($file);
  $this
    ->assertEqual($headers, NULL, t('Checking that file outside of backup directory is ingored'));
}