/** * Tests the drupal_rewrite_settings() function. */ function testDrupalRewriteSettings() { include_once \Drupal::root() . '/core/includes/install.inc'; $site_path = $this->container->get('site.path'); $tests = array(array('original' => '$no_index_value_scalar = TRUE;', 'settings' => array('no_index_value_scalar' => (object) array('value' => FALSE, 'comment' => 'comment')), 'expected' => '$no_index_value_scalar = false; // comment'), array('original' => '$no_index_value_scalar = TRUE;', 'settings' => array('no_index_value_foo' => array('foo' => array('value' => (object) array('value' => NULL, 'required' => TRUE, 'comment' => 'comment')))), 'expected' => <<<'EXPECTED' $no_index_value_scalar = TRUE; $no_index_value_foo['foo']['value'] = NULL; // comment EXPECTED ), array('original' => '$no_index_value_array = array("old" => "value");', 'settings' => array('no_index_value_array' => (object) array('value' => FALSE, 'required' => TRUE, 'comment' => 'comment')), 'expected' => '$no_index_value_array = array("old" => "value"); $no_index_value_array = false; // comment'), array('original' => '$has_index_value_scalar["foo"]["bar"] = NULL;', 'settings' => array('has_index_value_scalar' => array('foo' => array('bar' => (object) array('value' => FALSE, 'required' => TRUE, 'comment' => 'comment')))), 'expected' => '$has_index_value_scalar["foo"]["bar"] = false; // comment'), array('original' => '$has_index_value_scalar["foo"]["bar"] = "foo";', 'settings' => array('has_index_value_scalar' => array('foo' => array('value' => (object) array('value' => array('value' => 2), 'required' => TRUE, 'comment' => 'comment')))), 'expected' => <<<'EXPECTED' $has_index_value_scalar["foo"]["bar"] = "foo"; $has_index_value_scalar['foo']['value'] = array ( 'value' => 2, ); // comment EXPECTED )); foreach ($tests as $test) { $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php'; file_put_contents(\Drupal::root() . '/' . $filename, "<?php\n" . $test['original'] . "\n"); drupal_rewrite_settings($test['settings'], $filename); $this->assertEqual(file_get_contents(\Drupal::root() . '/' . $filename), "<?php\n" . $test['expected'] . "\n"); } // Test that <?php gets added to the start of an empty settings file. // Set the array of settings that will be written to the file. $test = array('settings' => array('no_index' => (object) array('value' => TRUE, 'required' => TRUE)), 'expected' => '$no_index = true;'); // Make an empty file. $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php'; file_put_contents(\Drupal::root() . '/' . $filename, ""); // Write the setting to the file. drupal_rewrite_settings($test['settings'], $filename); // Check that the result is just the php opening tag and the settings. $this->assertEqual(file_get_contents(\Drupal::root() . '/' . $filename), "<?php\n" . $test['expected'] . "\n"); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { global $config_directories, $install_state; $sync_directory = $form_state->getValue('sync_directory'); if ($sync_directory != config_get_config_directory(CONFIG_SYNC_DIRECTORY)) { $settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) array('value' => $sync_directory, 'required' => TRUE); drupal_rewrite_settings($settings); $config_directories[CONFIG_SYNC_DIRECTORY] = $sync_directory; } if ($path = $form_state->getValue('import_tarball')) { // Ensure that we have an empty directory if we're going. $sync = new FileStorage($sync_directory); $sync->deleteAll(); try { $archiver = new ArchiveTar($path, 'gz'); $files = array(); foreach ($archiver->listContent() as $file) { $files[] = $file['filename']; } $archiver->extractList($files, config_get_config_directory(CONFIG_SYNC_DIRECTORY)); drupal_set_message($this->t('Your configuration files were successfully uploaded, ready for import.')); } catch (\Exception $e) { drupal_set_message($this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())), 'error'); } drupal_unlink($path); } // Change the langcode to the site default langcode provided by the // configuration. $config_storage = new FileStorage(config_get_config_directory(CONFIG_SYNC_DIRECTORY)); $install_state['parameters']['langcode'] = $config_storage->read('system.site')['langcode']; }
/** * Tests rebuild_token_calculator.sh. */ public function testRebuildTokenCalculatorSh() { // The script requires a settings.php with a hash salt setting. $filename = $this->siteDirectory . '/settings.php'; touch($filename); $settings['settings']['hash_salt'] = (object) array('value' => 'some_random_key', 'required' => TRUE); drupal_rewrite_settings($settings, $filename); $_SERVER['argv'] = array('core/scripts/rebuild_token_calculator.sh'); ob_start(); include DRUPAL_ROOT . '/core/scripts/rebuild_token_calculator.sh'; $this->setRawContent(ob_get_contents()); ob_end_clean(); $this->assertRaw('token='); }
public function generate_file($database, $db_user, $db_pass, $project_domain) { $value = "mysql://" . $db_user . ":" . $db_pass . "@localhost/" . $database; $settings['db_url'] = array('value' => $value); try { require_once DRUPAL_ROOT . '/includes/install.inc'; $variables['url'] = "http://" . $project_domain . '/index.php'; // Override variables since this is obviously run from CLI // We do this to confuse the cat... err. conf_path() $this->drupal_override_server_variables($variables); // Rewrite the settings file in its' correct place drupal_rewrite_settings($settings); } catch (Exception $e) { throw new RumCouldNotCreateSettingsFileException($e->getMessage()); } return TRUE; }
public function generate_file($database, $db_user, $db_pass, $project_domain) { // Generate settings for the database $settings['databases'] = array('value' => array('default' => array('default' => array('prefix' => '', 'host' => 'localhost', 'driver' => 'mysql', 'database' => $database, 'username' => $db_user, 'password' => $db_pass))), 'required' => TRUE); try { require_once DRUPAL_ROOT . '/includes/install.inc'; $variables['url'] = "http://" . $project_domain . '/index.php'; // Override variables since this is obviously run from CLI // We do this to confuse the cat... err. conf_path() drupal_override_server_variables($variables); // Rewrite the settings file in its' correct place drupal_rewrite_settings($settings); } catch (Exception $e) { throw new RumCouldNotCreateSettingsFileException($e->getMessage()); } return TRUE; }
/** * Form API submit for install_settings form. */ function install_settings_form_submit($form, &$form_state) { global $profile, $install_locale; // Update global settings array and save $settings['db_url'] = array('value' => $form_state['values']['_db_url'], 'required' => TRUE); $settings['db_prefix'] = array('value' => $form_state['values']['db_prefix'], 'required' => TRUE); drupal_rewrite_settings($settings); // Continue to install profile step install_goto("install.php?profile={$profile}" . ($install_locale ? "&locale={$install_locale}" : '')); }
/** * Rewrites the settings.php file of the test site. * * @param array $settings * An array of settings to write out, in the format expected by * drupal_rewrite_settings(). * * @see drupal_rewrite_settings() */ protected function writeSettings(array $settings) { include_once DRUPAL_ROOT . '/core/includes/install.inc'; $filename = $this->siteDirectory . '/settings.php'; // system_requirements() removes write permissions from settings.php // whenever it is invoked. // Not using File API; a potential error must trigger a PHP warning. chmod($filename, 0666); drupal_rewrite_settings($settings, $filename); }
/** * Form API submit for install_settings form. */ function install_settings_form_submit($form, &$form_state) { global $profile, $install_locale; $database = array_intersect_key($form_state['values']['_database'], array_flip(array('driver', 'database', 'username', 'password', 'host', 'port'))); // Update global settings array and save $settings['databases'] = array('value' => array('default' => array('default' => $database)), 'required' => TRUE); $settings['db_prefix'] = array('value' => $form_state['values']['db_prefix'], 'required' => TRUE); drupal_rewrite_settings($settings); // Continue to install profile step install_goto("install.php?profile={$profile}" . ($install_locale ? "&locale={$install_locale}" : '')); }
/** * Form API submit for install_settings form. */ function install_settings_form_submit($form, &$form_state) { global $install_state; $database = array_intersect_key($form_state['values']['_database'], array_flip(array('driver', 'database', 'username', 'password', 'host', 'port'))); // Update global settings array and save $settings['databases'] = array('value' => array('default' => array('default' => $database)), 'required' => TRUE); $settings['db_prefix'] = array('value' => $form_state['values']['db_prefix'], 'required' => TRUE); drupal_rewrite_settings($settings); // Indicate that the settings file has been verified, and check the database // for the last completed task, now that we have a valid connection. This // last step is important since we want to trigger an error if the new // database already has Drupal installed. $install_state['settings_verified'] = TRUE; $install_state['completed_task'] = install_verify_completed_task(); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { global $install_state; // Update global settings array and save. $settings = array(); $database = $form_state->get('database'); $settings['databases']['default']['default'] = (object) array('value' => $database, 'required' => TRUE); $settings['settings']['hash_salt'] = (object) array('value' => Crypt::randomBytesBase64(55), 'required' => TRUE); // Remember the profile which was used. $settings['settings']['install_profile'] = (object) array('value' => $install_state['parameters']['profile'], 'required' => TRUE); drupal_rewrite_settings($settings); // Add the config directories to settings.php. drupal_install_config_directories(); // Indicate that the settings file has been verified, and check the database // for the last completed task, now that we have a valid connection. This // last step is important since we want to trigger an error if the new // database already has Drupal installed. $install_state['settings_verified'] = TRUE; $install_state['config_verified'] = TRUE; $install_state['database_verified'] = TRUE; $install_state['completed_task'] = install_verify_completed_task(); }
/** * Helper function to run pending database updates. */ protected function runUpdates() { if (!$this->zlibInstalled) { $this->fail('Missing zlib requirement for update tests.'); return FALSE; } // The site might be broken at the time so logging in using the UI might // not work, so we use the API itself. drupal_rewrite_settings(['settings' => ['update_free_access' => (object) ['value' => TRUE, 'required' => TRUE]]]); $this->drupalGet($this->updateUrl); $this->clickLink(t('Continue')); // Run the update hooks. $this->clickLink(t('Apply pending updates')); // The config schema can be incorrect while the update functions are being // executed. But once the update has been completed, it needs to be valid // again. Assert the schema of all configuration objects now. $names = $this->container->get('config.storage')->listAll(); /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */ $typed_config = $this->container->get('config.typed'); foreach ($names as $name) { $config = $this->config($name); $this->assertConfigSchema($typed_config, $name, $config->get()); } }
/** * Helper function to run pending database updates. */ protected function runUpdates() { if (!$this->zlibInstalled) { $this->fail('Missing zlib requirement for upgrade tests.'); return FALSE; } // The site might be broken at the time so logging in using the UI might // not work, so we use the API itself. drupal_rewrite_settings(['settings' => ['update_free_access' => (object) ['value' => TRUE, 'required' => TRUE]]]); $this->drupalGet($this->updateUrl); $this->clickLink(t('Continue')); // Run the update hooks. $this->clickLink(t('Apply pending updates')); }
/** * Helper function to run pending database updates. */ protected function runUpdates() { if (!$this->zlibInstalled) { $this->fail('Missing zlib requirement for update tests.'); return FALSE; } // The site might be broken at the time so logging in using the UI might // not work, so we use the API itself. drupal_rewrite_settings(['settings' => ['update_free_access' => (object) ['value' => TRUE, 'required' => TRUE]]]); $this->drupalGet($this->updateUrl); $this->clickLink(t('Continue')); $this->doSelectionTest(); // Run the update hooks. $this->clickLink(t('Apply pending updates')); // Ensure there are no failed updates. if ($this->checkFailedUpdates) { $this->assertNoRaw('<strong>' . t('Failed:') . '</strong>'); } // The config schema can be incorrect while the update functions are being // executed. But once the update has been completed, it needs to be valid // again. Assert the schema of all configuration objects now. $names = $this->container->get('config.storage')->listAll(); /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */ $typed_config = $this->container->get('config.typed'); $typed_config->clearCachedDefinitions(); foreach ($names as $name) { $config = $this->config($name); $this->assertConfigSchema($typed_config, $name, $config->get()); } // Ensure that the update hooks updated all entity schema. $this->assertFalse(\Drupal::service('entity.definition_update_manager')->needsUpdates(), 'After all updates ran, entity schema is up to date.'); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['#title'] = $this->t('Configure site'); // Warn about settings.php permissions risk $settings_file = $this->sitePath . '/settings.php'; // Check that $_POST is empty so we only show this message when the form is // first displayed, not on the next page after it is submitted. (We do not // want to repeat it multiple times because it is a general warning that is // not related to the rest of the installation process; it would also be // especially out of place on the last page of the installer, where it would // distract from the message that the Drupal installation has completed // successfully.) $post_params = $this->getRequest()->request->all(); if (empty($post_params)) { $original_profile_name = _config_installer_get_original_install_profile(); if ($original_profile_name) { $settings['settings']['install_profile'] = (object) array('value' => $original_profile_name, 'required' => TRUE); drupal_rewrite_settings($settings); } if (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $this->sitePath, FILE_NOT_WRITABLE, 'dir')) { drupal_set_message(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href="@handbook_url">online handbook</a>.', array('%dir' => $this->sitePath, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'warning'); } } $form['#attached']['library'][] = 'system/drupal.system'; $form['admin_account'] = array('#type' => 'fieldgroup', '#title' => $this->t('Site maintenance account')); $form['admin_account']['account']['name'] = array('#type' => 'textfield', '#title' => $this->t('Username'), '#maxlength' => USERNAME_MAX_LENGTH, '#description' => $this->t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'), '#required' => TRUE, '#attributes' => array('class' => array('username'))); $form['admin_account']['account']['pass'] = array('#type' => 'password_confirm', '#required' => TRUE, '#size' => 25); $form['admin_account']['account']['#tree'] = TRUE; $form['admin_account']['account']['mail'] = array('#type' => 'email', '#title' => $this->t('Email address'), '#required' => TRUE); // Use default drush options if available whilst running a site install. if (function_exists('drush_get_option') && function_exists('drush_generate_password')) { $form['admin_account']['account']['name']['#default_value'] = drush_get_option('account-name', 'admin'); $form['admin_account']['account']['pass']['#type'] = 'textfield'; $form['admin_account']['account']['pass']['#default_value'] = drush_get_option('account-pass', drush_generate_password()); $form['admin_account']['account']['mail']['#default_value'] = drush_get_option('account-mail', '*****@*****.**'); } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save and continue'), '#weight' => 15, '#button_type' => 'primary'); return $form; }