You are here

public function CasLoginFormTest::testPreventNormalLogin in CAS 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/CasLoginFormTest.php \Drupal\Tests\cas\Functional\CasLoginFormTest::testPreventNormalLogin()

Tests the "prevent normal login" feature.

File

tests/src/Functional/CasLoginFormTest.php, line 48

Class

CasLoginFormTest
Tests the login link on the user login form.

Namespace

Drupal\Tests\cas\Functional

Code

public function testPreventNormalLogin() {

  // Should be enabled by default.
  $config = $this
    ->config('cas.settings');
  $this
    ->assertTrue($config
    ->get('user_accounts.prevent_normal_login'));
  $normal_user = $this
    ->drupalCreateUser([], 'normal_user');
  $normal_user
    ->setPassword('password');
  $normal_user
    ->save();
  $cas_user = $this
    ->drupalCreateUser([], 'cas_user');
  $cas_user
    ->setPassword('password');
  $cas_user
    ->save();
  $this->container
    ->get('cas.user_manager')
    ->setCasUsernameForAccount($cas_user, 'cas_user');

  // Log in in as normal user should work.
  $this
    ->drupalPostForm('/user/login', [
    'name' => 'normal_user',
    'pass' => 'password',
  ], 'Log in');
  $this
    ->assertSession()
    ->addressEquals('/user/' . $normal_user
    ->id());
  $this
    ->drupalLogout();

  // Log in as CAS user should not work.
  $this
    ->drupalPostForm('/user/login', [
    'name' => 'cas_user',
    'pass' => 'password',
  ], 'Log in');
  $this
    ->assertSession()
    ->addressEquals('/user/login');
  $this
    ->assertSession()
    ->pageTextContains('This account must log in using CAS.');
  $this
    ->assertSession()
    ->linkExists('CAS');

  // Test a customized error message.
  $this
    ->config('cas.settings')
    ->set('error_handling.message_prevent_normal_login', 'Just use the <a href="[cas:login-url]">CAS Login</a>')
    ->save();
  $this
    ->drupalPostForm('/user/login', [
    'name' => 'cas_user',
    'pass' => 'password',
  ], 'Log in');
  $this
    ->assertSession()
    ->addressEquals('/user/login');
  $this
    ->assertSession()
    ->pageTextContains('Just use the CAS Login');
  $this
    ->assertSession()
    ->linkExists('CAS Login');

  // Now turn off the setting and try again.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer account settings',
  ]));
  $edit = [
    'user_accounts[prevent_normal_login]' => FALSE,
  ];
  $this
    ->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
  $this
    ->drupalLogout();

  // Log in as CAS user should work now.
  $this
    ->drupalPostForm('/user/login', [
    'name' => 'cas_user',
    'pass' => 'password',
  ], 'Log in');
  $this
    ->assertSession()
    ->addressEquals('/user/' . $cas_user
    ->id());
}