View source
<?php
namespace Drupal\Tests\cas\Functional;
class CasLoginFormTest extends CasBrowserTestBase {
protected static $modules = [
'cas',
'page_cache',
'dynamic_page_cache',
];
public function testLoginLinkOnLoginForm() {
$config = $this
->config('cas.settings');
$this
->assertFalse($config
->get('login_link_enabled'));
$this
->assertEquals('CAS Login', $config
->get('login_link_label'));
$this
->drupalGet('/user/login');
$this
->assertSession()
->linkNotExists('CAS Login');
$this
->drupalLogin($this
->drupalCreateUser([
'administer account settings',
]));
$edit = [
'general[login_link_enabled]' => TRUE,
'general[login_link_label]' => 'Click here to login!',
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$config = $this
->config('cas.settings');
$this
->assertTrue($config
->get('login_link_enabled'));
$this
->assertEquals('Click here to login!', $config
->get('login_link_label'));
$this
->drupalLogout();
$this
->drupalGet('/user/login');
$this
->assertSession()
->linkExists('Click here to login!');
}
public function testPreventNormalLogin() {
$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');
$this
->drupalPostForm('/user/login', [
'name' => 'normal_user',
'pass' => 'password',
], 'Log in');
$this
->assertSession()
->addressEquals('/user/' . $normal_user
->id());
$this
->drupalLogout();
$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');
$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');
$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();
$this
->drupalPostForm('/user/login', [
'name' => 'cas_user',
'pass' => 'password',
], 'Log in');
$this
->assertSession()
->addressEquals('/user/' . $cas_user
->id());
}
}