annotate core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\basic_auth\Tests;
Chris@0 4
Chris@14 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);
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides common functionality for Basic Authentication test classes.
Chris@0 9 *
Chris@0 10 * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0.
Chris@0 11 * Use \Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait instead.
Chris@0 12 *
Chris@0 13 * @see https://www.drupal.org/node/2862800
Chris@0 14 */
Chris@0 15 trait BasicAuthTestTrait {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Retrieves a Drupal path or an absolute path using basic authentication.
Chris@0 19 *
Chris@0 20 * @param \Drupal\Core\Url|string $path
Chris@0 21 * Drupal path or URL to load into the internal browser.
Chris@0 22 * @param string $username
Chris@0 23 * The username to use for basic authentication.
Chris@0 24 * @param string $password
Chris@0 25 * The password to use for basic authentication.
Chris@0 26 * @param array $options
Chris@0 27 * (optional) Options to be forwarded to the url generator.
Chris@0 28 *
Chris@0 29 * @return string
Chris@0 30 * The retrieved HTML string, also available as $this->getRawContent().
Chris@0 31 */
Chris@0 32 protected function basicAuthGet($path, $username, $password, array $options = []) {
Chris@0 33 return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password));
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Executes a form submission using basic authentication.
Chris@0 38 *
Chris@0 39 * @param string $path
Chris@0 40 * Location of the post form.
Chris@0 41 * @param array $edit
Chris@0 42 * Field data in an associative array.
Chris@0 43 * @param string $submit
Chris@0 44 * Value of the submit button whose click is to be emulated.
Chris@0 45 * @param string $username
Chris@0 46 * The username to use for basic authentication.
Chris@0 47 * @param string $password
Chris@0 48 * The password to use for basic authentication.
Chris@0 49 * @param array $options
Chris@0 50 * Options to be forwarded to the url generator.
Chris@0 51 * @param string $form_html_id
Chris@0 52 * (optional) HTML ID of the form to be submitted.
Chris@0 53 * @param string $extra_post
Chris@0 54 * (optional) A string of additional data to append to the POST submission.
Chris@0 55 *
Chris@0 56 * @return string
Chris@0 57 * The retrieved HTML string.
Chris@0 58 *
Chris@0 59 * @see \Drupal\simpletest\WebTestBase::drupalPostForm()
Chris@0 60 */
Chris@0 61 protected function basicAuthPostForm($path, $edit, $submit, $username, $password, array $options = [], $form_html_id = NULL, $extra_post = NULL) {
Chris@0 62 return $this->drupalPostForm($path, $edit, $submit, $options, $this->getBasicAuthHeaders($username, $password), $form_html_id, $extra_post);
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Returns HTTP headers that can be used for basic authentication in Curl.
Chris@0 67 *
Chris@0 68 * @param string $username
Chris@0 69 * The username to use for basic authentication.
Chris@0 70 * @param string $password
Chris@0 71 * The password to use for basic authentication.
Chris@0 72 *
Chris@0 73 * @return array
Chris@0 74 * An array of raw request headers as used by curl_setopt().
Chris@0 75 */
Chris@0 76 protected function getBasicAuthHeaders($username, $password) {
Chris@0 77 // Set up Curl to use basic authentication with the test user's credentials.
Chris@0 78 return ['Authorization: Basic ' . base64_encode("$username:$password")];
Chris@0 79 }
Chris@0 80
Chris@0 81 }