You are here

trait BasicAuthTestTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php \Drupal\basic_auth\Tests\BasicAuthTestTrait

Provides common functionality for Basic Authentication test classes.

Hierarchy

2 files declare their use of BasicAuthTestTrait
BasicAuthTest.php in core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php
Contains \Drupal\basic_auth\Tests\Authentication\BasicAuthTest.
SessionAuthenticationTest.php in core/modules/system/src/Tests/Session/SessionAuthenticationTest.php
Contains \Drupal\system\Tests\Session\SessionAuthenticationTest.

File

core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php, line 13
Contains \Drupal\basic_auth\Tests\BasicAuthTestTrait.

Namespace

Drupal\basic_auth\Tests
View source
trait BasicAuthTestTrait {

  /**
   * Retrieves a Drupal path or an absolute path using basic authentication.
   *
   * @param \Drupal\Core\Url|string $path
   *   Drupal path or URL to load into the internal browser.
   * @param string $username
   *   The username to use for basic authentication.
   * @param string $password
   *   The password to use for basic authentication.
   * @param array $options
   *   (optional) Options to be forwarded to the url generator.
   *
   * @return string
   *   The retrieved HTML string, also available as $this->getRawContent().
   */
  protected function basicAuthGet($path, $username, $password, array $options = []) {
    return $this
      ->drupalGet($path, $options, $this
      ->getBasicAuthHeaders($username, $password));
  }

  /**
   * Executes a form submission using basic authentication.
   *
   * @param string $path
   *   Location of the post form.
   * @param array $edit
   *   Field data in an associative array.
   * @param string $submit
   *   Value of the submit button whose click is to be emulated.
   * @param string $username
   *   The username to use for basic authentication.
   * @param string $password
   *   The password to use for basic authentication.
   * @param array $options
   *   Options to be forwarded to the url generator.
   * @param string $form_html_id
   *   (optional) HTML ID of the form to be submitted.
   * @param string $extra_post
   *   (optional) A string of additional data to append to the POST submission.
   *
   * @return string
   *   The retrieved HTML string.
   *
   * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
   */
  protected function basicAuthPostForm($path, $edit, $submit, $username, $password, array $options = array(), $form_html_id = NULL, $extra_post = NULL) {
    return $this
      ->drupalPostForm($path, $edit, $submit, $options, $this
      ->getBasicAuthHeaders($username, $password), $form_html_id, $extra_post);
  }

  /**
   * Returns HTTP headers that can be used for basic authentication in Curl.
   *
   * @param string $username
   *   The username to use for basic authentication.
   * @param string $password
   *   The password to use for basic authentication.
   *
   * @return array
   *   An array of raw request headers as used by curl_setopt().
   */
  protected function getBasicAuthHeaders($username, $password) {

    // Set up Curl to use basic authentication with the test user's credentials.
    return [
      'Authorization: Basic ' . base64_encode("{$username}:{$password}"),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BasicAuthTestTrait::basicAuthGet protected function Retrieves a Drupal path or an absolute path using basic authentication.
BasicAuthTestTrait::basicAuthPostForm protected function Executes a form submission using basic authentication.
BasicAuthTestTrait::getBasicAuthHeaders protected function Returns HTTP headers that can be used for basic authentication in Curl.