diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php index ca85916..3a3cfbe 100644 --- a/core/modules/simpletest/src/BrowserTestBase.php +++ b/core/modules/simpletest/src/BrowserTestBase.php @@ -7,23 +7,24 @@ namespace Drupal\simpletest; +use Behat\Mink\Driver\GoutteDriver; +use Behat\Mink\Element\Element; +use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\Exception; use Behat\Mink\Mink; use Behat\Mink\Session; -use Behat\Mink\Element\Element; -use Behat\Mink\Element\NodeElement; -use Behat\Mink\Driver\GoutteDriver; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Random; +use Drupal\Component\Utility\String; use Drupal\Core\Cache\Cache; use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AnonymousUserSession; -use Drupal\Component\Utility\String; use Drupal\Core\Session\UserSession; use Drupal\Core\Site\Settings; +use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\Test\TestRunnerKernel; use Symfony\Component\HttpFoundation\Request; @@ -46,21 +47,21 @@ * * @var string */ - protected $siteDirectory = NULL; + protected $siteDirectory; /** * The database prefix of this test run. * * @var string */ - protected $databasePrefix = NULL; + protected $databasePrefix; /** * The site directory of the original parent site. * * @var string */ - protected $originalSite; + protected $originalSiteDirectory; /** * Time limit in seconds for the test. @@ -143,7 +144,7 @@ * * @var string */ - protected $sessionName = NULL; + protected $sessionName; /** * The current user logged in using the internal browser. @@ -172,25 +173,13 @@ protected $customTranslations; /** + * Mink session manager. + * * @var Mink */ protected $mink; /** - * Test ID. - * - * @var string - */ - protected $testId; - - /** - * Constructor for \Drupal\simpletest\BrowserTestBase. - */ - public function __construct($test_id = NULL) { - parent::__construct($test_id); - } - - /** * Initializes mink sessions. */ protected function initMink() { @@ -242,7 +231,7 @@ public function setUp() { } // If the passed URL schema is 'https' then setup the $_SERVER variables // properly so that testing will run under HTTPS. - if ($parsed_url['scheme'] == 'https') { + if ($parsed_url['scheme'] === 'https') { $_SERVER['HTTPS'] = 'on'; } $_SERVER['HTTP_HOST'] = $host; @@ -298,6 +287,9 @@ public function setUp() { * Some tests chmod generated files to be read only. During * BrowserTestBase::cleanupEnvironment() and other cleanup operations, * these files need to get deleted too. + * + * @param string $path + * The file path. */ public static function filePreDeleteCallback($path) { chmod($path, 0700); @@ -350,7 +342,7 @@ public function tearDown() { * Returns Mink session. * * @param string|null $name - * Name of the session OR active session will be used. + * (optional) Name of the session OR active session will be used. * * @return \Behat\Mink\Session * The active mink session object. @@ -365,7 +357,7 @@ public function getSession($name = NULL) { * @param string|null $name * (optional) Name of the session. Defaults to the active session. * - * @return \Drupal\Simpletest\WebAssert + * @return \Drupal\simpletest\WebAssert * A new web-assert option for asserting the presence of elements with. */ public function assertSession($name = NULL) { @@ -391,7 +383,7 @@ protected function prepareRequest() { * @param string $path * Drupal path or URL to load into internal browser * @param array $options - * Options to be forwarded to the url generator. + * (optional) Options to be forwarded to the url generator. * * @return string * The retrieved HTML string, also available as $this->getRawContent() @@ -454,10 +446,11 @@ protected function getAbsoluteUrl($path) { * Creates a user with a given set of permissions. * * @param array $permissions - * Array of permission names to assign to user. Note that the user always - * has the default permissions derived from the "authenticated users" role. + * (optional) Array of permission names to assign to user. Note that the + * user always has the default permissions derived from the + * "authenticated users" role. * @param string $name - * The user name. + * (optional) The user name. * * @return \Drupal\user\Entity\User|false * A fully loaded user object with passRaw property, or FALSE if account @@ -569,7 +562,7 @@ protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NUL * \Drupal\simpletest\TestBase::randomString(). * * @param int $length - * Length of random string to generate. + * (optional) Length of random string to generate. * * @return string * Randomly generated unique string. @@ -591,7 +584,7 @@ public function randomMachineName($length = 8) { * introduction of random test failures. * * @param int $length - * Length of random string to generate. + * (optional) Length of random string to generate. * * @return string * Pseudo-randomly generated unique string including special characters. @@ -793,7 +786,8 @@ public function run(\PHPUnit_Framework_TestResult $result = NULL) { * * @return mixed * Either a test result or NULL. - * @throws \Exception When exception was thrown inside the test. + * @throws \PHPUnit_Framework_AssertionFailedError + * When exception was thrown inside the test. */ protected function runTest() { try { @@ -811,7 +805,7 @@ protected function runTest() { * \Drupal\simpletest\TestBase::randomString(). * * @param int $length - * Length of random string to generate. + * (optional) Length of random string to generate. * * @return string * Randomly generated unique string. @@ -873,7 +867,7 @@ public function installDrupal() { ); $this->writeSettings($settings); // Allow for test-specific overrides. - $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php'; + $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSiteDirectory . '/settings.testing.php'; if (file_exists($settings_testing_file)) { // Copy the testing-specific settings.php overrides in place. copy($settings_testing_file, $directory . '/settings.testing.php'); @@ -881,7 +875,7 @@ public function installDrupal() { // testing specific overrides. file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND); } - $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml'; + $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSiteDirectory . '/testing.services.yml'; if (file_exists($settings_services_file)) { // Copy the testing-specific service overrides in place. copy($settings_services_file, $directory . '/services.yml'); @@ -1035,15 +1029,13 @@ protected function installParameters() { * 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 DrupalWebTestCase, 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 + * being performed for the test. It is also used by the cookie value of + * SIMPLETEST_USER_AGENT by the mink browser. During early Drupal bootstrap, + * the cookie is parsed, and if it matches, all database queries use * the database table prefix that has been generated here. * - * @see WebTestBase::curlInitialize() * @see drupal_valid_test_ua() - * @see \Drupal\simpletest\TestBase::prepareEnvironment + * @see BrowserTestBase::prepareEnvironment() */ private function prepareDatabasePrefix() { // Ensure that the generated test site directory does not exist already, @@ -1059,7 +1051,7 @@ private function prepareDatabasePrefix() { /** * Changes the database connection to the prefixed one. * - * @see TestBase::prepareEnvironment() + * @see BrowserTestBase::prepareEnvironment() */ private function changeDatabasePrefix() { if (empty($this->databasePrefix)) { @@ -1082,18 +1074,13 @@ private function changeDatabasePrefix() { /** * Prepares the current environment for running the test. * - * Backups various current environment variables and resets them, so they do - * not interfere with the Drupal site installation in which tests are executed - * and can be restored in TestBase::restoreEnvironment(). - * * Also sets up new resources for the testing environment, such as the public * filesystem and configuration directories. * - * This method is private as it must only be called once by TestBase::run() - * (multiple invocations for the same test would have unpredictable - * consequences) and it must not be callable or overridable by test classes. - * - * @see TestBase::beforePrepareEnvironment() + * This method is private as it must only be called once by + * BrowserTestBase::setUp() (multiple invocations for the same test would have + * unpredictable consequences) and it must not be callable or overridable by + * test classes. */ protected function prepareEnvironment() { // Bootstrap Drupal so we can use Drupal's built in functions. @@ -1105,6 +1092,8 @@ protected function prepareEnvironment() { $kernel->prepareLegacyRequest($request); $this->prepareDatabasePrefix(); + $this->originalSiteDirectory = $kernel->findSitePath($request); + // Create test directory ahead of installation so fatal errors and debug // information can be logged during installation process. file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); @@ -1119,14 +1108,7 @@ protected function prepareEnvironment() { $this->configImporter = NULL; // Unregister all custom stream wrappers of the parent site. - // Availability of Drupal stream wrappers varies by test base class: - // - UnitTestBase operates in a completely empty environment. - // - KernelTestBase supports and maintains stream wrappers in a custom - // way. - // - WebTestBase re-initializes Drupal stream wrappers after installation. - // The original stream wrappers are restored after the test run. - // @see TestBase::restoreEnvironment() - $wrappers = file_get_stream_wrappers(); + $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL); foreach ($wrappers as $scheme => $info) { stream_wrapper_unregister($scheme); } @@ -1153,7 +1135,6 @@ protected function prepareEnvironment() { // After preparing the environment and changing the database prefix, we are // in a valid test environment. drupal_valid_test_ua($this->databasePrefix); - conf_path(FALSE, TRUE); // Reset settings. new Settings(array( @@ -1173,13 +1154,12 @@ protected function prepareEnvironment() { 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', 'test-runner'); } catch (ConnectionNotDefinedException $e) { // Check whether there is a backup of the original default connection. - // @see TestBase::prepareEnvironment() + // @see BrowserTestBase::prepareEnvironment() try { $connection = Database::getConnection('default', 'simpletest_original_default'); }