340 lines
11 KiB
Plaintext
340 lines
11 KiB
Plaintext
<?php
|
|
|
|
use Drupal\Core\Database\Database;
|
|
|
|
/**
|
|
* Perform end-to-end tests of the upgrade path.
|
|
*/
|
|
abstract class UpgradePathTestCase extends DrupalWebTestCase {
|
|
|
|
/**
|
|
* The file path(s) to the dumped database(s) to load into the child site.
|
|
*
|
|
* @var array
|
|
*/
|
|
var $databaseDumpFiles = array();
|
|
|
|
/**
|
|
* Flag that indicates whether the child site has been upgraded.
|
|
*/
|
|
var $upgradedSite = FALSE;
|
|
|
|
/**
|
|
* Array of errors triggered during the upgrade process.
|
|
*/
|
|
var $upgradeErrors = array();
|
|
|
|
/**
|
|
* Array of modules loaded when the test starts.
|
|
*/
|
|
var $loadedModules = array();
|
|
|
|
/**
|
|
* Checks that zlib is enabled in order to run the upgrade tests.
|
|
*/
|
|
protected function checkRequirements() {
|
|
if (!function_exists('gzopen')) {
|
|
return array(
|
|
'Missing zlib requirement for upgrade tests.',
|
|
);
|
|
}
|
|
return parent::checkRequirements();
|
|
}
|
|
|
|
/**
|
|
* Override of DrupalWebTestCase::setUp() specialized for upgrade testing.
|
|
*/
|
|
protected function setUp() {
|
|
global $user, $language_interface, $conf;
|
|
|
|
// Load the Update API.
|
|
require_once DRUPAL_ROOT . '/core/includes/update.inc';
|
|
|
|
// Reset flags.
|
|
$this->upgradedSite = FALSE;
|
|
$this->upgradeErrors = array();
|
|
|
|
$this->loadedModules = module_list();
|
|
|
|
// Generate a temporary prefixed database to ensure that tests have a clean starting point.
|
|
$this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
|
|
db_update('simpletest_test_id')
|
|
->fields(array('last_prefix' => $this->databasePrefix))
|
|
->condition('test_id', $this->testId)
|
|
->execute();
|
|
|
|
// Clone the current connection and replace the current prefix.
|
|
$connection_info = Database::getConnectionInfo('default');
|
|
Database::renameConnection('default', 'simpletest_original_default');
|
|
foreach ($connection_info as $target => $value) {
|
|
$connection_info[$target]['prefix'] = array(
|
|
'default' => $value['prefix']['default'] . $this->databasePrefix,
|
|
);
|
|
}
|
|
Database::addConnectionInfo('default', 'default', $connection_info['default']);
|
|
|
|
// Store necessary current values before switching to prefixed database.
|
|
$this->originalLanguage = $language_interface;
|
|
$this->originalLanguageDefault = variable_get('language_default');
|
|
$this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
|
|
$this->originalProfile = drupal_get_profile();
|
|
$clean_url_original = variable_get('clean_url', 0);
|
|
|
|
// Unregister the registry.
|
|
// This is required to make sure that the database layer works properly.
|
|
spl_autoload_unregister('drupal_autoload_class');
|
|
spl_autoload_unregister('drupal_autoload_interface');
|
|
|
|
// Create test directories ahead of installation so fatal errors and debug
|
|
// information can be logged during installation process.
|
|
// Use mock files directories with the same prefix as the database.
|
|
$public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
|
|
$private_files_directory = $public_files_directory . '/private';
|
|
$temp_files_directory = $private_files_directory . '/temp';
|
|
|
|
// Create the directories.
|
|
file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
|
file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY);
|
|
file_prepare_directory($temp_files_directory, FILE_CREATE_DIRECTORY);
|
|
$this->generatedTestFiles = FALSE;
|
|
|
|
// Log fatal errors.
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', $public_files_directory . '/error.log');
|
|
|
|
// Reset all statics and variables to perform tests in a clean environment.
|
|
$conf = array();
|
|
|
|
// Load the database from the portable PHP dump.
|
|
// The files can be gzipped.
|
|
foreach ($this->databaseDumpFiles as $file) {
|
|
if (substr($file, -3) == '.gz') {
|
|
$file = "compress.zlib://$file";
|
|
}
|
|
require $file;
|
|
}
|
|
|
|
// Set path variables.
|
|
$this->variable_set('file_public_path', $public_files_directory);
|
|
$this->variable_set('file_private_path', $private_files_directory);
|
|
$this->variable_set('file_temporary_path', $temp_files_directory);
|
|
|
|
$this->pass('Finished loading the dump.');
|
|
|
|
// Load user 1.
|
|
$this->originalUser = $user;
|
|
drupal_save_session(FALSE);
|
|
$user = db_query('SELECT * FROM {users} WHERE uid = :uid', array(':uid' => 1))->fetchObject();
|
|
|
|
// Generate and set a D6-compatible session cookie.
|
|
$this->curlInitialize();
|
|
$sid = drupal_hash_base64(uniqid(mt_rand(), TRUE) . drupal_random_bytes(55));
|
|
curl_setopt($this->curlHandle, CURLOPT_COOKIE, rawurlencode(session_name()) . '=' . rawurlencode($sid));
|
|
|
|
// Force our way into the session of the child site.
|
|
drupal_save_session(TRUE);
|
|
_drupal_session_write($sid, '');
|
|
// Remove the temporarily added ssid column.
|
|
drupal_save_session(FALSE);
|
|
|
|
// Restore necessary variables.
|
|
$this->variable_set('clean_url', $clean_url_original);
|
|
$this->variable_set('site_mail', 'simpletest@example.com');
|
|
|
|
drupal_set_time_limit($this->timeLimit);
|
|
$this->setup = TRUE;
|
|
}
|
|
|
|
/**
|
|
* Override of DrupalWebTestCase::tearDown() specialized for upgrade testing.
|
|
*/
|
|
protected function tearDown() {
|
|
global $user, $language_interface;
|
|
|
|
// In case a fatal error occurred that was not in the test process read the
|
|
// log to pick up any fatal errors.
|
|
simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);
|
|
|
|
// Delete temporary files directory.
|
|
file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10));
|
|
|
|
// Get back to the original connection.
|
|
Database::removeConnection('default');
|
|
Database::renameConnection('simpletest_original_default', 'default');
|
|
|
|
// Remove all prefixed tables.
|
|
$tables = db_find_tables($this->databasePrefix . '%');
|
|
foreach ($tables as $table) {
|
|
db_drop_table($table);
|
|
}
|
|
|
|
// Return the user to the original one.
|
|
$user = $this->originalUser;
|
|
drupal_save_session(TRUE);
|
|
|
|
// Ensure that internal logged in variable and cURL options are reset.
|
|
$this->loggedInUser = FALSE;
|
|
$this->additionalCurlOptions = array();
|
|
|
|
// Reload module list and implementations to ensure that test module hooks
|
|
// aren't called after tests.
|
|
module_list(TRUE);
|
|
module_implements_reset();
|
|
|
|
// Reset the Field API.
|
|
field_cache_clear();
|
|
|
|
// Rebuild caches.
|
|
parent::refreshVariables();
|
|
|
|
// Reset language.
|
|
$language_interface = $this->originalLanguage;
|
|
if ($this->originalLanguageDefault) {
|
|
$GLOBALS['conf']['language_default'] = $this->originalLanguageDefault;
|
|
}
|
|
|
|
// Close the CURL handler.
|
|
$this->curlClose();
|
|
}
|
|
|
|
/**
|
|
* Specialized variable_set() that works even if the child site is not upgraded.
|
|
*
|
|
* @param $name
|
|
* The name of the variable to set.
|
|
* @param $value
|
|
* The value to set. This can be any PHP data type; these functions take care
|
|
* of serialization as necessary.
|
|
*/
|
|
protected function variable_set($name, $value) {
|
|
db_delete('variable')
|
|
->condition('name', $name)
|
|
->execute();
|
|
db_insert('variable')
|
|
->fields(array(
|
|
'name' => $name,
|
|
'value' => serialize($value),
|
|
))
|
|
->execute();
|
|
|
|
try {
|
|
cache()->delete('variables');
|
|
cache('bootstrap')->delete('variables');
|
|
}
|
|
// Since cache_bootstrap won't exist in a Drupal 6 site, ignore the
|
|
// exception if the above fails.
|
|
catch (Exception $e) {}
|
|
}
|
|
|
|
/**
|
|
* Specialized refreshVariables().
|
|
*/
|
|
protected function refreshVariables() {
|
|
// No operation if the child has not been upgraded yet.
|
|
if (!$this->upgradedSite) {
|
|
return parent::refreshVariables();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Perform the upgrade.
|
|
*
|
|
* @param $register_errors
|
|
* Register the errors during the upgrade process as failures.
|
|
* @return
|
|
* TRUE if the upgrade succeeded, FALSE otherwise.
|
|
*/
|
|
protected function performUpgrade($register_errors = TRUE) {
|
|
|
|
// Load the first update screen.
|
|
$update_url = $GLOBALS['base_url'] . '/core/update.php';
|
|
$this->drupalGet($update_url, array('external' => TRUE));
|
|
if (!$this->assertResponse(200)) {
|
|
return FALSE;
|
|
}
|
|
|
|
// Continue.
|
|
$this->drupalPost(NULL, array(), t('Continue'));
|
|
if (!$this->assertResponse(200)) {
|
|
return FALSE;
|
|
}
|
|
|
|
// Go!
|
|
$this->drupalPost(NULL, array(), t('Apply pending updates'));
|
|
if (!$this->assertText('Updates were attempted')) {
|
|
return FALSE;
|
|
}
|
|
|
|
// Check for errors during the update process.
|
|
foreach ($this->xpath('//li[@class=:class]', array(':class' => 'failure')) as $element) {
|
|
$message = strip_tags($element->asXML());
|
|
$this->upgradeErrors[] = $message;
|
|
if ($register_errors) {
|
|
$this->fail($message);
|
|
}
|
|
}
|
|
|
|
if (!empty($this->upgradeErrors)) {
|
|
// Upgrade failed, the installation might be in an inconsistent state,
|
|
// don't process.
|
|
return FALSE;
|
|
}
|
|
|
|
// Check if there still are pending updates.
|
|
$this->drupalGet($update_url, array('external' => TRUE));
|
|
$this->drupalPost(NULL, array(), t('Continue'));
|
|
if (!$this->assertText(t('No pending updates.'), t('No pending updates at the end of the update process.'))) {
|
|
return FALSE;
|
|
}
|
|
|
|
// Upgrade succeed, rebuild the environment so that we can call the API
|
|
// of the child site directly from this request.
|
|
$this->upgradedSite = TRUE;
|
|
|
|
// Reload module list. For modules that are enabled in the test database,
|
|
// but not on the test client, we need to load the code here.
|
|
$new_modules = array_diff(module_list(TRUE), $this->loadedModules);
|
|
foreach ($new_modules as $module) {
|
|
drupal_load('module', $module);
|
|
}
|
|
|
|
// Re-register autoload functions.
|
|
spl_autoload_register('drupal_autoload_class');
|
|
spl_autoload_register('drupal_autoload_interface');
|
|
|
|
// Reload hook implementations
|
|
module_implements_reset();
|
|
|
|
// Rebuild caches.
|
|
drupal_static_reset();
|
|
drupal_flush_all_caches();
|
|
|
|
// Reload global $conf array and permissions.
|
|
$this->refreshVariables();
|
|
$this->checkPermissions(array(), TRUE);
|
|
|
|
// Check we can load the front page of the new site.
|
|
$this->drupalGet('');
|
|
return $this->assertText('Powered by Drupal', t('The front page of the upgraded site loads successfully.'));
|
|
}
|
|
|
|
/**
|
|
* Force uninstall all modules from a test database, except those listed.
|
|
*
|
|
* @param $modules
|
|
* The list of modules to keep installed. Required core modules will
|
|
* always be kept.
|
|
*/
|
|
protected function uninstallModulesExcept(array $modules) {
|
|
$required_modules = array('block', 'dblog', 'filter', 'node', 'system', 'update', 'user');
|
|
|
|
$modules = array_merge($required_modules, $modules);
|
|
|
|
db_delete('system')
|
|
->condition('type', 'module')
|
|
->condition('name', $modules, 'NOT IN')
|
|
->execute();
|
|
}
|
|
|
|
}
|