UpgradePathTestCase::setUp

  1. drupal
    1. 8
    2. 7
Versions
7 protected UpgradePathTestCase::setUp()

Override of DrupalWebTestCase::setUp() specialized for upgrade testing.

Code

modules/simpletest/tests/upgrade/upgrade.test, line 33

<?php
protected function setUp() {
  global $user, $language, $conf;

  // Load the Update API.
  require_once DRUPAL_ROOT . '/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;
  $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.
  foreach ($this->databaseDumpFiles as $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));
  $session_name = update_get_d6_session_name();
  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);
  // A session cannot be written without the ssid column which is missing on
  // Drupal 6 sites.
  db_add_field('sessions', 'ssid', array('description' => "Secure session ID. The value is generated by Drupal's session handlers.", 'type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
  _drupal_session_write($sid, '');
  // Remove the temporarily added ssid column.
  db_drop_field('sessions', 'ssid');
  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);
}
?>