Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / basic_auth / tests / src / Traits / BasicAuthTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\basic_auth\Traits;
4
5 /**
6  * Provides common functionality for Basic Authentication test classes.
7  */
8 trait BasicAuthTestTrait {
9
10   /**
11    * Retrieves a Drupal path or an absolute path using basic authentication.
12    *
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.
21    *
22    * @return string
23    *   The retrieved HTML string, also available as $this->getRawContent().
24    */
25   protected function basicAuthGet($path, $username, $password, array $options = []) {
26     return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password));
27   }
28
29   /**
30    * Executes a form submission using basic authentication.
31    *
32    * @param string $path
33    *   Location of the post form.
34    * @param array $edit
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.
48    *
49    * @return string
50    *   The retrieved HTML string.
51    *
52    * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
53    */
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);
56   }
57
58   /**
59    * Returns HTTP headers that can be used for basic authentication in Curl.
60    *
61    * @param string $username
62    *   The username to use for basic authentication.
63    * @param string $password
64    *   The password to use for basic authentication.
65    *
66    * @return array
67    *   An array of raw request headers as used by curl_setopt().
68    */
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")];
72   }
73
74 }