- 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);