You are here

public function BasicAuthTest::testUnauthorizedErrorMessage in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php \Drupal\Tests\basic_auth\Functional\BasicAuthTest::testUnauthorizedErrorMessage()
  2. 9 core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php \Drupal\Tests\basic_auth\Functional\BasicAuthTest::testUnauthorizedErrorMessage()

Tests if a comprehensive message is displayed when the route is denied.

File

core/modules/basic_auth/tests/src/Functional/BasicAuthTest.php, line 174

Class

BasicAuthTest
Tests for BasicAuth authentication provider.

Namespace

Drupal\Tests\basic_auth\Functional

Code

public function testUnauthorizedErrorMessage() {
  $account = $this
    ->drupalCreateUser();
  $url = Url::fromRoute('router_test.11');

  // Case when no credentials are passed, a user friendly access
  // unauthorized message is displayed.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(401);
  $this
    ->assertSession()
    ->pageTextNotContains('Exception');
  $this
    ->assertSession()
    ->pageTextContains('Please log in to access this page.');

  // Case when empty credentials are passed, a user friendly access denied
  // message is displayed.
  $this
    ->basicAuthGet($url, NULL, NULL);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');

  // Case when wrong credentials are passed, a user friendly access denied
  // message is displayed.
  $this
    ->basicAuthGet($url, $account
    ->getAccountName(), $this
    ->randomMachineName());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');

  // Case when correct credentials but hasn't access to the route, an user
  // friendly access denied message is displayed.
  $url = Url::fromRoute('router_test.15');
  $this
    ->basicAuthGet($url, $account
    ->getAccountName(), $account->pass_raw);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');
}