3 namespace Drupal\basic_auth\Tests;
5 @trigger_error(__FILE__ . ' is deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait instead. See https://www.drupal.org/node/2862800.', E_USER_DEPRECATED);
8 * Provides common functionality for Basic Authentication test classes.
10 * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0.
11 * Use \Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait instead.
13 * @see https://www.drupal.org/node/2862800
15 trait BasicAuthTestTrait {
18 * Retrieves a Drupal path or an absolute path using basic authentication.
20 * @param \Drupal\Core\Url|string $path
21 * Drupal path or URL to load into the internal browser.
22 * @param string $username
23 * The username to use for basic authentication.
24 * @param string $password
25 * The password to use for basic authentication.
26 * @param array $options
27 * (optional) Options to be forwarded to the url generator.
30 * The retrieved HTML string, also available as $this->getRawContent().
32 protected function basicAuthGet($path, $username, $password, array $options = []) {
33 return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password));
37 * Executes a form submission using basic authentication.
40 * Location of the post form.
42 * Field data in an associative array.
43 * @param string $submit
44 * Value of the submit button whose click is to be emulated.
45 * @param string $username
46 * The username to use for basic authentication.
47 * @param string $password
48 * The password to use for basic authentication.
49 * @param array $options
50 * Options to be forwarded to the url generator.
51 * @param string $form_html_id
52 * (optional) HTML ID of the form to be submitted.
53 * @param string $extra_post
54 * (optional) A string of additional data to append to the POST submission.
57 * The retrieved HTML string.
59 * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
61 protected function basicAuthPostForm($path, $edit, $submit, $username, $password, array $options = [], $form_html_id = NULL, $extra_post = NULL) {
62 return $this->drupalPostForm($path, $edit, $submit, $options, $this->getBasicAuthHeaders($username, $password), $form_html_id, $extra_post);
66 * Returns HTTP headers that can be used for basic authentication in Curl.
68 * @param string $username
69 * The username to use for basic authentication.
70 * @param string $password
71 * The password to use for basic authentication.
74 * An array of raw request headers as used by curl_setopt().
76 protected function getBasicAuthHeaders($username, $password) {
77 // Set up Curl to use basic authentication with the test user's credentials.
78 return ['Authorization: Basic ' . base64_encode("$username:$password")];