Commit 1377aaef authored by catch's avatar catch
Browse files

Issue #1215104 by sun, lucascaro, effulgentsia: Use the non-interactive...

Issue #1215104 by sun, lucascaro, effulgentsia: Use the non-interactive installer in WebTestBase::setUp().
parent ff4df099
...@@ -439,18 +439,17 @@ function _batch_finished() { ...@@ -439,18 +439,17 @@ function _batch_finished() {
$queue->deleteQueue(); $queue->deleteQueue();
} }
} }
// Clean-up the session. Not needed for CLI updates.
if (isset($_SESSION)) {
unset($_SESSION['batches'][$batch['id']]);
if (empty($_SESSION['batches'])) {
unset($_SESSION['batches']);
}
}
} }
$_batch = $batch; $_batch = $batch;
$batch = NULL; $batch = NULL;
// Clean-up the session. Not needed for CLI updates.
if (isset($_SESSION)) {
unset($_SESSION['batches'][$batch['id']]);
if (empty($_SESSION['batches'])) {
unset($_SESSION['batches']);
}
}
// Redirect if needed. // Redirect if needed.
if ($_batch['progressive']) { if ($_batch['progressive']) {
// Revert the 'destination' that was saved in batch_process(). // Revert the 'destination' that was saved in batch_process().
......
...@@ -2576,7 +2576,12 @@ function drupal_fast_404() { ...@@ -2576,7 +2576,12 @@ function drupal_fast_404() {
* Returns TRUE if a Drupal installation is currently being attempted. * Returns TRUE if a Drupal installation is currently being attempted.
*/ */
function drupal_installation_attempted() { function drupal_installation_attempted() {
return defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install'; // This cannot rely on the MAINTENANCE_MODE constant, since that would prevent
// tests from using the non-interactive installer, in which case Drupal
// only happens to be installed within the same request, but subsequently
// executed code does not involve the installer at all.
// @see install_drupal()
return isset($GLOBALS['install_state']) && empty($GLOBALS['install_state']['installation_finished']);
} }
/** /**
......
...@@ -26,13 +26,17 @@ ...@@ -26,13 +26,17 @@
* @see Drupal\Core\Cache\CacheBackendInterface * @see Drupal\Core\Cache\CacheBackendInterface
*/ */
function cache($bin = 'cache') { function cache($bin = 'cache') {
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['cache'] = &drupal_static(__FUNCTION__, array());
}
$cache_objects = &$drupal_static_fast['cache'];
// Temporary backwards compatibiltiy layer, allow old style prefixed cache // Temporary backwards compatibiltiy layer, allow old style prefixed cache
// bin names to be passed as arguments. // bin names to be passed as arguments.
$bin = str_replace('cache_', '', $bin); $bin = str_replace('cache_', '', $bin);
// We do not use drupal_static() here because we do not want to change the
// storage of a cache bin mid-request.
static $cache_objects;
if (!isset($cache_objects[$bin])) { if (!isset($cache_objects[$bin])) {
$cache_objects[$bin] = CacheFactory::get($bin); $cache_objects[$bin] = CacheFactory::get($bin);
} }
......
...@@ -94,19 +94,29 @@ function install_drupal($settings = array()) { ...@@ -94,19 +94,29 @@ function install_drupal($settings = array()) {
throw $e; throw $e;
} }
} }
// After execution, all tasks might be complete, in which case
// $install_state['installation_finished'] is TRUE. In case the last task
// has been processed, remove the global $install_state, so other code can
// reliably check whether it is running during the installer.
// @see drupal_installation_attempted()
$state = $install_state;
if (!empty($install_state['installation_finished'])) {
unset($install_state);
}
// All available tasks for this page request are now complete. Interactive // All available tasks for this page request are now complete. Interactive
// installations can send output to the browser or redirect the user to the // installations can send output to the browser or redirect the user to the
// next page. // next page.
if ($install_state['interactive']) { if ($state['interactive']) {
if ($install_state['parameters_changed']) { if ($state['parameters_changed']) {
// Redirect to the correct page if the URL parameters have changed. // Redirect to the correct page if the URL parameters have changed.
install_goto(install_redirect_url($install_state)); install_goto(install_redirect_url($state));
} }
elseif (isset($output)) { elseif (isset($output)) {
// Display a page only if some output is available. Otherwise it is // Display a page only if some output is available. Otherwise it is
// possible that we are printing a JSON page and theme output should // possible that we are printing a JSON page and theme output should
// not be shown. // not be shown.
install_display_output($output, $install_state); install_display_output($output, $state);
} }
} }
} }
...@@ -224,7 +234,9 @@ function install_state_defaults() { ...@@ -224,7 +234,9 @@ function install_state_defaults() {
*/ */
function install_begin_request(&$install_state) { function install_begin_request(&$install_state) {
// Add any installation parameters passed in via the URL. // Add any installation parameters passed in via the URL.
$install_state['parameters'] += $_GET; if ($install_state['interactive']) {
$install_state['parameters'] += $_GET;
}
// Validate certain core settings that are used throughout the installation. // Validate certain core settings that are used throughout the installation.
if (!empty($install_state['parameters']['profile'])) { if (!empty($install_state['parameters']['profile'])) {
...@@ -240,11 +252,10 @@ function install_begin_request(&$install_state) { ...@@ -240,11 +252,10 @@ function install_begin_request(&$install_state) {
if (!$install_state['interactive']) { if (!$install_state['interactive']) {
drupal_override_server_variables($install_state['server']); drupal_override_server_variables($install_state['server']);
} }
// The user agent header is used to pass a database prefix in the request when // The user agent header is used to pass a database prefix in the request when
// running tests. However, for security reasons, it is imperative that no // running tests. However, for security reasons, it is imperative that no
// installation be permitted using such a prefix. // installation be permitted using such a prefix.
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE) { elseif (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
exit; exit;
} }
......
...@@ -10,26 +10,30 @@ ...@@ -10,26 +10,30 @@
/** /**
* Loads all the modules that have been enabled in the system table. * Loads all the modules that have been enabled in the system table.
* *
* @param $bootstrap * @param bool $bootstrap
* Whether to load only the reduced set of modules loaded in "bootstrap mode" * Whether to load only the reduced set of modules loaded in "bootstrap mode"
* for cached pages. See bootstrap.inc. * for cached pages. See bootstrap.inc. Pass NULL to only check the current
* status without loading of modules.
* @param bool $reset
* (optional) Internal use only. Whether to reset the internal statically
* cached flag of whether modules have been loaded. If TRUE, all modules are
* (re)loaded in the same call. Used by the testing framework to override and
* persist a limited module list for the duration of a unit test (in which no
* module system exists).
* *
* @return * @return bool
* If $bootstrap is NULL, return a boolean indicating whether all modules * A Boolean indicating whether all modules have been loaded. This means all
* have been loaded. * modules; the load status of bootstrap modules cannot be checked.
*/ */
function module_load_all($bootstrap = FALSE) { function module_load_all($bootstrap = FALSE, $reset = FALSE) {
// Already loaded code cannot be unloaded, but new modules may be added within static $has_run = FALSE;
// a request, which should be loaded as well.
// Use the advanced drupal_static() pattern, since this is called very often. if ($reset) {
// @see theme() $has_run = FALSE;
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['has_run'] = &drupal_static(__FUNCTION__, FALSE);
} }
$has_run = &$drupal_static_fast['has_run'];
if (isset($bootstrap)) { // Unless $boostrap is NULL, load the requested set of modules.
if (isset($bootstrap) && !$has_run) {
$type = $bootstrap ? 'bootstrap' : 'module_enabled'; $type = $bootstrap ? 'bootstrap' : 'module_enabled';
foreach (module_list($type) as $module) { foreach (module_list($type) as $module) {
drupal_load('module', $module); drupal_load('module', $module);
...@@ -60,14 +64,29 @@ function module_load_all($bootstrap = FALSE) { ...@@ -60,14 +64,29 @@ function module_load_all($bootstrap = FALSE) {
* list will persist until the next call with a new $fixed_list passed in. * list will persist until the next call with a new $fixed_list passed in.
* Primarily intended for internal use (e.g., in install.php and update.php). * Primarily intended for internal use (e.g., in install.php and update.php).
* Use module_list_reset() to undo the $fixed_list override. * Use module_list_reset() to undo the $fixed_list override.
* @param bool $reset
* (optional) Whether to reset/remove the $fixed_list.
* *
* @return array * @return array
* An associative array whose keys and values are the names of the modules in * An associative array whose keys and values are the names of the modules in
* the list. * the list.
*
* @see module_list_reset()
*/ */
function module_list($type = 'module_enabled', array $fixed_list = NULL) { function module_list($type = 'module_enabled', array $fixed_list = NULL, $reset = FALSE) {
// This static is only used for $fixed_list. // This static is only used for $fixed_list. It must not be a drupal_static(),
$module_list = &drupal_static(__FUNCTION__); // since any call to drupal_static_reset() in unit tests would cause an
// attempt to retrieve the list of modules from the database (which does not
// exist).
static $module_list;
if ($reset) {
$module_list = NULL;
// Do nothing if no $type and no $fixed_list have been passed.
if (!isset($type) && !isset($fixed_list)) {
return;
}
}
// The list that will be be returned. Separate from $module_list in order // The list that will be be returned. Separate from $module_list in order
// to not duplicate the static cache of system_list(). // to not duplicate the static cache of system_list().
...@@ -93,7 +112,7 @@ function module_list($type = 'module_enabled', array $fixed_list = NULL) { ...@@ -93,7 +112,7 @@ function module_list($type = 'module_enabled', array $fixed_list = NULL) {
* Subsequent calls to module_list() will no longer use a fixed list. * Subsequent calls to module_list() will no longer use a fixed list.
*/ */
function module_list_reset() { function module_list_reset() {
drupal_static_reset('module_list'); module_list(NULL, NULL, TRUE);
} }
/** /**
......
...@@ -254,10 +254,22 @@ function locale($string = NULL, $context = NULL, $langcode = NULL) { ...@@ -254,10 +254,22 @@ function locale($string = NULL, $context = NULL, $langcode = NULL) {
// Use the advanced drupal_static() pattern, since this is called very often. // Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast; static $drupal_static_fast;
if (!isset($drupal_static_fast)) { if (!isset($drupal_static_fast)) {
$drupal_static_fast['locale'] = &drupal_static(__FUNCTION__); $drupal_static_fast['locale'] = &drupal_static(__FUNCTION__, array('cache' => array(), 'exists' => NULL));
} }
$locale_t = &$drupal_static_fast['locale']; $locale_t = &$drupal_static_fast['locale']['cache'];
$locale_exists = &$drupal_static_fast['locale']['exists'];
// Check whether Locale module is actually installed and operational.
// The mere existence of locale() does not imply that Locale module is
// actually enabled and its database tables are installed. Since PHP code
// cannot be unloaded, this is typically the case in the environment that
// is executing a test.
if (!isset($locale_exists)) {
$locale_exists = function_exists('module_exists') && module_exists('locale');
}
if (!$locale_exists) {
return $string;
}
if (!isset($string)) { if (!isset($string)) {
// Return all cached strings if no string was specified // Return all cached strings if no string was specified
......
...@@ -665,6 +665,14 @@ protected function changeDatabasePrefix() { ...@@ -665,6 +665,14 @@ protected function changeDatabasePrefix() {
} }
Database::addConnectionInfo('default', 'default', $connection_info['default']); Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Additionally override global $databases, since the installer does not use
// the Database connection info.
// @see install_verify_database_settings()
// @see install_database_errors()
// @todo Fix installer to use Database connection info.
global $databases;
$databases['default']['default'] = $connection_info['default'];
// Indicate the database prefix was set up correctly. // Indicate the database prefix was set up correctly.
$this->setupDatabasePrefix = TRUE; $this->setupDatabasePrefix = TRUE;
} }
...@@ -698,7 +706,10 @@ protected function prepareEnvironment() { ...@@ -698,7 +706,10 @@ protected function prepareEnvironment() {
// Save further contextual information. // Save further contextual information.
$this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files'); $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
$this->originalProfile = drupal_get_profile(); $this->originalProfile = drupal_get_profile();
$this->originalUser = $user; $this->originalUser = clone $user;
// Ensure that the current session is not changed by the new environment.
drupal_save_session(FALSE);
// Save and clean the shutdown callbacks array because it is static cached // Save and clean the shutdown callbacks array because it is static cached
// and will be changed by the test run. Otherwise it will contain callbacks // and will be changed by the test run. Otherwise it will contain callbacks
...@@ -793,6 +804,10 @@ protected function tearDown() { ...@@ -793,6 +804,10 @@ protected function tearDown() {
// Restore original database connection. // Restore original database connection.
Database::removeConnection('default'); Database::removeConnection('default');
Database::renameConnection('simpletest_original_default', 'default'); Database::renameConnection('simpletest_original_default', 'default');
// @see TestBase::changeDatabasePrefix()
global $databases;
$connection_info = Database::getConnectionInfo('default');
$databases['default']['default'] = $connection_info['default'];
// Restore original globals. // Restore original globals.
$GLOBALS['theme_key'] = $this->originalThemeKey; $GLOBALS['theme_key'] = $this->originalThemeKey;
...@@ -801,9 +816,9 @@ protected function tearDown() { ...@@ -801,9 +816,9 @@ protected function tearDown() {
// Reset all static variables. // Reset all static variables.
drupal_static_reset(); drupal_static_reset();
// Restore has_run state. // Reset module list and module load status.
$has_run = &drupal_static('module_load_all'); module_list_reset();
$has_run = TRUE; module_load_all(FALSE, TRUE);
// Restore original in-memory configuration. // Restore original in-memory configuration.
$conf = $this->originalConf; $conf = $this->originalConf;
......
...@@ -595,6 +595,13 @@ protected function setUp() { ...@@ -595,6 +595,13 @@ protected function setUp() {
global $user, $conf; global $user, $conf;
$language_interface = language(LANGUAGE_TYPE_INTERFACE); $language_interface = language(LANGUAGE_TYPE_INTERFACE);
// When running tests through the Simpletest UI (vs. on the command line),
// Simpletest's batch conflicts with the installer's batch. Batch API does
// not support the concept of nested batches (in which the nested is not
// progressive), so we need to temporarily pretend there was no batch.
// Backup the currently running Simpletest batch.
$this->originalBatch = batch_get();
// Create the database prefix for this test. // Create the database prefix for this test.
$this->prepareDatabasePrefix(); $this->prepareDatabasePrefix();
...@@ -617,16 +624,75 @@ protected function setUp() { ...@@ -617,16 +624,75 @@ protected function setUp() {
return FALSE; return FALSE;
} }
// Preset the 'install_profile' system variable, so the first call into // Set the 'simpletest_parent_profile' variable to add the parent profile's
// system_rebuild_module_data() (in drupal_install_system()) will register // search path to the child site's search paths.
// the test's profile as a module. Without this, the installation profile of // @see drupal_system_listing()
// the parent site (executing the test) is registered, and the test $conf['simpletest_parent_profile'] = $this->originalProfile;
// profile's hook_install() and other hook implementations are never invoked.
$conf['install_profile'] = $this->profile; // Set installer parameters.
// @see install.php, install.core.inc
$connection_info = Database::getConnectionInfo('default');
$this->root_user = (object) array(
'name' => 'admin',
'mail' => 'admin@example.com',
'pass_raw' => $this->randomName(),
);
$settings = array(
'interactive' => FALSE,
'parameters' => array(
'profile' => $this->profile,
'langcode' => 'en',
),
'forms' => array(
'install_settings_form' => array(
'driver' => $connection_info['default']['driver'],
'username' => $connection_info['default']['username'],
'host' => $connection_info['default']['host'],
'port' => $connection_info['default']['port'],
'password' => $connection_info['default']['password'],
'database' => $connection_info['default']['database'],
'prefix' => $connection_info['default']['prefix'],
),
'install_configure_form' => array(
'site_name' => 'Drupal',
'site_mail' => 'simpletest@example.com',
'account' => array(
'name' => $this->root_user->name,
'mail' => $this->root_user->mail,
'pass' => array(
'pass1' => $this->root_user->pass_raw,
'pass2' => $this->root_user->pass_raw,
),
),
// form_type_checkboxes_value() requires NULL instead of FALSE values
// for programmatic form submissions to disable a checkbox.
'update_status_module' => array(
1 => NULL,
2 => NULL,
),
),
),
);
// Replace the global $user session with an anonymous user to resemble a
// regular installation.
$user = drupal_anonymous_user();
// Reset the static batch to remove Simpletest's batch operations.
$batch = &batch_get();
$batch = array();
// Execute the non-interactive installer.
require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
install_drupal($settings);
// Perform the actual Drupal installation. // Restore the original Simpletest batch.
include_once DRUPAL_ROOT . '/core/includes/install.inc'; $batch = &batch_get();
drupal_install_system(); $batch = $this->originalBatch;
// Revert install_begin_request() cache and lock service overrides.
unset($conf['cache_classes']);
unset($conf['lock_backend']);
// Set path variables. // Set path variables.
variable_set('file_public_path', $this->public_files_directory); variable_set('file_public_path', $this->public_files_directory);
...@@ -636,16 +702,8 @@ protected function setUp() { ...@@ -636,16 +702,8 @@ protected function setUp() {
// Set 'parent_profile' of simpletest to add the parent profile's // Set 'parent_profile' of simpletest to add the parent profile's
// search path to the child site's search paths. // search path to the child site's search paths.
// @see drupal_system_listing() // @see drupal_system_listing()
// @todo This may need to be primed like 'install_profile' above.
config('simpletest.settings')->set('parent_profile', $this->originalProfile)->save(); config('simpletest.settings')->set('parent_profile', $this->originalProfile)->save();
// Include the testing profile.
variable_set('install_profile', $this->profile);
$profile_details = install_profile_info($this->profile, 'en');
// Install the modules specified by the testing profile.
module_enable($profile_details['dependencies'], FALSE);
// Collect modules to install. // Collect modules to install.
$class = get_class($this); $class = get_class($this);
$modules = array(); $modules = array();
...@@ -660,14 +718,6 @@ protected function setUp() { ...@@ -660,14 +718,6 @@ protected function setUp() {
$this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules)))); $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules))));
} }
// Run the profile tasks.
$install_profile_module_exists = db_query("SELECT 1 FROM {system} WHERE type = 'module' AND name = :name", array(
':name' => $this->profile,
))->fetchField();
if ($install_profile_module_exists) {
module_enable(array($this->profile), FALSE);
}
// Create a new DrupalKernel for testing purposes, now that all required // Create a new DrupalKernel for testing purposes, now that all required
// modules have been enabled. This also stores a new dependency injection // modules have been enabled. This also stores a new dependency injection
// container in drupal_container(). Drupal\simpletest\TestBase::tearDown() // container in drupal_container(). Drupal\simpletest\TestBase::tearDown()
...@@ -687,25 +737,6 @@ protected function setUp() { ...@@ -687,25 +737,6 @@ protected function setUp() {
// Reset/rebuild all data structures after enabling the modules. // Reset/rebuild all data structures after enabling the modules.
$this->resetAll(); $this->resetAll();
// Run cron once in that environment, as install.php does at the end of
// the installation process.
drupal_cron_run();
// Ensure that the session is not written to the new environment and replace
// the global $user session with uid 1 from the new test site.
drupal_save_session(FALSE);
// Login as uid 1.
$user = user_load(1);
// Restore necessary variables.
variable_set('install_task', 'done');
config('system.site')->set('mail', 'simpletest@example.com')->save();
variable_set('date_default_timezone', date_default_timezone_get());
// Set up English language.
unset($conf['language_default']);
$language_interface = language_default();
// Use the test mail class instead of the default mail handler class. // Use the test mail class instead of the default mail handler class.
variable_set('mail_system', array('default-system' => 'Drupal\Core\Mail\VariableLog')); variable_set('mail_system', array('default-system' => 'Drupal\Core\Mail\VariableLog'));
......
...@@ -191,13 +191,13 @@ protected function performUpgrade($register_errors = TRUE) { ...@@ -191,13 +191,13 @@ protected function performUpgrade($register_errors = TRUE) {
$update_url = $GLOBALS['base_url'] . '/core/update.php'; $update_url = $GLOBALS['base_url'] . '/core/update.php';
$this->drupalGet($update_url, array('external' => TRUE)); $this->drupalGet($update_url, array('external' => TRUE));
if (!$this->assertResponse(200)) { if (!$this->assertResponse(200)) {
return FALSE; throw new Exception('Initial GET to update.php did not return HTTP 200 status.');
} }
// Continue. // Continue.
$this->drupalPost(NULL, array(), t('Continue')); $this->drupalPost(NULL, array(), t('Continue'));
if (!$this->assertResponse(200)) { if (!$this->assertResponse(200)) {
return FALSE; throw new Exception('POST to continue update.php did not return HTTP 200 status.');
} }
// The test should pass if there are no pending updates. // The test should pass if there are no pending updates.
...@@ -211,7 +211,7 @@ protected function performUpgrade($register_errors = TRUE) { ...@@ -211,7 +211,7 @@ protected function performUpgrade($register_errors = TRUE) {
// Go! // Go!
$this->drupalPost(NULL, array(), t('Apply pending updates')); $this->drupalPost(NULL, array(), t('Apply pending updates'));
if (!$this->assertResponse(200)) { if (!$this->assertResponse(200)) {
return FALSE; throw new Exception('POST to update.php to apply pending updates did not return HTTP 200 status.');
} }
// Check for errors during the update process. // Check for errors during the update process.
...@@ -222,36 +222,30 @@ protected function performUpgrade($register_errors = TRUE) { ...@@ -222,36 +222,30 @@ protected function performUpgrade($register_errors = TRUE) {
$this->fail($message); $this->fail($message);
} }
} }
if (!empty($this->upgradeErrors)) { if (!empty($this->upgradeErrors)) {
// Upgrade failed, the installation might be in an inconsistent state, // Upgrade failed, the installation might be in an inconsistent state,
// don't process. // don't process.
return FALSE; throw new Exception('Errors during update process.');