You are here

protected function DrupalWebTestCase::setUp in SimpleTest 6.2

Same name and namespace in other branches
  1. 7.2 drupal_web_test_case.php \DrupalWebTestCase::setUp()
  2. 7 drupal_web_test_case.php \DrupalWebTestCase::setUp()

Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix. A temporary files directory is created with the same name as the database prefix.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

4 calls to DrupalWebTestCase::setUp()
BlockTestCase::setUp in tests/block.test
Implementation of setUp().
SimpleTestBrowserTestCase::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
SimpleTestFolderTestCase::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
SimpleTestFunctionalTest::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
4 methods override DrupalWebTestCase::setUp()
BlockTestCase::setUp in tests/block.test
Implementation of setUp().
SimpleTestBrowserTestCase::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
SimpleTestFolderTestCase::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
SimpleTestFunctionalTest::setUp in ./simpletest.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…

File

./drupal_web_test_case.php, line 1203

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

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

  // Generate a temporary prefixed database to ensure that tests have a clean starting point.
  $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
  $db_prefix_new = is_array($GLOBALS['db_prefix']) ? $GLOBALS['db_prefix']['default'] : $GLOBALS['db_prefix'] . $this->databasePrefix;
  db_query("UPDATE {simpletest_test_id} SET last_prefix = '%s' WHERE test_id = %d", $db_prefix_new, $this->testId);

  // Store necessary current values before switching to prefixed database.
  $this->originalPrefix = $GLOBALS['db_prefix'];
  $this->originalLanguage = $language;
  $this->originalLanguageDefault = variable_get('language_default', (object) array(
    'language' => 'en',
    'name' => 'English',
    'native' => 'English',
    'direction' => 0,
    'enabled' => 1,
    'plurals' => 0,
    'formula' => '',
    'domain' => '',
    'prefix' => '',
    'weight' => 0,
    'javascript' => '',
  ));
  $this->originalFileDirectory = variable_get('file_directory_path', conf_path() . '/files');
  $this->originalProfile = variable_get('install_profile', 'default');
  $clean_url_original = variable_get('clean_url', 0);

  // Must reset locale here, since schema calls t(). (Drupal 6)
  if (module_exists('locale')) {
    $language = (object) array(
      'language' => 'en',
      'name' => 'English',
      'native' => 'English',
      'direction' => 0,
      'enabled' => 1,
      'plurals' => 0,
      'formula' => '',
      'domain' => '',
      'prefix' => '',
      'weight' => 0,
      'javascript' => '',
    );
    locale(NULL, NULL, TRUE);
  }

  // Workaround to insure we init the theme layer before going into prefixed
  // environment. (Drupal 6)
  $this
    ->pass(t('Starting run with db_prefix %prefix', array(
    '%prefix' => $db_prefix_new,
  )), 'System');

  // Clone the current connection and replace the current prefix.
  $GLOBALS['db_prefix'] = $db_prefix_new;

  // Create test directory ahead of installation so fatal errors and debug
  // information can be logged during installation process.
  // Use temporary files directory with the same prefix as the database.
  $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
  $temp_files_directory = $public_files_directory . '/tmp';

  // Create the directories
  file_check_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  file_check_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();

  // Set the test information for use in other parts of Drupal.
  $test_info =& $GLOBALS['drupal_test_info'];
  $test_info['test_run_id'] = $this->databasePrefix;
  $test_info['in_child_site'] = FALSE;
  include_once './includes/install.inc';
  drupal_install_system();

  // Set path variables.
  variable_set('file_directory_path', $public_files_directory);
  variable_set('file_directory_temp', $temp_files_directory);

  // Include the testing profile.
  variable_set('install_profile', $this->profile);
  $profile_modules = drupal_verify_profile($this->profile, 'en');

  // Install the modules specified by the testing profile.
  drupal_install_modules($profile_modules);

  // Install modules needed for this test. This could have been passed in as
  // either a single array argument or a variable number of string arguments.
  // @todo Remove this compatibility layer in Drupal 8, and only accept
  // $modules as a single array argument.
  $modules = func_get_args();
  if (isset($modules[0]) && is_array($modules[0])) {
    $modules = $modules[0];
  }
  if ($modules) {
    drupal_install_modules($modules);
  }

  // Run the profile tasks.
  $task = 'profile';
  $function = $this->profile . '_profile_tasks';
  if (function_exists($function)) {
    $function($task, '');
  }

  // Reset/rebuild all data structures after enabling the modules.
  $this
    ->resetAll();

  // Run cron once in that environment, as install.php does at the end of
  // the installation process.
  drupal_cron_run();

  // Log in with a clean $user.
  $this->originalUser = $user;
  session_save_session(FALSE);
  $user = user_load(array(
    'uid' => 1,
  ));

  // Restore necessary variables.
  variable_set('install_task', 'done');
  variable_set('clean_url', $clean_url_original);
  variable_set('site_mail', 'simpletest@example.com');

  // Set up English language.
  unset($GLOBALS['conf']['language_default']);
  $language = language_default();

  // Use the test mail class instead of the default mail handler class.
  variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/simpletest.mail.inc');
  set_time_limit($this->timeLimit);
}