View source
<?php
class BackupMigrateUnitTest extends DrupalTestCase {
function get_info() {
return array(
'name' => t('Backup and Migrate Unit Tests'),
'desc' => t('Executes the unit test suite for misc functions in backup and migrate.'),
'group' => t('Backup and Migrate module'),
);
}
var $directory_backup;
function setUp() {
parent::setUp();
$directory = _backup_migrate_get_save_path();
if (is_dir($directory)) {
$this->directory_backup = $directory . $this
->randomName(5, '_');
rename($directory, $this->directory_backup);
}
}
function tearDown() {
if ($this->directory_backup) {
$directory = _backup_migrate_get_save_path();
$this
->delete_directory(_backup_migrate_get_save_path());
rename($this->directory_backup, $directory);
}
parent::tearDown();
}
function testGetSavePath() {
$this
->assertEqual(_backup_migrate_get_save_path(), file_directory_path() . "/backup_migrate");
$this
->assertEqual(_backup_migrate_get_save_path('manual'), file_directory_path() . "/backup_migrate/manual");
$this
->assertEqual(_backup_migrate_get_save_path('scheduled'), file_directory_path() . "/backup_migrate/scheduled");
}
function testFileReadableRemotely() {
$filename = $this
->randomName(5, 'readtest_') . ".txt";
$filepath = file_directory_path() . '/' . $filename;
$contents = $this
->randomName(5, 'contents_');
$this
->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);
$this
->assertFalse(_backup_migrate_test_file_readable_remotely($filename, $contents), t('Checking false on non existant file read'));
if (($fp = @fopen($filepath, 'w')) && @fputs($fp, $contents)) {
fclose($fp);
}
else {
$this
->assertTrue(false, t('Need to be able to write to a file in files to complete this test'));
}
$this
->assertTrue(_backup_migrate_test_file_readable_remotely($filename, $contents), t('Checking readable file'));
unlink(realpath($filepath));
}
function testCreateBackupDirectory() {
$directory = _backup_migrate_get_save_path();
$full_path = realpath(file_directory_path());
$relative_path = str_replace(realpath('.') . "/", '', $full_path);
$this
->drupalVariableSet('file_downloads', FILE_DOWNLOADS_PUBLIC);
$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");
$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");
$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");
$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",
));
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");
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");
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";
$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");
}
function testCleanFileName() {
$safe_pattern = "/[A-Za-z0-9\\-\\_\\.]+/";
$unsafe_name = "@#/\$he*ll&o, world!";
$safe_name = _backup_migrate_clean_filename($unsafe_name);
$this
->assertTrue(preg_match($safe_pattern, $safe_name), t('Testing that filename does not contain unsafe characters'));
$this
->assertEqual($safe_name, "helloworld", t('Testing that filename is consistent with input'));
$unsafe_name = str_repeat("abc", 50);
$safe_name = _backup_migrate_clean_filename($unsafe_name);
$this
->assertTrue(strlen($safe_name) <= 50, t('Testing that filename is not too long'));
$safe_name = _backup_migrate_clean_filename("");
$this
->assertTrue(strlen($safe_name) > 1, t('Testing that filename is not non existant'));
}
function testDefaultFileName() {
$this
->drupalModuleEnable('token');
$name = _backup_migrate_default_file_name();
$this
->assertEqual($name, '[site-name]', t('Checking default filename is site-name token'));
$this
->drupalModuleDisable('token');
$sitename = $this
->randomName(5, 'site_');
$this
->drupalVariableSet('site_name', $sitename);
$name = _backup_migrate_default_file_name();
$this
->assertEqual($name, $sitename, t('Checking default filename is the site name'));
}
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');
}
$this
->assertEqual($this
->countFiles($dir), 10, t('Reality checking the initial number of files in the scheduled dir'));
$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");
_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");
}
function testTempFileDelete() {
$dir = file_directory_path() . "/backup_migrate/manual/";
_backup_migrate_check_destination_dir('manual');
for ($i = 0; $i < 10; $i++) {
$file = $dir . $this
->randomName(5, 'somefile_');
$files[] = $file;
touch($file);
}
$this
->assertEqual($this
->countFiles($dir), 10, t('Checking the number of files created is 10'));
_backup_migrate_temp_files_delete($files);
$this
->assertEqual($this
->countFiles($dir), 0, t('Checking the number of files created is 0'));
$this
->delete_directory(file_directory_path() . "/backup_migrate");
}
function testTempFile() {
$dir = file_directory_path() . "/backup_migrate/manual/";
_backup_migrate_check_destination_dir('manual');
for ($i = 0; $i < 5; $i++) {
$ext = $this
->randomName($i, '');
$files[$i] = _backup_migrate_temp_file($ext);
touch($files[$i]);
$this
->assertTrue(file_exists($files[$i]), t('Checking that the temporary file is available for writing'));
$this
->assertTrue(file_check_location($files[$i], file_directory_temp()), t('Check that the file is in the temp directory'));
if ($ext) {
$this
->assertEqual(substr($files[$i], -strlen($ext)), $ext, t('Check that the temp file has the right extension'));
}
else {
$this
->assertTrue(strpos($files[$i], '.') === false, $ext, t('Check that the temp file has no extension'));
}
}
_backup_migrate_temp_file(NULL, $true);
for ($i = 0; $i < 5; $i++) {
$this
->assertFalse(file_exists($file[$i]), t('Check that the temp file has been deleted'));
}
}
function testGetFileInfo() {
$contents = $this
->randomName($size, '');
$valid_extensions = array(
'sql' => 'text/x-sql',
'gz' => 'application/x-gzip',
'bz' => 'application/x-bzip',
'zip' => 'application/zip',
);
$invalid_extensions = array(
'txt',
'exe',
'foo',
'',
);
foreach ($valid_extensions as $ext => $mime) {
$file = _backup_migrate_temp_file($ext);
$size = rand(10, 100);
file_put_contents($file, $this
->randomName($size, ''));
$info = _backup_migrate_file_info($file);
$this
->assertTrue(is_array($info), t('Checking that info is available on the given file (!ext)', array(
'!ext' => $ext,
)));
$this
->assertEqual($info['filesize'], $size, t('Checking that the filesize is correct'));
$this
->assertEqual($info['extension'], ".{$ext}", t('Checking that the file extension is correct'));
$this
->assertEqual($info['filename'], basename($file), t('Checking that the file basename'));
$this
->assertEqual($info['filemtime'], filemtime($file), t('Checking that modified time is correct'));
$this
->assertEqual($info['filectime'], filectime($file), t('Checking that created time is correct'));
$this
->assertEqual($info['filepath'], $file, t('Checking that file path is correct'));
$this
->assertEqual($info['filemime'], $mime, t('Checking that file mime is correct'));
}
foreach ($invalid_extensions as $ext) {
$file = _backup_migrate_temp_file($ext);
$size = rand(10, 100);
file_put_contents($file, $this
->randomName($size, ''));
$info = _backup_migrate_file_info($file);
$this
->assertFalse($info, t('Checking that info is not available on the given 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'));
_backup_migrate_temp_file(NULL, $true);
}
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'));
}
$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 = _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'));
}
function testPathinSaveDir() {
$directory = _backup_migrate_get_save_path();
$this
->delete_directory(file_directory_path() . "/backup_migrate");
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "/test"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "/a/b/c"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "/manual", "manual"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "manual/a/b/c", "manual"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "/scheduled", "scheduled"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir($directory . "scheduled/a/b/c", "scheduled"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/" . $directory), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/", "manual"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/", "scheduled"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("manual", "manual"), t('Checking that non-existant directory returns false'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("scheduled", "scheduled"), t('Checking that non-existant directory returns false'));
_backup_migrate_check_destination_dir('manual');
_backup_migrate_check_destination_dir('scheduled');
$this
->assertTrue(_backup_migrate_path_is_in_save_dir($directory), t('Checking that valid path is confirmed'));
$this
->assertTrue(_backup_migrate_path_is_in_save_dir($directory . "/test"), t('Checking that valid path is confirmed'));
$this
->assertTrue(_backup_migrate_path_is_in_save_dir($directory . "/manual", "manual"), t('Checking that valid path is confirmed'));
$this
->assertTrue(_backup_migrate_path_is_in_save_dir($directory . "/scheduled", "scheduled"), t('Checking that valid path is confirmed'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/" . $directory), t('Checking that invalid path is rejected'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test"), t('Checking that invalid path is rejected'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/", "manual"), t('Checking that invalid path is rejected'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("test/", "scheduled"), t('Checking that invalid path is rejected'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("manual", "manual"), t('Checking that invalid path is rejected'));
$this
->assertFalse(_backup_migrate_path_is_in_save_dir("scheduled", "scheduled"), t('Checking that invalid path is rejected'));
$this
->delete_directory(file_directory_path() . "/backup_migrate");
}
function testGetFileTypes() {
$types = _backup_migrate_filetypes();
$this
->assertTrue(is_array($types), t('Checking that the function returned an array'));
$this
->assertTrue(count($types) > 1, t('Checking that the function returned an array'));
foreach ($types as $key => $type) {
$this
->assertTrue(preg_match('/.[a-z0-9\\.]+/', $type['extension']), t('Checking that the returned type has a valid extension'));
$this
->assertTrue(preg_match('/[a-z0-9\\-\\.]+\\/[a-z0-9\\-\\.]+/', $type['filemime']), t('Checking that the returned type has a valid mimetype'));
}
}
function testSaveToDisk() {
$directory = file_directory_path() . "/backup_migrate";
$tempfile = file_directory_temp() . "/" . $this
->randomName(10, '');
$contents = $this
->randomName(128, '');
$filename = $this
->randomName(10, '');
$this
->delete_directory($directory);
file_put_contents($tempfile, $contents);
_backup_migrate_save_to_disk($tempfile, $filename, 'manual');
$this
->assertTrue(file_exists($directory . '/manual/' . $filename), t('Checking that the file was saved to the server'));
$this
->assertEqual($contents, file_get_contents($directory . '/manual/' . $filename), t('Checking that the file contains the right contents'));
$this
->assertFalse(file_exists($tempfile), t('Checking that the temp file is no longer there'));
$this
->delete_directory($directory);
file_put_contents($tempfile, $contents);
_backup_migrate_save_to_disk($tempfile, $filename, 'scheduled');
$this
->assertTrue(file_exists($directory . '/scheduled/' . $filename), t('Checking that the file was saved to the server'));
$this
->assertEqual($contents, file_get_contents($directory . '/scheduled/' . $filename), t('Checking that the file contains the right contents'));
$this
->assertFalse(file_exists($tempfile), t('Checking that the temp file is no longer there'));
$this
->delete_directory($directory);
}
function testGZipEncode() {
$tempfile = file_directory_temp() . "/" . $this
->randomName(10, 'gztest') . '.gz';
$knownfile = drupal_get_path('module', 'backup_migrate') . "/tests/test.txt";
$knownzip = drupal_get_path('module', 'backup_migrate') . "/tests/test.gz";
_backup_migrate_gzip_encode($knownfile, $tempfile);
$temp_contents = file_get_contents($tempfile);
$known_contents = file_get_contents($knownfile);
$knownzip_contents = file_get_contents($knownzip);
$zd = gzopen($tempfile, "r");
$temp_decoded = gzread($zd, 1000);
gzclose($zd);
$this
->assertEqual(md5($temp_contents), md5($knownzip_contents));
$this
->assertEqual($temp_decoded, $known_contents);
}
function testBZipEncode() {
$tempfile = file_directory_temp() . "/" . $this
->randomName(10, 'bztest');
$knownfile = drupal_get_path('module', 'backup_migrate') . "/tests/test.txt";
_backup_migrate_bzip_encode($knownfile, $tempfile);
$temp_contents = file_get_contents($tempfile);
$known_contents = file_get_contents($knownfile);
$this
->assertEqual(md5($temp_contents), md5(bzcompress($known_contents, 9)));
$this
->assertEqual(bzdecompress($temp_contents), $known_contents);
unlink($tempfile);
}
function testZipEncode() {
$tempfile = file_directory_temp() . "/" . $this
->randomName(10, 'ziptest') . ".zip";
$knownfile = drupal_get_path('module', 'backup_migrate') . "/tests/test.txt";
_backup_migrate_zip_encode($knownfile, $tempfile, 'test.txt');
$zip = zip_open($tempfile);
$entry = zip_read($zip);
zip_entry_open($zip, $entry);
$temp_contents = zip_entry_read($entry);
$known_contents = file_get_contents($knownfile);
$this
->assertEqual($temp_contents, $known_contents);
unlink($tempfile);
}
function testDefaultTables() {
$safe_pattern = "/[a-z\\_]+/";
$tables = _backup_migrate_default_exclude_tables();
$this
->assertTrue(is_array($tables), t('Testing that default exclude tables is an array'));
foreach ($tables as $table) {
$this
->assertTrue(is_string($table), t('Testing that table name is a string'));
$this
->assertTrue(preg_match($safe_pattern, $table), t('Testing that table name: %name is valid', array(
'%name' => $name,
)));
}
$tables = _backup_migrate_default_structure_only_tables();
$this
->assertTrue(is_array($tables), t('Testing that default structure only tables is an array'));
foreach ($tables as $table) {
$this
->assertTrue(is_string($table), t('Testing that table name is a string'));
$this
->assertTrue(preg_match($safe_pattern, $table), t('Testing that table name: %name is valid', array(
'%name' => $name,
)));
}
}
function testRestoreFile() {
}
function testGetTableNames() {
$safe_pattern = "/[a-z\\_]+/";
$test_table = $this
->randomName(10, 'testtable_');
db_query("CREATE TABLE {$test_table} (testid int(10));");
$tables = _backup_migrate_get_table_names();
$this
->assertTrue(is_array($tables), t('Testing that default exclude tables is an array'));
$this
->assertTrue(count($tables) > 0, t('Testing at least one table is present'));
foreach ($tables as $table) {
$this
->assertTrue(is_string($table), t('Testing that table name is a string'));
$this
->assertTrue(preg_match($safe_pattern, $table), t('Testing that table name: %name is valid', array(
'%name' => $name,
)));
}
$this
->assertTrue(in_array($test_table, $tables), t('Testing that the test table is present'));
db_query("DROP TABLE {$test_table};");
}
function testGetTables() {
$test_table = $this
->randomName(10, 'testtable_');
db_query("CREATE TABLE {$test_table} (testid int(10));");
$table_names = _backup_migrate_get_table_names();
$tables = _backup_migrate_get_tables();
foreach ($table_names as $table) {
$this
->assertTrue(isset($tables[$table]), t('Testing that table: %name is present', array(
'%name' => $name,
)));
$this
->assertTrue(isset($tables[$table]['Name']), t('Testing that table name is present', array(
'%name' => $name,
)));
$this
->assertTrue(array_key_exists('Auto_increment', $tables[$table]), t('Testing that table auto increment is present', array(
'%name' => $name,
)));
}
$this
->assertTrue(isset($tables[$test_table]), t('Testing that the test table is present'));
db_query("DROP TABLE {$test_table};");
}
function testGetSQLFooter() {
$footer = _backup_migrate_get_sql_file_footer();
$this
->assertTrue(strpos($footer, "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;") !== false, t('Check for one of the mode reset statements'));
}
function testGetSQLHeader() {
$footer = _backup_migrate_get_sql_file_header();
$this
->assertTrue(strpos($footer, "SET NAMES utf8;") !== false, t('Check for the set names'));
$this
->assertTrue(strpos($footer, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;") !== false, t('Check for one of the mode reset statements'));
}
function testGetTableStuctureSQL() {
}
function testDumpSQLToFileHandle() {
}
function testGetDumpSQL() {
}
function testSendFileToDownload() {
}
function testDumpTables() {
}
function testBackupWithDefaults() {
}
function testDeleteCallback() {
}
function testDeleteConfirm() {
}
function testScheduleForm() {
}
function testBackupForm() {
}
function testListFiles() {
}
function testHookPerm() {
}
function testHookMenu() {
}
function testHookCron() {
}
function testHookSimpleTest() {
}
function delete_directory($dirname) {
if (is_dir($dirname) && ($dir_handle = opendir($dirname))) {
while ($file = readdir($dir_handle)) {
if ($file != '.' && $file != '..') {
if (!is_dir($dirname . '/' . $file)) {
unlink($dirname . '/' . $file);
}
else {
$this
->delete_directory($dirname . '/' . $file);
}
}
}
closedir($dir_handle);
rmdir($dirname);
}
}
function assertDrupalMessage($type, $drupal_message, $message) {
foreach (@$_SESSION['messages'][$type] as $session_message) {
if ($session_message == $drupal_message) {
$this
->assertTrue(true, $message);
return;
}
}
$this
->assertTrue(false, $message);
}
function removeDrupalMessage($type, $drupal_message) {
foreach (@$_SESSION['messages'][$type] as $key => $session_message) {
if ($session_message == $drupal_message) {
unset($_SESSION['messages'][$type][$key]);
}
}
}
function countFiles($dir) {
$out = 0;
if ($res = opendir($dir)) {
while ($file = readdir($res)) {
if ($file != '.' && $file != '..' && $file != 'test.txt') {
$out++;
}
}
}
return $out;
}
function getOldestFiletime($dir) {
$out = 0xffffffff;
if ($res = opendir($dir)) {
while ($file = readdir($res)) {
if ($file != '.' && $file != '..' && $file != 'test.txt') {
$out = min($out, filemtime($dir . '/' . $file));
}
}
}
return $out;
}
}