summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-03-22 22:03:10 (GMT)
committerAlex Pott2017-03-22 22:03:10 (GMT)
commitf1bfa50c32de4ade3a532f7a3cdc38da8a520935 (patch)
treed55c2f87b8d30c2ef9ca51e5ef3e2c6a24e524a7
parent8e871302fee22d21e6774db6b576dba250cf2de2 (diff)
Issue #2862494 by klausi, GoZ, Jo Fitzgerald: Convert web tests to browser tests for basic_auth module
-rw-r--r--core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php7
-rw-r--r--core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php (renamed from core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php)17
-rw-r--r--core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php74
3 files changed, 90 insertions, 8 deletions
diff --git a/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php b/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php
index 8a289b0..0e0aceb 100644
--- a/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php
+++ b/core/modules/basic_auth/src/Tests/BasicAuthTestTrait.php
@@ -2,8 +2,15 @@
namespace Drupal\basic_auth\Tests;
+@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);
+
/**
* Provides common functionality for Basic Authentication test classes.
+ *
+ * @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
*/
trait BasicAuthTestTrait {
diff --git a/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php b/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 @@
<?php
-namespace Drupal\basic_auth\Tests\Authentication;
+namespace Drupal\Tests\basic_auth\Functional;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Url;
-use Drupal\basic_auth\Tests\BasicAuthTestTrait;
+use Drupal\Tests\basic_auth\Traits\BasicAuthTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
-use Drupal\simpletest\WebTestBase;
+use Drupal\Tests\BrowserTestBase;
/**
* Tests for BasicAuth authentication provider.
*
* @group basic_auth
*/
-class BasicAuthTest extends WebTestBase {
+class BasicAuthTest extends BrowserTestBase {
use BasicAuthTestTrait;
@@ -39,14 +39,14 @@ class BasicAuthTest extends WebTestBase {
$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();
+ $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 @@ class BasicAuthTest extends WebTestBase {
$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 @@ class BasicAuthTest extends WebTestBase {
$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 @@ class BasicAuthTest extends WebTestBase {
$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 @@
+<?php
+
+namespace Drupal\Tests\basic_auth\Traits;
+
+/**
+ * Provides common functionality for Basic Authentication test classes.
+ */
+trait BasicAuthTestTrait {
+
+ /**
+ * Retrieves a Drupal path or an absolute path using basic authentication.
+ *
+ * @param \Drupal\Core\Url|string $path
+ * Drupal path or URL to load into the internal browser.
+ * @param string $username
+ * The username to use for basic authentication.
+ * @param string $password
+ * The password to use for basic authentication.
+ * @param array $options
+ * (optional) Options to be forwarded to the url generator.
+ *
+ * @return string
+ * The retrieved HTML string, also available as $this->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")];
+ }
+
+}