use Behat\Mink\Mink;
use Behat\Mink\Selector\SelectorsHandler;
use Behat\Mink\Session;
-use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
-use Drupal\Component\Utility\Html;
-use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Database\Database;
-use Drupal\Core\Session\AccountInterface;
-use Drupal\Core\Session\AnonymousUserSession;
-use Drupal\Core\Site\Settings;
-use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\Test\FunctionalTestSetupTrait;
-use Drupal\Core\Test\TestRunnerKernel;
use Drupal\Core\Test\TestSetupTrait;
-use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use Drupal\FunctionalTests\AssertLegacyTrait;
-use Drupal\simpletest\AssertHelperTrait;
-use Drupal\simpletest\ContentTypeCreationTrait;
-use Drupal\simpletest\BlockCreationTrait;
-use Drupal\simpletest\NodeCreationTrait;
-use Drupal\simpletest\UserCreationTrait;
-use Symfony\Component\CssSelector\CssSelectorConverter;
-use Symfony\Component\HttpFoundation\Request;
+use Drupal\Tests\block\Traits\BlockCreationTrait;
+use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
+use Drupal\Tests\node\Traits\NodeCreationTrait;
+use Drupal\Tests\user\Traits\UserCreationTrait;
+use GuzzleHttp\Cookie\CookieJar;
+use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
+use Symfony\Component\CssSelector\CssSelectorConverter;
/**
* Provides a test case for functional Drupal tests.
* Drupal\Tests\yourmodule\Functional namespace and live in the
* modules/yourmodule/tests/src/Functional directory.
*
+ * Tests extending this base class should only translate text when testing
+ * translation functionality. For example, avoid wrapping test text with t()
+ * or TranslatableMarkup().
+ *
* @ingroup testing
*/
-abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
+abstract class BrowserTestBase extends TestCase {
use FunctionalTestSetupTrait;
+ use UiHelperTrait {
+ FunctionalTestSetupTrait::refreshVariables insteadof UiHelperTrait;
+ }
use TestSetupTrait;
- use AssertHelperTrait;
use BlockCreationTrait {
placeBlock as drupalPlaceBlock;
}
use AssertLegacyTrait;
use RandomGeneratorTrait;
- use SessionTestTrait;
use NodeCreationTrait {
getNodeByTitle as drupalGetNodeByTitle;
createNode as drupalCreateNode;
createContentType as drupalCreateContentType;
}
use ConfigTestTrait;
+ use TestRequirementsTrait;
use UserCreationTrait {
createRole as drupalCreateRole;
createUser as drupalCreateUser;
}
use XdebugRequestTrait;
+ use PhpunitCompatibilityTrait;
/**
* The database prefix of this test run.
*/
protected $profile = 'testing';
- /**
- * The current user logged in using the Mink controlled browser.
- *
- * @var \Drupal\user\UserInterface
- */
- protected $loggedInUser = FALSE;
-
/**
* An array of custom translations suitable for drupal_rewrite_settings().
*
/*
* Mink class for the default driver to use.
*
- * Shoud be a fully qualified class name that implements
+ * Should be a fully-qualified class name that implements
* Behat\Mink\Driver\DriverInterface.
*
* Value can be overridden using the environment variable MINK_DRIVER_CLASS.
*
- * @var string.
+ * @var string
*/
protected $minkDefaultDriverClass = GoutteDriver::class;
protected $preserveGlobalState = FALSE;
/**
- * Class name for HTML output logging.
- *
- * @var string
- */
- protected $htmlOutputClassName;
-
- /**
- * Directory name for HTML output logging.
- *
- * @var string
- */
- protected $htmlOutputDirectory;
-
- /**
- * Counter storage for HTML output logging.
+ * The base URL.
*
* @var string
*/
- protected $htmlOutputCounterStorage;
-
- /**
- * Counter for HTML output logging.
- *
- * @var int
- */
- protected $htmlOutputCounter = 1;
+ protected $baseUrl;
/**
- * HTML output output enabled.
+ * The original array of shutdown function callbacks.
*
- * @var bool
+ * @var array
*/
- protected $htmlOutputEnabled = FALSE;
+ protected $originalShutdownCallbacks = [];
/**
- * The file name to write the list of URLs to.
- *
- * This file is read by the PHPUnit result printer.
+ * The app root.
*
* @var string
- *
- * @see \Drupal\Tests\Listeners\HtmlOutputPrinter
- */
- protected $htmlOutputFile;
-
- /**
- * HTML output test ID.
- *
- * @var int
*/
- protected $htmlOutputTestId;
+ protected $root;
/**
- * The base URL.
+ * The original container.
*
- * @var string
- */
- protected $baseUrl;
-
- /**
- * The original array of shutdown function callbacks.
+ * Move this to \Drupal\Core\Test\FunctionalTestSetupTrait once TestBase no
+ * longer provides the same value.
*
- * @var array
+ * @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
- protected $originalShutdownCallbacks = [];
+ protected $originalContainer;
/**
- * The number of meta refresh redirects to follow, or NULL if unlimited.
- *
- * @var null|int
+ * {@inheritdoc}
*/
- protected $maximumMetaRefreshCount = NULL;
+ public function __construct($name = NULL, array $data = [], $dataName = '') {
+ parent::__construct($name, $data, $dataName);
- /**
- * The number of meta refresh redirects followed during ::drupalGet().
- *
- * @var int
- */
- protected $metaRefreshCount = 0;
+ $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
+ }
/**
* Initializes Mink sessions.
}
$selectors_handler = new SelectorsHandler([
- 'hidden_field_selector' => new HiddenFieldSelector()
+ 'hidden_field_selector' => new HiddenFieldSelector(),
]);
$session = new Session($driver, $selectors_handler);
+ $cookies = $this->extractCookiesFromRequest(\Drupal::request());
+ foreach ($cookies as $cookie_name => $values) {
+ foreach ($values as $value) {
+ $session->setCookie($cookie_name, $value);
+ }
+ }
$this->mink = new Mink();
$this->mink->registerSession('default', $session);
$this->mink->setDefaultSessionName('default');
$this->registerSessions();
- // According to the W3C WebDriver specification a cookie can only be set if
- // the cookie domain is equal to the domain of the active document. When the
- // browser starts up the active document is not our domain but 'about:blank'
- // or similar. To be able to set our User-Agent and Xdebug cookies at the
- // start of the test we now do a request to the front page so the active
- // document matches the domain.
- // @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie
- // @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975
- $session = $this->getSession();
- $session->visit($this->baseUrl);
+ $this->initFrontPage();
return $session;
}
+ /**
+ * Visits the front page when initializing Mink.
+ *
+ * According to the W3C WebDriver specification a cookie can only be set if
+ * the cookie domain is equal to the domain of the active document. When the
+ * browser starts up the active document is not our domain but 'about:blank'
+ * or similar. To be able to set our User-Agent and Xdebug cookies at the
+ * start of the test we now do a request to the front page so the active
+ * document matches the domain.
+ *
+ * @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie
+ * @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975
+ */
+ protected function initFrontPage() {
+ $session = $this->getSession();
+ $session->visit($this->baseUrl);
+ }
+
/**
* Gets an instance of the default Mink driver.
*
* When provided default Mink driver class can't be instantiated.
*/
protected function getDefaultDriverInstance() {
- // Get default driver params from environment if availables.
- if ($arg_json = getenv('MINK_DRIVER_ARGS')) {
- $this->minkDefaultDriverArgs = json_decode($arg_json);
+ // Get default driver params from environment if available.
+ if ($arg_json = $this->getMinkDriverArgs()) {
+ $this->minkDefaultDriverArgs = json_decode($arg_json, TRUE);
}
- // Get and check default driver class from environment if availables.
+ // Get and check default driver class from environment if available.
if ($minkDriverClass = getenv('MINK_DRIVER_CLASS')) {
if (class_exists($minkDriverClass)) {
$this->minkDefaultDriverClass = $minkDriverClass;
return $driver;
}
+ /**
+ * Get the Mink driver args from an environment variable, if it is set. Can
+ * be overridden in a derived class so it is possible to use a different
+ * value for a subset of tests, e.g. the JavaScript tests.
+ *
+ * @return string|false
+ * The JSON-encoded argument string. False if it is not set.
+ */
+ protected function getMinkDriverArgs() {
+ return getenv('MINK_DRIVER_ARGS');
+ }
+
/**
* Provides a Guzzle middleware handler to log every response received.
*
* {@inheritdoc}
*/
protected function setUp() {
- global $base_url;
- parent::setUp();
-
- // Get and set the domain of the environment we are running our test
- // coverage against.
- $base_url = getenv('SIMPLETEST_BASE_URL');
- if (!$base_url) {
- throw new \Exception(
- 'You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.'
- );
+ // Installing Drupal creates 1000s of objects. Garbage collection of these
+ // objects is expensive. This appears to be causing random segmentation
+ // faults in PHP 5.x due to https://bugs.php.net/bug.php?id=72286. Once
+ // Drupal is installed is rebuilt, garbage collection is re-enabled.
+ $disable_gc = version_compare(PHP_VERSION, '7', '<') && gc_enabled();
+ if ($disable_gc) {
+ gc_collect_cycles();
+ gc_disable();
}
+ parent::setUp();
- // Setup $_SERVER variable.
- $parsed_url = parse_url($base_url);
- $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
- $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
- $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;
-
- $this->baseUrl = $base_url;
-
- // 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') {
- $_SERVER['HTTPS'] = 'on';
- }
- $_SERVER['HTTP_HOST'] = $host;
- $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
- $_SERVER['SERVER_ADDR'] = '127.0.0.1';
- $_SERVER['SERVER_PORT'] = $port;
- $_SERVER['SERVER_SOFTWARE'] = NULL;
- $_SERVER['SERVER_NAME'] = 'localhost';
- $_SERVER['REQUEST_URI'] = $path . '/';
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
- $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
- $_SERVER['PHP_SELF'] = $path . '/index.php';
- $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
+ $this->setupBaseUrl();
// Install Drupal test site.
$this->prepareEnvironment();
$this->installDrupal();
// Setup Mink.
- $session = $this->initMink();
+ $this->initMink();
- $cookies = $this->extractCookiesFromRequest(\Drupal::request());
- foreach ($cookies as $cookie_name => $values) {
- foreach ($values as $value) {
- $session->setCookie($cookie_name, $value);
- }
+ // Set up the browser test output file.
+ $this->initBrowserOutputFile();
+ // If garbage collection was disabled prior to rebuilding container,
+ // re-enable it.
+ if ($disable_gc) {
+ gc_enable();
}
- // Creates the directory to store browser output in if a file to write
- // URLs to has been created by \Drupal\Tests\Listeners\HtmlOutputPrinter.
- $browser_output_file = getenv('BROWSERTEST_OUTPUT_FILE');
- $this->htmlOutputEnabled = is_file($browser_output_file);
- if ($this->htmlOutputEnabled) {
- $this->htmlOutputFile = $browser_output_file;
- $this->htmlOutputClassName = str_replace("\\", "_", get_called_class());
- $this->htmlOutputDirectory = DRUPAL_ROOT . '/sites/simpletest/browser_output';
- if (file_prepare_directory($this->htmlOutputDirectory, FILE_CREATE_DIRECTORY) && !file_exists($this->htmlOutputDirectory . '/.htaccess')) {
- file_put_contents($this->htmlOutputDirectory . '/.htaccess', "<IfModule mod_expires.c>\nExpiresActive Off\n</IfModule>\n");
- }
- $this->htmlOutputCounterStorage = $this->htmlOutputDirectory . '/' . $this->htmlOutputClassName . '.counter';
- $this->htmlOutputTestId = str_replace('sites/simpletest/', '', $this->siteDirectory);
- if (is_file($this->htmlOutputCounterStorage)) {
- $this->htmlOutputCounter = max(1, (int) file_get_contents($this->htmlOutputCounterStorage)) + 1;
- }
- }
+ // Ensure that the test is not marked as risky because of no assertions. In
+ // PHPUnit 6 tests that only make assertions using $this->assertSession()
+ // can be marked as risky.
+ $this->addToAssertionCount(1);
}
/**
}
/**
- * Returns WebAssert object.
- *
- * @param string $name
- * (optional) Name of the session. Defaults to the active session.
+ * Get session cookies from current session.
*
- * @return \Drupal\Tests\WebAssert
- * A new web-assert option for asserting the presence of elements with.
+ * @return \GuzzleHttp\Cookie\CookieJar
+ * A cookie jar with the current session.
*/
- public function assertSession($name = NULL) {
- return new WebAssert($this->getSession($name), $this->baseUrl);
- }
+ protected function getSessionCookies() {
+ $domain = parse_url($this->getUrl(), PHP_URL_HOST);
+ $session_id = $this->getSession()->getCookie($this->getSessionName());
+ $cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain);
- /**
- * Prepare for a request to testing site.
- *
- * The testing site is protected via a SIMPLETEST_USER_AGENT cookie that is
- * checked by drupal_valid_test_ua().
- *
- * @see drupal_valid_test_ua()
- */
- protected function prepareRequest() {
- $session = $this->getSession();
- $session->setCookie('SIMPLETEST_USER_AGENT', drupal_generate_test_ua($this->databasePrefix));
+ return $cookies;
}
/**
- * Builds an a absolute URL from a system path or a URL object.
+ * Obtain the HTTP client for the system under test.
*
- * @param string|\Drupal\Core\Url $path
- * A system path or a URL.
- * @param array $options
- * Options to be passed to Url::fromUri().
+ * Use this method for arbitrary HTTP requests to the site under test. For
+ * most tests, you should not get the HTTP client and instead use navigation
+ * methods such as drupalGet() and clickLink() in order to benefit from
+ * assertions.
*
- * @return string
- * An absolute URL stsring.
- */
- protected function buildUrl($path, array $options = []) {
- if ($path instanceof Url) {
- $url_options = $path->getOptions();
- $options = $url_options + $options;
- $path->setOptions($options);
- return $path->setAbsolute()->toString();
- }
- // The URL generator service is not necessarily available yet; e.g., in
- // interactive installer tests.
- elseif ($this->container->has('url_generator')) {
- $force_internal = isset($options['external']) && $options['external'] == FALSE;
- if (!$force_internal && UrlHelper::isExternal($path)) {
- return Url::fromUri($path, $options)->toString();
- }
- else {
- $uri = $path === '<front>' ? 'base:/' : 'base:/' . $path;
- // Path processing is needed for language prefixing. Skip it when a
- // path that may look like an external URL is being used as internal.
- $options['path_processing'] = !$force_internal;
- return Url::fromUri($uri, $options)
- ->setAbsolute()
- ->toString();
- }
- }
- else {
- return $this->getAbsoluteUrl($path);
- }
- }
-
- /**
- * Retrieves a Drupal path or an absolute path.
- *
- * @param string|\Drupal\Core\Url $path
- * Drupal path or URL to load into Mink controlled browser.
- * @param array $options
- * (optional) Options to be forwarded to the url generator.
- * @param string[] $headers
- * An array containing additional HTTP request headers, the array keys are
- * the header names and the array values the header values. This is useful
- * to set for example the "Accept-Language" header for requesting the page
- * in a different language. Note that not all headers are supported, for
- * example the "Accept" header is always overridden by the browser. For
- * testing REST APIs it is recommended to directly use an HTTP client such
- * as Guzzle instead.
+ * Subclasses which substitute a different Mink driver should override this
+ * method and provide a Guzzle client if the Mink driver provides one.
*
- * @return string
- * The retrieved HTML string, also available as $this->getRawContent()
- */
- protected function drupalGet($path, array $options = [], array $headers = []) {
- $options['absolute'] = TRUE;
- $url = $this->buildUrl($path, $options);
-
- $session = $this->getSession();
-
- $this->prepareRequest();
- foreach ($headers as $header_name => $header_value) {
- $session->setRequestHeader($header_name, $header_value);
- }
-
- $session->visit($url);
- $out = $session->getPage()->getContent();
-
- // Ensure that any changes to variables in the other thread are picked up.
- $this->refreshVariables();
-
- // Replace original page output with new output from redirected page(s).
- if ($new = $this->checkForMetaRefresh()) {
- $out = $new;
- // We are finished with all meta refresh redirects, so reset the counter.
- $this->metaRefreshCount = 0;
- }
-
- // Log only for JavascriptTestBase tests because for Goutte we log with
- // ::getResponseLogHandler.
- if ($this->htmlOutputEnabled && !($this->getSession()->getDriver() instanceof GoutteDriver)) {
- $html_output = 'GET request to: ' . $url .
- '<hr />Ending URL: ' . $this->getSession()->getCurrentUrl();
- $html_output .= '<hr />' . $out;
- $html_output .= $this->getHtmlOutputHeaders();
- $this->htmlOutput($html_output);
- }
-
- return $out;
- }
-
- /**
- * Takes a path and returns an absolute path.
- *
- * @param string $path
- * A path from the Mink controlled browser content.
+ * @return \GuzzleHttp\ClientInterface
+ * The client with BrowserTestBase configuration.
*
- * @return string
- * The $path with $base_url prepended, if necessary.
+ * @throws \RuntimeException
+ * If the Mink driver does not support a Guzzle HTTP client, throw an
+ * exception.
*/
- protected function getAbsoluteUrl($path) {
- global $base_url, $base_path;
-
- $parts = parse_url($path);
- if (empty($parts['host'])) {
- // Ensure that we have a string (and no xpath object).
- $path = (string) $path;
- // Strip $base_path, if existent.
- $length = strlen($base_path);
- if (substr($path, 0, $length) === $base_path) {
- $path = substr($path, $length);
- }
- // Ensure that we have an absolute path.
- if (empty($path) || $path[0] !== '/') {
- $path = '/' . $path;
- }
- // Finally, prepend the $base_url.
- $path = $base_url . $path;
+ protected function getHttpClient() {
+ /* @var $mink_driver \Behat\Mink\Driver\DriverInterface */
+ $mink_driver = $this->getSession()->getDriver();
+ if ($mink_driver instanceof GoutteDriver) {
+ return $mink_driver->getClient()->getClient();
}
- return $path;
- }
-
- /**
- * Logs in a user using the Mink controlled browser.
- *
- * If a user is already logged in, then the current user is logged out before
- * logging in the specified user.
- *
- * Please note that neither the current user nor the passed-in user object is
- * populated with data of the logged in user. If you need full access to the
- * user object after logging in, it must be updated manually. If you also need
- * access to the plain-text password of the user (set by drupalCreateUser()),
- * e.g. to log in the same user again, then it must be re-assigned manually.
- * For example:
- * @code
- * // Create a user.
- * $account = $this->drupalCreateUser(array());
- * $this->drupalLogin($account);
- * // Load real user object.
- * $pass_raw = $account->passRaw;
- * $account = User::load($account->id());
- * $account->passRaw = $pass_raw;
- * @endcode
- *
- * @param \Drupal\Core\Session\AccountInterface $account
- * User object representing the user to log in.
- *
- * @see drupalCreateUser()
- */
- protected function drupalLogin(AccountInterface $account) {
- if ($this->loggedInUser) {
- $this->drupalLogout();
- }
-
- $this->drupalGet('user/login');
- $this->assertSession()->statusCodeEquals(200);
- $this->submitForm([
- 'name' => $account->getUsername(),
- 'pass' => $account->passRaw,
- ], t('Log in'));
-
- // @see BrowserTestBase::drupalUserIsLoggedIn()
- $account->sessionId = $this->getSession()->getCookie($this->getSessionName());
- $this->assertTrue($this->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', ['%name' => $account->getAccountName()]));
-
- $this->loggedInUser = $account;
- $this->container->get('current_user')->setAccount($account);
- }
-
- /**
- * Logs a user out of the Mink controlled browser and confirms.
- *
- * Confirms logout by checking the login page.
- */
- protected function drupalLogout() {
- // Make a request to the logout page, and redirect to the user page, the
- // idea being if you were properly logged out you should be seeing a login
- // screen.
- $assert_session = $this->assertSession();
- $this->drupalGet('user/logout', ['query' => ['destination' => 'user']]);
- $assert_session->statusCodeEquals(200);
- $assert_session->fieldExists('name');
- $assert_session->fieldExists('pass');
-
- // @see BrowserTestBase::drupalUserIsLoggedIn()
- unset($this->loggedInUser->sessionId);
- $this->loggedInUser = FALSE;
- $this->container->get('current_user')->setAccount(new AnonymousUserSession());
- }
-
- /**
- * Fills and submits a form.
- *
- * @param array $edit
- * Field data in an associative array. Changes the current input fields
- * (where possible) to the values indicated.
- *
- * A checkbox can be set to TRUE to be checked and should be set to FALSE to
- * be unchecked.
- * @param string $submit
- * Value of the submit button whose click is to be emulated. For example,
- * t('Save'). The processing of the request depends on this value. For
- * example, a form may have one button with the value t('Save') and another
- * button with the value t('Delete'), and execute different code depending
- * on which one is clicked.
- * @param string $form_html_id
- * (optional) HTML ID of the form to be submitted. On some pages
- * there are many identical forms, so just using the value of the submit
- * button is not enough. For example: 'trigger-node-presave-assign-form'.
- * Note that this is not the Drupal $form_id, but rather the HTML ID of the
- * form, which is typically the same thing but with hyphens replacing the
- * underscores.
- */
- protected function submitForm(array $edit, $submit, $form_html_id = NULL) {
- $assert_session = $this->assertSession();
-
- // Get the form.
- if (isset($form_html_id)) {
- $form = $assert_session->elementExists('xpath', "//form[@id='$form_html_id']");
- $submit_button = $assert_session->buttonExists($submit, $form);
- $action = $form->getAttribute('action');
- }
- else {
- $submit_button = $assert_session->buttonExists($submit);
- $form = $assert_session->elementExists('xpath', './ancestor::form', $submit_button);
- $action = $form->getAttribute('action');
- }
-
- // Edit the form values.
- foreach ($edit as $name => $value) {
- $field = $assert_session->fieldExists($name, $form);
-
- // Provide support for the values '1' and '0' for checkboxes instead of
- // TRUE and FALSE.
- // @todo Get rid of supporting 1/0 by converting all tests cases using
- // this to boolean values.
- $field_type = $field->getAttribute('type');
- if ($field_type === 'checkbox') {
- $value = (bool) $value;
- }
-
- $field->setValue($value);
- }
-
- // Submit form.
- $this->prepareRequest();
- $submit_button->press();
-
- // Ensure that any changes to variables in the other thread are picked up.
- $this->refreshVariables();
-
- // Check if there are any meta refresh redirects (like Batch API pages).
- if ($this->checkForMetaRefresh()) {
- // We are finished with all meta refresh redirects, so reset the counter.
- $this->metaRefreshCount = 0;
- }
-
- // Log only for JavascriptTestBase tests because for Goutte we log with
- // ::getResponseLogHandler.
- if ($this->htmlOutputEnabled && !($this->getSession()->getDriver() instanceof GoutteDriver)) {
- $out = $this->getSession()->getPage()->getContent();
- $html_output = 'POST request to: ' . $action .
- '<hr />Ending URL: ' . $this->getSession()->getCurrentUrl();
- $html_output .= '<hr />' . $out;
- $html_output .= $this->getHtmlOutputHeaders();
- $this->htmlOutput($html_output);
- }
-
- }
-
- /**
- * Executes a form submission.
- *
- * It will be done as usual POST request with Mink.
- *
- * @param \Drupal\Core\Url|string $path
- * Location of the post form. Either a Drupal path or an absolute path or
- * NULL to post to the current page. For multi-stage forms you can set the
- * path to NULL and have it post to the last received page. Example:
- *
- * @code
- * // First step in form.
- * $edit = array(...);
- * $this->drupalPostForm('some_url', $edit, t('Save'));
- *
- * // Second step in form.
- * $edit = array(...);
- * $this->drupalPostForm(NULL, $edit, t('Save'));
- * @endcode
- * @param array $edit
- * Field data in an associative array. Changes the current input fields
- * (where possible) to the values indicated.
- *
- * When working with form tests, the keys for an $edit element should match
- * the 'name' parameter of the HTML of the form. For example, the 'body'
- * field for a node has the following HTML:
- * @code
- * <textarea id="edit-body-und-0-value" class="text-full form-textarea
- * resize-vertical" placeholder="" cols="60" rows="9"
- * name="body[0][value]"></textarea>
- * @endcode
- * When testing this field using an $edit parameter, the code becomes:
- * @code
- * $edit["body[0][value]"] = 'My test value';
- * @endcode
- *
- * A checkbox can be set to TRUE to be checked and should be set to FALSE to
- * be unchecked. Multiple select fields can be tested using 'name[]' and
- * setting each of the desired values in an array:
- * @code
- * $edit = array();
- * $edit['name[]'] = array('value1', 'value2');
- * @endcode
- * @todo change $edit to disallow NULL as a value for Drupal 9.
- * https://www.drupal.org/node/2802401
- * @param string $submit
- * Value of the submit button whose click is to be emulated. For example,
- * t('Save'). The processing of the request depends on this value. For
- * example, a form may have one button with the value t('Save') and another
- * button with the value t('Delete'), and execute different code depending
- * on which one is clicked.
- *
- * This function can also be called to emulate an Ajax submission. In this
- * case, this value needs to be an array with the following keys:
- * - path: A path to submit the form values to for Ajax-specific processing.
- * - triggering_element: If the value for the 'path' key is a generic Ajax
- * processing path, this needs to be set to the name of the element. If
- * the name doesn't identify the element uniquely, then this should
- * instead be an array with a single key/value pair, corresponding to the
- * element name and value. The \Drupal\Core\Form\FormAjaxResponseBuilder
- * uses this to find the #ajax information for the element, including
- * which specific callback to use for processing the request.
- *
- * This can also be set to NULL in order to emulate an Internet Explorer
- * submission of a form with a single text field, and pressing ENTER in that
- * textfield: under these conditions, no button information is added to the
- * POST data.
- * @param array $options
- * Options to be forwarded to the url generator.
- */
- protected function drupalPostForm($path, $edit, $submit, array $options = []) {
- if (is_object($submit)) {
- // Cast MarkupInterface objects to string.
- $submit = (string) $submit;
- }
- if ($edit === NULL) {
- $edit = [];
- }
- if (is_array($edit)) {
- $edit = $this->castSafeStrings($edit);
- }
-
- if (isset($path)) {
- $this->drupalGet($path, $options);
- }
-
- $this->submitForm($edit, $submit);
+ throw new \RuntimeException('The Mink client type ' . get_class($mink_driver) . ' does not support getHttpClient().');
}
/**
$this->rebuildAll();
}
- /**
- * Returns the parameters that will be used when Simpletest installs Drupal.
- *
- * @see install_drupal()
- * @see install_state_defaults()
- */
- protected function installParameters() {
- $connection_info = Database::getConnectionInfo();
- $driver = $connection_info['default']['driver'];
- $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
- unset($connection_info['default']['driver']);
- unset($connection_info['default']['namespace']);
- unset($connection_info['default']['pdo']);
- unset($connection_info['default']['init_commands']);
- $parameters = [
- 'interactive' => FALSE,
- 'parameters' => [
- 'profile' => $this->profile,
- 'langcode' => 'en',
- ],
- 'forms' => [
- 'install_settings_form' => [
- 'driver' => $driver,
- $driver => $connection_info['default'],
- ],
- 'install_configure_form' => [
- 'site_name' => 'Drupal',
- 'site_mail' => 'simpletest@example.com',
- 'account' => [
- 'name' => $this->rootUser->name,
- 'mail' => $this->rootUser->getEmail(),
- 'pass' => [
- 'pass1' => $this->rootUser->pass_raw,
- 'pass2' => $this->rootUser->pass_raw,
- ],
- ],
- // form_type_checkboxes_value() requires NULL instead of FALSE values
- // for programmatic form submissions to disable a checkbox.
- 'enable_update_status_module' => NULL,
- 'enable_update_status_emails' => NULL,
- ],
- ],
- ];
- return $parameters;
- }
-
- /**
- * Prepares the current environment for running the test.
- *
- * 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
- * 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.
- $this->classLoader = require __DIR__ . '/../../../../autoload.php';
- $request = Request::createFromGlobals();
- $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
- // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
- chdir(DRUPAL_ROOT);
- $kernel->prepareLegacyRequest($request);
- $this->prepareDatabasePrefix();
-
- $this->originalSite = $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);
-
- // Prepare filesystem directory paths.
- $this->publicFilesDirectory = $this->siteDirectory . '/files';
- $this->privateFilesDirectory = $this->siteDirectory . '/private';
- $this->tempFilesDirectory = $this->siteDirectory . '/temp';
- $this->translationFilesDirectory = $this->siteDirectory . '/translations';
-
- // Ensure the configImporter is refreshed for each test.
- $this->configImporter = NULL;
-
- // Unregister all custom stream wrappers of the parent site.
- $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
- foreach ($wrappers as $scheme => $info) {
- stream_wrapper_unregister($scheme);
- }
-
- // Reset statics.
- drupal_static_reset();
-
- // Ensure there is no service container.
- $this->container = NULL;
- \Drupal::unsetContainer();
-
- // Unset globals.
- unset($GLOBALS['config_directories']);
- unset($GLOBALS['config']);
- unset($GLOBALS['conf']);
-
- // Log fatal errors.
- ini_set('log_errors', 1);
- ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
-
- // Change the database prefix.
- $this->changeDatabasePrefix();
-
- // After preparing the environment and changing the database prefix, we are
- // in a valid test environment.
- drupal_valid_test_ua($this->databasePrefix);
-
- // Reset settings.
- new Settings([
- // For performance, simply use the database prefix as hash salt.
- 'hash_salt' => $this->databasePrefix,
- ]);
-
- drupal_set_time_limit($this->timeLimit);
-
- // Save and clean the shutdown callbacks array because it is static cached
- // and will be changed by the test run. Otherwise it will contain callbacks
- // from both environments and the testing environment will try to call the
- // handlers defined by the original one.
- $callbacks = &drupal_register_shutdown_function();
- $this->originalShutdownCallbacks = $callbacks;
- $callbacks = [];
- }
-
- /**
- * Returns whether a given user account is logged in.
- *
- * @param \Drupal\Core\Session\AccountInterface $account
- * The user account object to check.
- *
- * @return bool
- * Return TRUE if the user is logged in, FALSE otherwise.
- */
- protected function drupalUserIsLoggedIn(AccountInterface $account) {
- $logged_in = FALSE;
-
- if (isset($account->sessionId)) {
- $session_handler = $this->container->get('session_handler.storage');
- $logged_in = (bool) $session_handler->read($account->sessionId);
- }
-
- return $logged_in;
- }
-
- /**
- * Clicks the element with the given CSS selector.
- *
- * @param string $css_selector
- * The CSS selector identifying the element to click.
- */
- protected function click($css_selector) {
- $this->getSession()->getDriver()->click($this->cssSelectToXpath($css_selector));
- }
-
/**
* Prevents serializing any properties.
*
return [];
}
- /**
- * Logs a HTML output message in a text file.
- *
- * The link to the HTML output message will be printed by the results printer.
- *
- * @param string $message
- * The HTML output message to be stored.
- *
- * @see \Drupal\Tests\Listeners\VerbosePrinter::printResult()
- */
- protected function htmlOutput($message) {
- if (!$this->htmlOutputEnabled) {
- return;
- }
- $message = '<hr />ID #' . $this->htmlOutputCounter . ' (<a href="' . $this->htmlOutputClassName . '-' . ($this->htmlOutputCounter - 1) . '-' . $this->htmlOutputTestId . '.html">Previous</a> | <a href="' . $this->htmlOutputClassName . '-' . ($this->htmlOutputCounter + 1) . '-' . $this->htmlOutputTestId . '.html">Next</a>)<hr />' . $message;
- $html_output_filename = $this->htmlOutputClassName . '-' . $this->htmlOutputCounter . '-' . $this->htmlOutputTestId . '.html';
- file_put_contents($this->htmlOutputDirectory . '/' . $html_output_filename, $message);
- file_put_contents($this->htmlOutputCounterStorage, $this->htmlOutputCounter++);
- file_put_contents($this->htmlOutputFile, file_create_url('sites/simpletest/browser_output/' . $html_output_filename) . "\n", FILE_APPEND);
- }
-
- /**
- * Returns headers in HTML output format.
- *
- * @return string
- * HTML output headers.
- */
- protected function getHtmlOutputHeaders() {
- return $this->formatHtmlOutputHeaders($this->getSession()->getResponseHeaders());
- }
-
- /**
- * Formats HTTP headers as string for HTML output logging.
- *
- * @param array[] $headers
- * Headers that should be formatted.
- *
- * @return string
- * The formatted HTML string.
- */
- protected function formatHtmlOutputHeaders(array $headers) {
- $flattened_headers = array_map(function($header) {
- if (is_array($header)) {
- return implode(';', array_map('trim', $header));
- }
- else {
- return $header;
- }
- }, $headers);
- return '<hr />Headers: <pre>' . Html::escape(var_export($flattened_headers, TRUE)) . '</pre>';
- }
-
/**
* Translates a CSS expression to its XPath equivalent.
*
return (new CssSelectorConverter($html))->toXPath($selector, $prefix);
}
- /**
- * Searches elements using a CSS selector in the raw content.
- *
- * The search is relative to the root element (HTML tag normally) of the page.
- *
- * @param string $selector
- * CSS selector to use in the search.
- *
- * @return \Behat\Mink\Element\NodeElement[]
- * The list of elements on the page that match the selector.
- */
- protected function cssSelect($selector) {
- return $this->getSession()->getPage()->findAll('css', $selector);
- }
-
- /**
- * Follows a link by complete name.
- *
- * Will click the first link found with this link text.
- *
- * If the link is discovered and clicked, the test passes. Fail otherwise.
- *
- * @param string|\Drupal\Component\Render\MarkupInterface $label
- * Text between the anchor tags.
- * @param int $index
- * (optional) The index number for cases where multiple links have the same
- * text. Defaults to 0.
- */
- protected function clickLink($label, $index = 0) {
- $label = (string) $label;
- $links = $this->getSession()->getPage()->findAll('named', ['link', $label]);
- $links[$index]->click();
- }
-
- /**
- * Retrieves the plain-text content from the current page.
- */
- protected function getTextContent() {
- return $this->getSession()->getPage()->getText();
- }
-
/**
* Performs an xpath search on the contents of the internal browser.
*
return $this->getSession()->getResponseHeader($name);
}
- /**
- * Get the current URL from the browser.
- *
- * @return string
- * The current URL.
- */
- protected function getUrl() {
- return $this->getSession()->getCurrentUrl();
- }
-
/**
* Gets the JavaScript drupalSettings variable for the currently-loaded page.
*
}
/**
- * Checks for meta refresh tag and if found call drupalGet() recursively.
+ * Transforms a nested array into a flat array suitable for drupalPostForm().
*
- * This function looks for the http-equiv attribute to be set to "Refresh" and
- * is case-sensitive.
+ * @param array $values
+ * A multi-dimensional form values array to convert.
*
- * @return string|false
- * Either the new page content or FALSE.
- */
- protected function checkForMetaRefresh() {
- $refresh = $this->cssSelect('meta[http-equiv="Refresh"]');
- if (!empty($refresh) && (!isset($this->maximumMetaRefreshCount) || $this->metaRefreshCount < $this->maximumMetaRefreshCount)) {
- // Parse the content attribute of the meta tag for the format:
- // "[delay]: URL=[page_to_redirect_to]".
- if (preg_match('/\d+;\s*URL=(?<url>.*)/i', $refresh[0]->getAttribute('content'), $match)) {
- $this->metaRefreshCount++;
- return $this->drupalGet($this->getAbsoluteUrl(Html::decodeEntities($match['url'])));
- }
- }
- return FALSE;
+ * @return array
+ * The flattened $edit array suitable for BrowserTestBase::drupalPostForm().
+ */
+ protected function translatePostValues(array $values) {
+ $edit = [];
+ // The easiest and most straightforward way to translate values suitable for
+ // BrowserTestBase::drupalPostForm() is to actually build the POST data
+ // string and convert the resulting key/value pairs back into a flat array.
+ $query = http_build_query($values);
+ foreach (explode('&', $query) as $item) {
+ list($key, $value) = explode('=', $item);
+ $edit[urldecode($key)] = urldecode($value);
+ }
+ return $edit;
}
}