1 backup.test | public BackupSettingsTestCase::testBackupSettingsForm() |
Tests the backup settings form.
File
- core/
modules/ simpletest/ tests/ backup.test, line 136 - Test classes for verifying Backup and Restore functionality.
Class
- BackupSettingsTestCase
- Tests backup settings form.
Code
public function testBackupSettingsForm() {
// Hit the homepage to ensure a cache entry exists in cache_page.
$this->backdropGet('<front>');
$cached_homepage = cache_get(url('<front>', array('absolute' => TRUE)), 'cache_page');
$this->assertTrue($cached_homepage, 'Cache exists for the site homepage.');
// Check that the "backup_test" table was created and has a value, as set
// in backup_test.install.
$database_row = db_query('SELECT * FROM {backup_test}')->fetchAssoc();
$this->assertEqual($database_row['name'], 'test_value1', 'Initial value present in backup_test table before backup.');
// Check that the default config file has the expected value.
$config_value = config_get('backup_test.settings', 'test_setting');
$this->assertEqual($config_value, 'foo', 'Initial config value present in backup_test.settings config file.');
// Verify backup page requires permissions to view.
$this->backdropGet('backup-test');
$this->assertResponse(403, 'Settings form not accessible to non-admins.');
// Log in as admin account with "restore site backups" permission.
$this->backdropLogin($this->adminUser);
$this->backdropGet('backup-test');
$this->assertResponse(200, 'Settings form accessible to an admin.');
// Verify defaults are as expected.
$this->assertFieldChecked('edit-targets-db-default-enabled', 'Default database backup checked.');
$this->assertNoFieldChecked('edit-targets-db-default-settings-lock-tables', 'Lock tables on default database not checked.');
// When running inside a test, only the simpletest prefix for this test run
// should be included in the backup. This means that the "system" and all
// other non-prefixed tables should not be included.
$original_connection = Database::getConnection('default', 'simpletest_original_default');
$original_prefix = $original_connection->tablePrefix('system');
$test_prefix = $this->databasePrefix;
$include_exclude_id = 'edit-targets-db-default-settings-include-exclude';
$this->assertOptionSelected($include_exclude_id, 'include');
$exclude_tables_id = 'edit-targets-db-default-settings-include-tables';
$this->assertOptionSelected($exclude_tables_id, $test_prefix . 'system');
$this->assertOptionSelected($exclude_tables_id, $test_prefix . 'cache_page');
$this->assertNoOptionSelected($exclude_tables_id, $original_prefix . 'system');
$this->assertNoOptionSelected($exclude_tables_id, $original_prefix . 'cache_page');
// The cache tables for the test prefix should have their data excluded, but
// the original prefix's table should not be data excluded (the entire table
// is skipped per above).
$nodata_tables_id = 'edit-targets-db-default-settings-nodata-tables';
$this->assertNoOptionSelected($nodata_tables_id, $original_prefix . 'cache_page');
$this->assertOptionSelected($nodata_tables_id, $test_prefix . 'cache_page');
// Create a backup with the default settings.
$original_backups = backup_directory_list();
$this->backdropPost(NULL, array(), t('Create backup'));
$this->assertText(t('Backup created successfully.'));
$new_backups = backup_directory_list();
$this->assertTrue(count($new_backups) === (count($original_backups) + 1), 'New backup directory created.');
// Now with the backup created, make changes to the database and config.
config_set('backup_test.settings', 'test_setting', 'bar');
db_update('backup_test')
->condition('name', 'test_setting')
->fields(array(
'value' => 'bar',
))
->execute();
// Restore the backup via restore.php.
$this->restoreBackup();
// Confirm the page cache cleared out as part of the restore.
$cached_homepage = cache_get(url('<front>', array('absolute' => TRUE)), 'cache_page');
$this->assertFalse($cached_homepage, 'The home page does not have a cache after restoring the database backup, because its table data was excluded.');
// Confirm the backup_test table's contents were restored.
$database_row = db_query('SELECT * FROM {backup_test}')->fetchAssoc();
$this->assertEqual($database_row['name'], 'test_value1', 'Initial value present in backup_test table before backup.');
// Check that the config file data has been restored.
backdrop_static_reset('config');
$config_value = config_get('backup_test.settings', 'test_setting');
$this->assertEqual($config_value, 'foo', 'Initial config value present in backup_test.settings config file.');
// Set a backup limit of 2 and create a second backup.
config_set('system.backup', 'backup_limit', 2);
sleep(1);
$edit = array(
'options[name]' => 'test_backup',
'options[label]' => 'Test Backup',
'options[description]' => 'Test backup description.',
);
$this->backdropPost('backup-test', $edit, t('Create backup'));
$this->assertText(t('Backup created successfully.'));
// Confirm that at least two backups exist.
$two_backups1 = backup_directory_list();
$this->assertTrue(count($two_backups1) == 2, 'Two backups created with backup limit of two.');
// And confirm the saved values are present.
$backup = $two_backups1['test_backup'];
$this->assertEqual($backup['name'], 'test_backup');
$this->assertTrue($backup['label'], 'Test Backup');
$this->assertTrue($backup['description'], 'Test backup description.');
// Create a 3rd backup, and confirm that the oldest one is deleted.
sleep(1);
$this->backdropPost('backup-test', array(), t('Create backup'));
$this->assertText(t('Backup created successfully.'));
$two_backups2 = backup_directory_list();
$this->assertTrue(count($two_backups2) == 2, 'Still only two backups after creating third backup with limit of two.');
// Backups are listed alphabetically, and since that is also chronological,
// the first item would be the oldest backup.
$deleted_backup_key = key($two_backups1);
$this->assertTrue(!in_array($deleted_backup_key, $two_backups2), 'The oldest backup was deleted to keep within the backup limit.');
}