You are here

trait BasicAuthTestTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php \Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait
  2. 10 core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php \Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait

Provides common functionality for Basic Authentication test classes.

Hierarchy

2 files declare their use of BasicAuthTestTrait
BasicAuthTest.php in core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php
SessionAuthenticationTest.php in core/modules/system/tests/src/Functional/Session/SessionAuthenticationTest.php

File

core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php, line 8

Namespace

Drupal\Tests\basic_auth\Traits
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));
  }

  /**
   * 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::getBasicAuthHeaders protected function Returns HTTP headers that can be used for basic authentication in Curl.