- <?php
- * @file
- * Test classes for verifying Backup and Restore functionality.
- */
-
- * Base class for testing Backup and Restore functionality.
- */
- class BackupBaseTestCase extends BackdropWebTestCase {
-
-
- * Array of errors triggered during the restore process.
- */
- protected $restoreErrors = array();
-
-
- * Admin user with the permission to run updates.
- *
- * @var User
- */
- protected $adminUser;
-
-
- * List of all backups from before the test was executed.
- *
- * @var array[]
- */
- protected $backups = array();
-
-
- * Checks that zlib is enabled in order to run the upgrade tests.
- */
- protected function checkRequirements() {
- $errors = parent::checkRequirements();
- if (!function_exists('gzopen')) {
- $errors[] = 'Missing zlib requirement for update tests.';
- }
- return $errors;
- }
-
-
-
- * Overrides BackdropWebTestCase::setUp() for upgrade testing.
- */
- protected function setUp() {
- $module_list = func_get_arg(0);
- parent::setUp($module_list);
- require_once BACKDROP_ROOT . '/core/includes/update.inc';
- require_once BACKDROP_ROOT . '/core/includes/backup.inc';
-
- $this->adminUser = $this->backdropCreateUser(array(
- 'access content',
- 'access user profiles',
- 'access administration pages',
- 'administer site configuration',
- 'administer software updates',
- 'restore site backups',
- 'view the administration theme',
- 'access site in maintenance mode',
- ));
-
-
- $this->backups = backup_directory_list();
- }
-
-
- * {@inheritdoc}
- */
- protected function tearDown() {
- parent::tearDown();
-
-
-
- $new_backups = array_diff_key(backup_directory_list(), $this->backups);
- foreach ($new_backups as $backup) {
- file_unmanaged_delete_recursive($backup['backup_directory']);
- }
- }
-
-
- * Restore a backup via restore.php.
- *
- * @return bool
- * TRUE if the restore succeeded, FALSE otherwise.
- */
- protected function restoreBackup() {
-
- $update_url = $GLOBALS['base_url'] . '/core/restore.php';
- $this->backdropGet($update_url, array('external' => TRUE));
- if (!$this->assertResponse(200)) {
- return FALSE;
- }
-
-
- $this->backdropPost(NULL, array(), t('Continue'));
- if (!$this->assertResponse(200)) {
- return FALSE;
- }
-
- $this->backdropPost(NULL, array(), t('Restore backup'));
-
-
- $error_messages = (array) $this->xpath('//li[@class=:class]', array(':class' => 'failure'));
- foreach ($error_messages as $element) {
- $message = strip_tags($element->asXML());
- $this->restoreErrors[] = $message;
- $this->fail($message);
- }
-
- if (!empty($this->restoreErrors)) {
- return FALSE;
- }
-
- $this->assertText(t('The backup was successfully restored.'));
- $this->assertLink(t('Return to your site'));
-
- return TRUE;
- }
- }
-
- * Tests backup settings form.
- */
- class BackupSettingsTestCase extends BackupBaseTestCase {
-
- * Overrides BackdropWebTestCase::setUp() for backup settings testing.
- */
- protected function setUp() {
- parent::setUp(array('backup_test'));
- }
-
-
- * Tests the backup settings form.
- */
- public function testBackupSettingsForm() {
-
- $this->backdropGet('<front>');
- $cached_homepage = cache_get(url('<front>', array('absolute' => TRUE)), 'cache_page');
- $this->assertTrue($cached_homepage, 'Cache exists for the site homepage.');
-
-
-
- $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.');
-
-
- $config_value = config_get('backup_test.settings', 'test_setting');
- $this->assertEqual($config_value, 'foo', 'Initial config value present in backup_test.settings config file.');
-
-
- $this->backdropGet('backup-test');
- $this->assertResponse(403, 'Settings form not accessible to non-admins.');
-
-
- $this->backdropLogin($this->adminUser);
- $this->backdropGet('backup-test');
- $this->assertResponse(200, 'Settings form accessible to an admin.');
-
-
- $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.');
-
-
-
-
- $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');
-
-
-
-
- $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');
-
-
- $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.');
-
-
- config_set('backup_test.settings', 'test_setting', 'bar');
- db_update('backup_test')
- ->condition('name', 'test_setting')
- ->fields(array(
- 'value' => 'bar',
- ))
- ->execute();
-
-
- $this->restoreBackup();
-
-
- $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.');
-
-
- $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.');
-
-
- 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.');
-
-
- 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.'));
-
-
- $two_backups1 = backup_directory_list();
- $this->assertTrue(count($two_backups1) == 2, 'Two backups created with backup limit of two.');
-
-
- $backup = $two_backups1['test_backup'];
- $this->assertEqual($backup['name'], 'test_backup');
- $this->assertTrue($backup['label'], 'Test Backup');
- $this->assertTrue($backup['description'], 'Test backup description.');
-
-
- 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.');
-
-
-
- $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.');
- }
- }
-
- * Tests backup and restore via update.php and restore.php.
- */
- class BackupUpdateTestCase extends BackupBaseTestCase {
-
- * Array of errors triggered during the upgrade process.
- */
- protected $upgradeErrors = array();
-
-
- * Overrides BackdropWebTestCase::setUp() for upgrade testing.
- */
- protected function setUp() {
- parent::setUp(array('update_test_1', 'update_test_2', 'update_test_3'));
-
- $this->upgradeErrors = array();
-
- db_query("UPDATE {system} SET schema_version = 0 WHERE name LIKE 'update_test_%'");
- }
-
-
- * Perform the upgrade.
- *
- * @return bool
- * TRUE if the upgrade succeeded, FALSE otherwise.
- */
- protected function performUpgradeWithBackup() {
-
- $update_url = $GLOBALS['base_url'] . '/core/update.php';
- $this->backdropGet($update_url, array('external' => TRUE));
- if (!$this->assertResponse(200)) {
- return FALSE;
- }
-
-
- $this->backdropPost(NULL, array(), t('Continue'));
- if (!$this->assertResponse(200)) {
- return FALSE;
- }
-
- $this->backdropPost(NULL, array(), t('Create backup'));
- if (!$this->assertResponse(200)) {
- return FALSE;
- }
-
-
- $this->backdropPost(NULL, array(), t('Apply pending updates'));
- if (!$this->assertText(t('Updates were attempted'))) {
- return FALSE;
- }
-
-
- foreach ($this->xpath('//li[@class=:class]', array(':class' => 'failure')) as $element) {
- $message = strip_tags($element->asXML());
- $this->upgradeErrors[] = $message;
- $this->fail($message);
- }
-
- if (!empty($this->upgradeErrors)) {
-
-
- return FALSE;
- }
-
-
- $this->backdropGet($update_url, array('external' => TRUE));
- $this->backdropPost(NULL, array(), t('Continue'));
- if (!$this->assertText(t('No pending updates.'), 'No pending updates at the end of the update process.')) {
- return FALSE;
- }
-
- $this->checkPermissions(array(), TRUE);
-
-
- $this->backdropGet('');
- return $this->assertText('Powered by Backdrop CMS', 'The home page of the upgraded site loads successfully.');
- }
-
-
- * Tests running an update with a backup and then restoring it.
- */
- public function testUpdateWithBackup() {
- $this->backdropLogin($this->adminUser);
-
-
- $original_site_name = 'Original site name ' . $this->randomName(16);
- $edit = array(
- 'site_name' => $original_site_name,
- );
- $this->backdropPost('admin/config/system/site-information', $edit, t('Save configuration'));
-
-
- $test_node1 = $this->backdropCreateNode();
- $test_user1 = $this->backdropCreateUser();
-
- $this->performUpgradeWithBackup();
-
-
- $test_node2 = $this->backdropCreateNode();
- $test_user2 = $this->backdropCreateUser();
-
-
- $new_site_name = 'New site name ' . $this->randomName(16);
- $edit = array(
- 'site_name' => $new_site_name,
- );
- $this->backdropPost('admin/config/system/site-information', $edit, t('Save configuration'));
- $this->assertText(t('The configuration options have been saved.'));
-
- $this->backdropGet('<front>');
- $this->assertText($new_site_name);
- $this->assertNoText($original_site_name);
-
- $this->restoreBackup();
-
- $this->backdropGet('<front>');
- $this->assertText('Powered by Backdrop CMS', 'The home page of the restored site loads successfully.');
-
-
- $this->assertText($original_site_name);
- $this->assertNoText($new_site_name);
-
-
- $this->backdropGet('node/' . $test_node1->nid);
- $this->assertResponse(200, 'Test content from before backup is accessible.');
- $this->backdropGet('user/' . $test_user1->uid);
- $this->assertResponse(200, 'Test user from before backup is accessible.');
-
-
- $this->backdropGet('node/' . $test_node2->nid);
- $this->assertResponse(404, 'Test content from after backup has been removed.');
- $this->backdropGet('user/' . $test_user2->uid);
- $this->assertResponse(404, 'Test user from after backup has been removed.');
- }
- }