You are here

public function KeyAuthTest::keyAuthRequest in Key auth 8

Perform a key authentication request to the test page.

Parameters

string $detection_method: The key detection method.

string $param_name: The key parameter name.

int $status_code: The expected response status code.

string $key: The authentication key.

\Drupal\user\UserInterface $user: The user making the request.

1 call to KeyAuthTest::keyAuthRequest()
KeyAuthTest::testKeyAuth in tests/src/Functional/KeyAuthTest.php
Test key authentication and related settings.

File

tests/src/Functional/KeyAuthTest.php, line 290

Class

KeyAuthTest
Tests for key authentication provider.

Namespace

Drupal\Tests\key_auth\Functional

Code

public function keyAuthRequest($detection_method = NULL, $param_name = NULL, $status_code = 200, $key = NULL, UserInterface $user = NULL) {

  // Check if no key or detection method was provided.
  if (!$detection_method || !$key) {

    // Make a regular request.
    $this
      ->drupalGet(Url::fromRoute('key_auth.test'));
  }
  elseif ($detection_method == KeyAuth::DETECTION_METHOD_HEADER) {
    $this
      ->drupalGet(Url::fromRoute('key_auth.test'), [], [
      $param_name => $key,
    ]);
  }
  elseif ($detection_method == KeyAuth::DETECTION_METHOD_QUERY) {
    $this
      ->drupalGet(Url::fromRoute('key_auth.test', [], [
      'query' => [
        $param_name => $key,
      ],
    ]));
  }
  else {
    $this
      ->assertTrue(FALSE);
    return;
  }

  // Check the status code.
  $this
    ->assertSession()
    ->statusCodeEquals($status_code);

  // Check if a 200 status code is expected.
  if ($status_code == 200) {

    // Ensure that caching was disabled.
    $this
      ->assertFalse($this
      ->drupalGetHeader('X-Drupal-Cache'));
    $this
      ->assertIdentical(strpos($this
      ->drupalGetHeader('Cache-Control'), 'public'), FALSE);
  }

  // Check if a user was provided.
  if ($user) {

    // If a 200 is expected, the user's name should appear on the page.
    if ($status_code == 200) {
      $this
        ->assertSession()
        ->pageTextContains($user
        ->getAccountName());
    }
    else {
      $this
        ->assertSession()
        ->pageTextNotContains($user
        ->getAccountName());
    }
  }

  // Reset the sessions.
  $this->mink
    ->resetSessions();
}