Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / basic_auth / src / Tests / BasicAuthTestTrait.php
1 <?php
2
3 namespace Drupal\basic_auth\Tests;
4
5 @trigger_error(__NAMESPACE__ . '\BasicAuthTestTrait 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);
6
7 /**
8  * Provides common functionality for Basic Authentication test classes.
9  *
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.
12  *
13  * @see https://www.drupal.org/node/2862800
14  */
15 trait BasicAuthTestTrait {
16
17   /**
18    * Retrieves a Drupal path or an absolute path using basic authentication.
19    *
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.
28    *
29    * @return string
30    *   The retrieved HTML string, also available as $this->getRawContent().
31    */
32   protected function basicAuthGet($path, $username, $password, array $options = []) {
33     return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password));
34   }
35
36   /**
37    * Executes a form submission using basic authentication.
38    *
39    * @param string $path
40    *   Location of the post form.
41    * @param array $edit
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.
55    *
56    * @return string
57    *   The retrieved HTML string.
58    *
59    * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
60    */
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);
63   }
64
65   /**
66    * Returns HTTP headers that can be used for basic authentication in Curl.
67    *
68    * @param string $username
69    *   The username to use for basic authentication.
70    * @param string $password
71    *   The password to use for basic authentication.
72    *
73    * @return array
74    *   An array of raw request headers as used by curl_setopt().
75    */
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")];
79   }
80
81 }