Commit d2b46426 authored by alexpott's avatar alexpott

Issue #2796105 by larowlan, Sam152, Yogesh Pawar, alexpott, dawehner, anish.a,...

Issue #2796105 by larowlan, Sam152, Yogesh Pawar, alexpott, dawehner, anish.a, Fabianx: Move similar methods in BrowserTestBase / WebTestBase to a trait; untangle installDrupal()
parent 94f0aacf
This diff is collapsed.
<?php
namespace Drupal\Core\Test;
use Drupal\Core\Database\Database;
/**
* Provides a trait for shared test setup functionality.
*/
trait TestSetupTrait {
/**
* An array of config object names that are excluded from schema checking.
*
* @var string[]
*/
protected static $configSchemaCheckerExclusions = array(
// Following are used to test lack of or partial schema. Where partial
// schema is provided, that is explicitly tested in specific tests.
'config_schema_test.noschema',
'config_schema_test.someschema',
'config_schema_test.schema_data_types',
'config_schema_test.no_schema_data_types',
// Used to test application of schema to filtering of configuration.
'config_test.dynamic.system',
);
/**
* The dependency injection container used in the test.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* The site directory of this test run.
*
* @var string
*/
protected $siteDirectory = NULL;
/**
* The public file directory for the test environment.
*
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
*
* @var string
*/
protected $publicFilesDirectory;
/**
* The site directory of the original parent site.
*
* @var string
*/
protected $originalSite;
/**
* The private file directory for the test environment.
*
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
*
* @var string
*/
protected $privateFilesDirectory;
/**
* The original installation profile.
*
* @var string
*/
protected $originalProfile;
/**
* Set to TRUE to strict check all configuration saved.
*
* @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
*
* @var bool
*/
protected $strictConfigSchema = TRUE;
/**
* The DrupalKernel instance used in the test.
*
* @var \Drupal\Core\DrupalKernel
*/
protected $kernel;
/**
* The temporary file directory for the test environment.
*
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
*
* @var string
*/
protected $tempFilesDirectory;
/**
* The test run ID.
*
* @var string
*/
protected $testId;
/**
* Returns the database connection to the site running Simpletest.
*
* @return \Drupal\Core\Database\Connection
* The database connection to use for inserting assertions.
*/
public static function getDatabaseConnection() {
return TestDatabase::getConnection();
}
/**
* Generates a database prefix for running tests.
*
* The database prefix is used by prepareEnvironment() to setup a public files
* directory for the test to be run, which also contains the PHP error log,
* which is written to in case of a fatal error. Since that directory is based
* on the database prefix, all tests (even unit tests) need to have one, in
* order to access and read the error log.
*
* The generated database table prefix is used for the Drupal installation
* being performed for the test. It is also used as user agent HTTP header
* value by the cURL-based browser of WebTestBase, which is sent to the Drupal
* installation of the test. During early Drupal bootstrap, the user agent
* HTTP header is parsed, and if it matches, all database queries use the
* database table prefix that has been generated here.
*
* @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
* @see \Drupal\simpletest\WebTestBase::curlInitialize()
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see drupal_valid_test_ua()
*/
private function prepareDatabasePrefix() {
$test_db = new TestDatabase();
$this->siteDirectory = $test_db->getTestSitePath();
$this->databasePrefix = $test_db->getDatabasePrefix();
}
/**
* Changes the database connection to the prefixed one.
*/
private function changeDatabasePrefix() {
if (empty($this->databasePrefix)) {
$this->prepareDatabasePrefix();
}
// If the test is run with argument dburl then use it.
$db_url = getenv('SIMPLETEST_DB');
if (!empty($db_url)) {
$database = Database::convertDbUrlToConnectionInfo($db_url, DRUPAL_ROOT);
Database::addConnectionInfo('default', 'default', $database);
}
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('default');
if (is_null($connection_info)) {
throw new \InvalidArgumentException('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh.');
}
else {
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
// Replace the full table prefix definition to ensure that no table
// prefixes of the test runner leak into the test.
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
}
}
/**
* Gets the config schema exclusions for this test.
*
* @return string[]
* An array of config object names that are excluded from schema checking.
*/
protected function getConfigSchemaExclusions() {
$class = get_class($this);
$exceptions = [];
while ($class) {
if (property_exists($class, 'configSchemaCheckerExclusions')) {
$exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
}
$class = get_parent_class($class);
}
// Filter out any duplicates.
return array_unique($exceptions);
}
}
......@@ -10,6 +10,7 @@
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\Test\TestDatabase;
use Drupal\Core\Test\TestSetupTrait;
use Drupal\Core\Utility\Error;
use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait;
......@@ -22,6 +23,7 @@
*/
abstract class TestBase {
use TestSetupTrait;
use SessionTestTrait;
use RandomGeneratorTrait;
use AssertHelperTrait;
......@@ -31,20 +33,6 @@
copyConfig as public;
}
/**
* The test run ID.
*
* @var string
*/
protected $testId;
/**
* The site directory of this test run.
*
* @var string
*/
protected $siteDirectory = NULL;
/**
* The database prefix of this test run.
*
......@@ -177,13 +165,6 @@
*/
protected $originalPrefix;
/**
* The original installation profile.
*
* @var string
*/
protected $originalProfile;
/**
* The name of the session cookie of the test-runner.
*
......@@ -205,13 +186,6 @@
*/
protected $originalShutdownCallbacks;
/**
* The site directory of the original parent site.
*
* @var string
*/
protected $originalSite;
/**
* The original user, before testing began.
*
......@@ -219,33 +193,6 @@
*/
protected $originalUser;
/**
* The public file directory for the test environment.
*
* This is set in TestBase::prepareEnvironment().
*
* @var string
*/
protected $publicFilesDirectory;
/**
* The private file directory for the test environment.
*
* This is set in TestBase::prepareEnvironment().
*
* @var string
*/
protected $privateFilesDirectory;
/**
* The temporary file directory for the test environment.
*
* This is set in TestBase::prepareEnvironment().
*
* @var string
*/
protected $tempFilesDirectory;
/**
* The translation file directory for the test environment.
*
......@@ -264,20 +211,6 @@
*/
public $dieOnFail = FALSE;
/**
* The DrupalKernel instance used in the test.
*
* @var \Drupal\Core\DrupalKernel
*/
protected $kernel;
/**
* The dependency injection container used in the test.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* The config importer that can used in a test.
*
......@@ -285,31 +218,6 @@
*/
protected $configImporter;
/**
* Set to TRUE to strict check all configuration saved.
*
* @see \Drupal\Core\Config\Development\ConfigSchemaChecker
*
* @var bool
*/
protected $strictConfigSchema = TRUE;
/**
* An array of config object names that are excluded from schema checking.
*
* @var string[]
*/
protected static $configSchemaCheckerExclusions = array(
// Following are used to test lack of or partial schema. Where partial
// schema is provided, that is explicitly tested in specific tests.
'config_schema_test.noschema',
'config_schema_test.someschema',
'config_schema_test.schema_data_types',
'config_schema_test.no_schema_data_types',
// Used to test application of schema to filtering of configuration.
'config_test.dynamic.system',
);
/**
* HTTP authentication method (specified as a CURLAUTH_* constant).
*
......@@ -505,16 +413,6 @@ public static function deleteAssert($message_id) {
->execute();
}
/**
* Returns the database connection to the site running Simpletest.
*
* @return \Drupal\Core\Database\Connection
* The database connection to use for inserting assertions.
*/
public static function getDatabaseConnection() {
return TestDatabase::getConnection();
}
/**
* Cycles through backtrace until the first non-assertion method is found.
*
......@@ -1124,36 +1022,6 @@ private function prepareDatabasePrefix() {
}
}
/**
* Changes the database connection to the prefixed one.
*
* @see TestBase::prepareEnvironment()
*/
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');
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
// Replace the full table prefix definition to ensure that no table
// prefixes of the test runner leak into the test.
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
}
/**
* Act on global state information before the environment is altered for a test.
*
......@@ -1572,23 +1440,4 @@ public function getTempFilesDirectory() {
return $this->tempFilesDirectory;
}
/**
* Gets the config schema exclusions for this test.
*
* @return string[]
* An array of config object names that are excluded from schema checking.
*/
protected function getConfigSchemaExclusions() {
$class = get_class($this);
$exceptions = [];
while ($class) {
if (property_exists($class, 'configSchemaCheckerExclusions')) {
$exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
}
$class = get_parent_class($class);
}
// Filter out any duplicates.
return array_unique($exceptions);
}
}
This diff is collapsed.
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