Commit 8b41aeee authored by catch's avatar catch

Issue #2170023 by tstoeckler, sun: Use exceptions when something goes wrong in test setup.

parent a3c8c88f
......@@ -72,19 +72,21 @@ function __construct($test_id = NULL) {
}
/**
* Sets up Drupal unit test environment.
*
* @see \DrupalUnitTestBase::$modules
* @see \DrupalUnitTestBase
* Overrides TestBase::beforePrepareEnvironment().
*/
protected function setUp() {
protected function beforePrepareEnvironment() {
// Copy/prime extension file lists once to avoid filesystem scans.
if (!isset($this->moduleFiles)) {
$this->moduleFiles = \Drupal::state()->get('system.module.files') ?: array();
$this->themeFiles = \Drupal::state()->get('system.theme.files') ?: array();
$this->themeData = \Drupal::state()->get('system.theme.data') ?: array();
}
}
/**
* Sets up Drupal unit test environment.
*/
protected function setUp() {
$this->keyValueFactory = new KeyValueMemoryFactory();
parent::setUp();
......
......@@ -46,41 +46,74 @@ function setUp() {
}
// If the test is being run from within simpletest, set up the broken test.
else {
$this->pass(t('The test setUp() method has been run.'));
// Don't call parent::setUp(). This should trigger an error message.
if (file_get_contents($this->originalFileDirectory . '/simpletest/trigger') === 'setup') {
throw new \Exception('Broken setup');
}
$this->pass('The setUp() method has run.');
}
}
function tearDown() {
// If the test is being run from the main site, tear down normally.
if (!drupal_valid_test_ua()) {
unlink($this->originalFileDirectory . '/simpletest/trigger');
parent::tearDown();
}
// If the test is being run from within simpletest, output a message.
else {
// If the test is being run from within simpletest, output a message.
$this->pass(t('The tearDown() method has run.'));
if (file_get_contents($this->originalFileDirectory . '/simpletest/trigger') === 'teardown') {
throw new \Exception('Broken teardown');
}
$this->pass('The tearDown() method has run.');
}
}
/**
* Runs this test case from within the simpletest child site.
*/
function testBreakSetUp() {
function testMethod() {
// If the test is being run from the main site, run it again from the web
// interface within the simpletest child site.
if (!drupal_valid_test_ua()) {
// Verify that a broken setUp() method is caught.
file_put_contents($this->originalFileDirectory . '/simpletest/trigger', 'setup');
$edit['Drupal\simpletest\Tests\BrokenSetUpTest'] = TRUE;
$this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
$this->assertRaw('Broken setup');
$this->assertNoRaw('The setUp() method has run.');
$this->assertNoRaw('Broken test');
$this->assertNoRaw('The test method has run.');
$this->assertNoRaw('Broken teardown');
$this->assertNoRaw('The tearDown() method has run.');
// Verify that a broken tearDown() method is caught.
file_put_contents($this->originalFileDirectory . '/simpletest/trigger', 'teardown');
$edit['Drupal\simpletest\Tests\BrokenSetUpTest'] = TRUE;
$this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
$this->assertNoRaw('Broken setup');
$this->assertRaw('The setUp() method has run.');
$this->assertNoRaw('Broken test');
$this->assertRaw('The test method has run.');
$this->assertRaw('Broken teardown');
$this->assertNoRaw('The tearDown() method has run.');
// Verify that the broken test and its tearDown() method are skipped.
$this->assertRaw(t('The test setUp() method has been run.'));
$this->assertRaw(t('The test cannot be executed because it has not been set up properly.'));
$this->assertNoRaw(t('The test method has run.'));
$this->assertNoRaw(t('The tearDown() method has run.'));
// Verify that a broken test method is caught.
file_put_contents($this->originalFileDirectory . '/simpletest/trigger', 'test');
$edit['Drupal\simpletest\Tests\BrokenSetUpTest'] = TRUE;
$this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
$this->assertNoRaw('Broken setup');
$this->assertRaw('The setUp() method has run.');
$this->assertRaw('Broken test');
$this->assertNoRaw('The test method has run.');
$this->assertNoRaw('Broken teardown');
$this->assertRaw('The tearDown() method has run.');
}
// If the test is being run from within simpletest, output a message.
else {
$this->pass(t('The test method has run.'));
if (file_get_contents($this->originalFileDirectory . '/simpletest/trigger') === 'test') {
throw new \Exception('Broken test');
}
$this->pass('The test method has run.');
}
}
}
......@@ -41,32 +41,6 @@ function __construct($test_id = NULL) {
* setUp() method.
*/
protected function setUp() {
global $conf;
// Create the database prefix for this test.
$this->prepareDatabasePrefix();
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
$this->settingsSet('file_public_path', $this->public_files_directory);
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since \Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
$this->setup = TRUE;
}
}
......@@ -698,27 +698,19 @@ protected function drupalLogout() {
/**
* Sets up a Drupal site for running functional and integration tests.
*
* Generates a random database prefix and installs Drupal with the specified
* installation profile in \Drupal\simpletest\WebTestBase::$profile into the
* prefixed database. Afterwards, installs any additional modules specified by
* the test.
* Installs Drupal with the installation profile specified in
* \Drupal\simpletest\WebTestBase::$profile into the prefixed database.
* Afterwards, installs any additional modules specified in the static
* \Drupal\simpletest\WebTestBase::$modules property of each class in the
* class hierarchy.
*
* After installation all caches are flushed and several configuration values
* are reset to the values of the parent site executing the test, since the
* default values may be incompatible with the environment in which tests are
* being executed.
*
* @param ...
* 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.
*
* @see \Drupal\simpletest\WebTestBase::prepareDatabasePrefix()
* @see \Drupal\simpletest\WebTestBase::changeDatabasePrefix()
* @see \Drupal\simpletest\WebTestBase::prepareEnvironment()
*/
protected function setUp() {
global $conf;
// 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
......@@ -726,33 +718,6 @@ protected function setUp() {
// Backup the currently running Simpletest batch.
$this->originalBatch = batch_get();
// Create the database prefix for this test.
$this->prepareDatabasePrefix();
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since \Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Set the 'simpletest_parent_profile' variable to add the parent profile's
// search path to the child site's search paths.
// @see drupal_system_listing()
$conf['simpletest_parent_profile'] = $this->originalProfile;
// Define information about the user 1 account.
$this->root_user = new UserSession(array(
'uid' => 1,
......@@ -846,14 +811,12 @@ protected function setUp() {
// Use the test mail class instead of the default mail handler class.
\Drupal::config('system.mail')->set('interface.default', 'Drupal\Core\Mail\TestMailCollector')->save();
drupal_set_time_limit($this->timeLimit);
// Temporary fix so that when running from run-tests.sh we don't get an
// empty current path which would indicate we're on the home page.
$path = current_path();
if (empty($path)) {
_current_path('run-tests');
}
$this->setup = TRUE;
}
/**
......
......@@ -15,6 +15,13 @@
*/
class InstallerTranslationTest extends InstallerTest {
/**
* Whether the installer has completed.
*
* @var bool
*/
protected $isInstalled = FALSE;
public static function getInfo() {
return array(
'name' => 'Installer translation test',
......@@ -24,41 +31,8 @@ public static function getInfo() {
}
protected function setUp() {
global $conf;
// 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.
// Back up the currently running SimpleTest batch.
$this->originalBatch = batch_get();
// Add the translations directory so we can retrieve German translations.
$conf['locale.settings']['translation.path'] = drupal_get_path('module', 'simpletest') . '/files/translations';
$conf['language_default']['name'] = 'German';
$conf['language_default']['id'] = 'de';
// Create the database prefix for this test.
$this->prepareDatabasePrefix();
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
$this->isInstalled = FALSE;
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since \Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
$variable_groups = array(
'system.file' => array(
'path.private' => $this->private_files_directory,
......@@ -134,14 +108,14 @@ protected function setUp() {
// Use the test mail class instead of the default mail handler class.
\Drupal::config('system.mail')->set('interface.default', 'Drupal\Core\Mail\TestMailCollector')->save();
drupal_set_time_limit($this->timeLimit);
// When running from run-tests.sh we don't get an empty current path which
// would indicate we're on the home page.
$path = current_path();
if (empty($path)) {
_current_path('run-tests');
}
$this->setup = TRUE;
$this->isInstalled = TRUE;
}
}
......@@ -15,6 +15,13 @@
*/
class InstallerTest extends WebTestBase {
/**
* Whether the installer has completed.
*
* @var bool
*/
protected $isInstalled = FALSE;
public static function getInfo() {
return array(
'name' => 'Installer tests',
......@@ -24,36 +31,8 @@ public static function getInfo() {
}
protected function setUp() {
global $conf;
// 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.
// Back up the currently running SimpleTest batch.
$this->originalBatch = batch_get();
// Create the database prefix for this test.
$this->prepareDatabasePrefix();
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
$this->isInstalled = FALSE;
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since \Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
$variable_groups = array(
'system.file' => array(
'path.private' => $this->private_files_directory,
......@@ -98,25 +77,24 @@ protected function setUp() {
// Use the test mail class instead of the default mail handler class.
\Drupal::config('system.mail')->set('interface.default', 'Drupal\Core\Mail\TestMailCollector')->save();
drupal_set_time_limit($this->timeLimit);
// When running from run-tests.sh we don't get an empty current path which
// would indicate we're on the home page.
$path = current_path();
if (empty($path)) {
_current_path('run-tests');
}
$this->setup = TRUE;
$this->isInstalled = TRUE;
}
/**
* {@inheritdoc}
*
* During setup(), drupalPost calls refreshVariables() which tries to read
* variables which are not yet there because the child Drupal is not yet
* installed.
* WebTestBase::refreshVariables() tries to operate on persistent storage,
* which is only available after the installer completed.
*/
protected function refreshVariables() {
if (!empty($this->setup)) {
if ($this->isInstalled) {
parent::refreshVariables();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment