3 namespace Drupal\Tests\basic_auth\Traits;
6 * Provides common functionality for Basic Authentication test classes.
8 trait BasicAuthTestTrait {
11 * Retrieves a Drupal path or an absolute path using basic authentication.
13 * @param \Drupal\Core\Url|string $path
14 * Drupal path or URL to load into the internal browser.
15 * @param string $username
16 * The username to use for basic authentication.
17 * @param string $password
18 * The password to use for basic authentication.
19 * @param array $options
20 * (optional) Options to be forwarded to the url generator.
23 * The retrieved HTML string, also available as $this->getRawContent().
25 protected function basicAuthGet($path, $username, $password, array $options = []) {
26 return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password));
30 * Executes a form submission using basic authentication.
33 * Location of the post form.
35 * Field data in an associative array.
36 * @param string $submit
37 * Value of the submit button whose click is to be emulated.
38 * @param string $username
39 * The username to use for basic authentication.
40 * @param string $password
41 * The password to use for basic authentication.
42 * @param array $options
43 * Options to be forwarded to the url generator.
44 * @param string $form_html_id
45 * (optional) HTML ID of the form to be submitted.
46 * @param string $extra_post
47 * (optional) A string of additional data to append to the POST submission.
50 * The retrieved HTML string.
52 * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
54 protected function basicAuthPostForm($path, $edit, $submit, $username, $password, array $options = [], $form_html_id = NULL, $extra_post = NULL) {
55 return $this->drupalPostForm($path, $edit, $submit, $options, $this->getBasicAuthHeaders($username, $password), $form_html_id, $extra_post);
59 * Returns HTTP headers that can be used for basic authentication in Curl.
61 * @param string $username
62 * The username to use for basic authentication.
63 * @param string $password
64 * The password to use for basic authentication.
67 * An array of raw request headers as used by curl_setopt().
69 protected function getBasicAuthHeaders($username, $password) {
70 // Set up Curl to use basic authentication with the test user's credentials.
71 return ['Authorization' => 'Basic ' . base64_encode("$username:$password")];