From b52e70e842a9c615f4bb952e9878677851742b71 Mon Sep 17 00:00:00 2001 From: GoZ Date: Wed, 22 Mar 2017 09:52:54 +0100 Subject: [PATCH] Issue #2862494 by klausi: Convert web tests to browser tests for basic_auth module --- .../basic_auth/src/Tests/BasicAuthTestTrait.php | 6 ++ .../src/Functional}/BasicAuthTest.php | 17 ++--- .../tests/src/Traits/BasicAuthTestTrait.php | 74 ++++++++++++++++++++++ 3 files changed, 89 insertions(+), 8 deletions(-) rename core/modules/basic_auth/{src/Tests/Authentication => tests/src/Functional}/BasicAuthTest.php (96%) create mode 100644 core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php diff --git a/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php b/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php index 8a289b0..c21547a 100644 --- a/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php +++ b/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php @@ -4,7 +4,13 @@ /** * Provides common functionality for Basic Authentication test classes. + * + * @deprecated in Drupal 8.4.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 */ +@trigger_error(__FILE__ . ' is deprecated in Drupal 8.4.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); trait BasicAuthTestTrait { /** diff --git a/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php b/core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php similarity index 96% rename from core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php rename to core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php index 3650e4f..b197a76 100644 --- a/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php +++ b/core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php @@ -1,19 +1,19 @@ basicAuthGet($url, $account->getUsername(), $account->pass_raw); $this->assertText($account->getUsername(), 'Account name is displayed.'); $this->assertResponse('200', 'HTTP response is OK'); - $this->curlClose(); + $this->mink->resetSessions(); $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache')); $this->assertIdentical(strpos($this->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'Cache-Control is not set to public'); $this->basicAuthGet($url, $account->getUsername(), $this->randomMachineName()); $this->assertNoText($account->getUsername(), 'Bad basic auth credentials do not authenticate the user.'); $this->assertResponse('403', 'Access is not granted.'); - $this->curlClose(); + $this->mink->resetSessions(); $this->drupalGet($url); $this->assertEqual($this->drupalGetHeader('WWW-Authenticate'), SafeMarkup::format('Basic realm="@realm"', ['@realm' => \Drupal::config('system.site')->get('name')])); @@ -60,7 +60,7 @@ public function testBasicAuth() { $this->basicAuthGet(Url::fromRoute('system.admin'), $account->getUsername(), $account->pass_raw); $this->assertNoLink('Log out', 'User is not logged in'); $this->assertResponse('403', 'No basic authentication for routes not explicitly defining authentication providers.'); - $this->curlClose(); + $this->mink->resetSessions(); // Ensure that pages already in the page cache aren't returned from page // cache if basic auth credentials are provided. @@ -148,7 +148,6 @@ public function testLocale() { $this->basicAuthGet($url, $account->getUsername(), $account->pass_raw); $this->assertText($account->getUsername(), 'Account name is displayed.'); $this->assertResponse('200', 'HTTP response is OK'); - $this->curlClose(); } /** @@ -197,6 +196,8 @@ public function testControllerNotCalledBeforeAuth() { $this->basicAuthGet('/basic_auth_test/state/modify', $account->getUsername(), $account->pass_raw); $this->assertResponse(200); $this->assertRaw('Done'); + + $this->mink->resetSessions(); $this->drupalGet('/basic_auth_test/state/read'); $this->assertResponse(200); $this->assertRaw('yep'); diff --git a/core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php b/core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php new file mode 100644 index 0000000..f848220 --- /dev/null +++ b/core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php @@ -0,0 +1,74 @@ +getRawContent(). + */ + protected function basicAuthGet($path, $username, $password, array $options = []) { + return $this->drupalGet($path, $options, $this->getBasicAuthHeaders($username, $password)); + } + + /** + * Executes a form submission using basic authentication. + * + * @param string $path + * Location of the post form. + * @param array $edit + * Field data in an associative array. + * @param string $submit + * Value of the submit button whose click is to be emulated. + * @param string $username + * The username to use for basic authentication. + * @param string $password + * The password to use for basic authentication. + * @param array $options + * Options to be forwarded to the url generator. + * @param string $form_html_id + * (optional) HTML ID of the form to be submitted. + * @param string $extra_post + * (optional) A string of additional data to append to the POST submission. + * + * @return string + * The retrieved HTML string. + * + * @see \Drupal\simpletest\WebTestBase::drupalPostForm() + */ + protected function basicAuthPostForm($path, $edit, $submit, $username, $password, array $options = [], $form_html_id = NULL, $extra_post = NULL) { + return $this->drupalPostForm($path, $edit, $submit, $options, $this->getBasicAuthHeaders($username, $password), $form_html_id, $extra_post); + } + + /** + * Returns HTTP headers that can be used for basic authentication in Curl. + * + * @param string $username + * The username to use for basic authentication. + * @param string $password + * The password to use for basic authentication. + * + * @return array + * An array of raw request headers as used by curl_setopt(). + */ + protected function getBasicAuthHeaders($username, $password) { + // Set up Curl to use basic authentication with the test user's credentials. + return ['Authorization' => 'Basic ' . base64_encode("$username:$password")]; + } + +} -- 2.8.1