You are here

public function BasicAuthTest::testUnauthorizedErrorMessage in Drupal 8

Same name and namespace in other branches
  1. 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 175

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.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(401);
  $this
    ->assertNoText('Exception', "No raw exception is displayed on the page.");
  $this
    ->assertText('Please log in to access this page.', "A user friendly access unauthorized message is displayed.");

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

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

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