Commit 682c5504 authored by webchick's avatar webchick

Issue #1808220 by sun: Remove run-tests.sh dependency on existing/installed parent site.

parent e1db0da4
......@@ -13,6 +13,14 @@
* Tests _contextual_links_to_id() & _contextual_id_to_links().
*/
class ContextualUnitTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('contextual');
public static function getInfo() {
return array(
'name' => 'Conversion to and from "contextual id"s (for placeholders)',
......
......@@ -371,13 +371,24 @@ public static function deleteAssert($message_id) {
* The database connection to use for inserting assertions.
*/
public static function getDatabaseConnection() {
// Check whether there is a test runner connection.
// @see run-tests.sh
// @todo Convert Simpletest UI runner to create + use this connection, too.
try {
$connection = Database::getConnection('default', 'simpletest_original_default');
$connection = Database::getConnection('default', 'test-runner');
}
catch (ConnectionNotDefinedException $e) {
// If the test was not set up, the simpletest_original_default
// connection does not exist.
$connection = Database::getConnection('default', 'default');
// Check whether there is a backup of the original default connection.
// @see TestBase::prepareEnvironment()
try {
$connection = Database::getConnection('default', 'simpletest_original_default');
}
catch (ConnectionNotDefinedException $e) {
// If TestBase::prepareEnvironment() or TestBase::restoreEnvironment()
// failed, the test-specific database connection does not exist
// yet/anymore, so fall back to the default of the (UI) test runner.
$connection = Database::getConnection('default', 'default');
}
}
return $connection;
}
......@@ -918,7 +929,7 @@ private function prepareDatabasePrefix() {
// As soon as the database prefix is set, the test might start to execute.
// All assertions as well as the SimpleTest batch operations are associated
// with the testId, so the database prefix has to be associated with it.
$affected_rows = db_update('simpletest_test_id')
$affected_rows = self::getDatabaseConnection()->update('simpletest_test_id')
->fields(array('last_prefix' => $this->databasePrefix))
->condition('test_id', $this->testId)
->execute();
......@@ -936,6 +947,13 @@ private function changeDatabasePrefix() {
if (empty($this->databasePrefix)) {
$this->prepareDatabasePrefix();
}
// If the backup already exists, something went terribly wrong.
// This case is possible, because database connection info is a static
// global state construct on the Database class, which at least persists
// for all test methods executed in one PHP process.
if (Database::getConnectionInfo('simpletest_original_default')) {
throw new \RuntimeException("Bad Database connection state: 'simpletest_original_default' connection key already exists. Broken test?");
}
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('default');
......@@ -992,6 +1010,9 @@ private function prepareEnvironment() {
$this->originalSite = conf_path();
$this->originalSettings = settings()->getAll();
$this->originalConfig = $GLOBALS['config'];
// @todo Remove all remnants of $GLOBALS['conf'].
// @see https://drupal.org/node/2183323
$this->originalConf = isset($GLOBALS['conf']) ? $GLOBALS['conf'] : NULL;
// Backup statics and globals.
$this->originalContainer = clone \Drupal::getContainer();
......@@ -1075,6 +1096,8 @@ private function prepareEnvironment() {
// Unset globals.
unset($GLOBALS['config_directories']);
unset($GLOBALS['config']);
unset($GLOBALS['conf']);
unset($GLOBALS['theme_key']);
unset($GLOBALS['theme']);
......@@ -1085,9 +1108,6 @@ private function prepareEnvironment() {
// Change the database prefix.
$this->changeDatabasePrefix();
// Remove all configuration overrides.
$GLOBALS['config'] = array();
// After preparing the environment and changing the database prefix, we are
// in a valid test environment.
drupal_valid_test_ua($this->databasePrefix);
......@@ -1214,6 +1234,7 @@ private function restoreEnvironment() {
// Restore original in-memory configuration.
$GLOBALS['config'] = $this->originalConfig;
$GLOBALS['conf'] = $this->originalConf;
new Settings($this->originalSettings);
// Restore original statics and globals.
......
......@@ -233,7 +233,7 @@ function confirmStubTestResults() {
$this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertEqual('6 passes, 5 fails, 2 exceptions, 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct');
$this->assertEqual('6 passes, 5 fails, 2 exceptions, 1 debug message', $this->childTestResults['summary']);
$this->test_ids[] = $test_id = $this->getTestIdFromResults();
$this->assertTrue($test_id, 'Found test ID in results.');
......
......@@ -361,8 +361,16 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
* that ran.
*/
function simpletest_last_test_get($test_id) {
$last_prefix = db_query_range('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array(':test_id' => $test_id))->fetchField();
$last_test_class = db_query_range('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, array(':test_id' => $test_id))->fetchField();
$last_prefix = TestBase::getDatabaseConnection()
->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array(
':test_id' => $test_id,
))
->fetchField();
$last_test_class = TestBase::getDatabaseConnection()
->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, array(
':test_id' => $test_id,
))
->fetchField();
return array($last_prefix, $last_test_class);
}
......@@ -696,22 +704,23 @@ function simpletest_clean_temporary_directories() {
*/
function simpletest_clean_results_table($test_id = NULL) {
if (\Drupal::config('simpletest.settings')->get('clear_results')) {
$connection = TestBase::getDatabaseConnection();
if ($test_id) {
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
$count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
db_delete('simpletest')
$connection->delete('simpletest')
->condition('test_id', $test_id)
->execute();
db_delete('simpletest_test_id')
$connection->delete('simpletest_test_id')
->condition('test_id', $test_id)
->execute();
}
else {
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
$count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
// Clear test results.
db_delete('simpletest')->execute();
db_delete('simpletest_test_id')->execute();
$connection->delete('simpletest')->execute();
$connection->delete('simpletest_test_id')->execute();
}
return $count;
......
This diff is collapsed.
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