View source
<?php
namespace Drupal\Tests\cas\Functional;
class CasUserFormFieldTest extends CasBrowserTestBase {
public static $modules = [
'cas',
'page_cache',
'dynamic_page_cache',
];
public function testCasUsernameField() {
$test_user_1 = $this
->drupalCreateUser([], 'test_user_1');
$this
->drupalLogin($test_user_1);
$this
->drupalGet('/user/' . $test_user_1
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNull($page
->findField('cas_enabled'), 'CAS enabled checkbox was found on page when user should not have access.');
$this
->assertNull($page
->findField('cas_username'), 'CAS username field was found on page when user should not have access.');
$this
->drupalLogout();
$admin_user = $this
->drupalCreateUser([
'administer users',
], 'test_admin');
$this
->drupalLogin($admin_user);
$this
->drupalGet('/user/' . $test_user_1
->id() . '/edit');
$cas_enabled_checkbox = $this
->getSession()
->getPage()
->findField('cas_enabled');
$this
->assertNotNull($cas_enabled_checkbox, 'CAS enabled checkbox should exist on user form.');
$cas_username_field = $this
->getSession()
->getPage()
->findField('cas_username');
$this
->assertNotNull($cas_username_field, 'CAS username field should exist on user form.');
$edit = [
'cas_enabled' => FALSE,
'cas_username' => 'test_user_1_cas',
];
$this
->drupalPostForm('/user/' . $test_user_1
->id() . '/edit', $edit, 'Save');
$cas_username_field = $this
->getSession()
->getPage()
->findField('cas_username');
$this
->assertEmpty($cas_username_field
->getValue(), 'CAS username field should be empty.');
$edit = [
'cas_enabled' => TRUE,
'cas_username' => 'test_user_1_cas',
];
$this
->drupalPostForm('/user/' . $test_user_1
->id() . '/edit', $edit, 'Save');
$cas_username_field = $this
->getSession()
->getPage()
->findField('cas_username');
$this
->assertEquals('test_user_1_cas', $cas_username_field
->getValue());
$authmap = $this->container
->get('externalauth.authmap');
$this
->assertEquals('test_user_1_cas', $authmap
->get($test_user_1
->id(), 'cas'));
$new_user_data = [
'mail' => 'test_user_2@sample.com',
'name' => 'test_user_2',
'pass[pass1]' => 'test_user_2',
'pass[pass2]' => 'test_user_2',
'cas_enabled' => TRUE,
'cas_username' => 'test_user_1_cas',
];
$this
->drupalPostForm('/admin/people/create', $new_user_data, 'Create new account');
$output = $this
->getSession()
->getPage()
->getContent();
$validation_error_message = 'The specified CAS username is already in use by another user.';
$this
->assertContains($validation_error_message, $output, 'Expected validation error not found on page.');
$new_user_data['cas_username'] = 'test_user_2_cas';
$this
->drupalPostForm('/admin/people/create', $new_user_data, 'Create new account');
$output = $this
->getSession()
->getPage()
->getContent();
$this
->assertNotContains($validation_error_message, $output, 'Validation error should not be found.');
$test_user_2 = $this->container
->get('entity_type.manager')
->getStorage('user')
->loadByProperties([
'name' => 'test_user_2',
]);
$test_user_2 = reset($test_user_2);
$this
->assertNotNull($test_user_2);
$authmap = $this->container
->get('externalauth.authmap');
$this
->assertEquals($test_user_2
->id(), $authmap
->getUid('test_user_2_cas', 'cas'));
$edit = [
'cas_enabled' => FALSE,
];
$this
->drupalPostForm('/user/' . $test_user_2
->id() . '/edit', $edit, 'Save');
$authmap = $this->container
->get('externalauth.authmap');
$this
->assertFalse($authmap
->get($test_user_2
->id(), 'cas'));
$this
->drupalGet('/user/' . $test_user_2
->id() . '/edit');
$this
->assertEmpty($this
->getSession()
->getPage()
->findField('cas_username')
->getValue());
}
public function testRestrictedPasswordManagementWorks() {
$admin = $this
->drupalCreateUser([
'administer account settings',
'administer users',
]);
$non_cas_user = $this
->drupalCreateUser();
$cas_user = $this
->drupalCreateUser();
$this->container
->get('cas.user_manager')
->setCasUsernameForAccount($cas_user, 'cas_user');
$this
->drupalLogin($admin);
$edit = [
'user_accounts[restrict_password_management]' => TRUE,
'user_accounts[prevent_normal_login]' => FALSE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->assertEquals(TRUE, $this
->config('cas.settings')
->get('user_accounts.restrict_password_management'));
$this
->drupalLogout();
$this
->drupalLogin($non_cas_user);
$this
->drupalGet('/user/' . $non_cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNotNull($page
->findField('pass[pass1]'));
$this
->assertNotNull($page
->findField('pass[pass2]'));
$this
->assertNotNull($page
->findField('current_pass'));
$form_data = [
'pass[pass1]' => 'newpass',
'pass[pass2]' => 'newpass',
'current_pass' => 'incorrectpassword',
'mail' => 'new-noncasuser-email@sample.com',
];
$this
->drupalPostForm('/user/' . $non_cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('Your current password is missing or incorrect');
$form_data['current_pass'] = $non_cas_user->pass_raw;
$this
->drupalPostForm('/user/' . $non_cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('The changes have been saved.');
$this
->drupalLogout();
$this
->drupalLogin($cas_user);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNull($page
->findField('pass[pass1]'));
$this
->assertNull($page
->findField('pass[pass2]'));
$this
->assertNull($page
->findField('current_pass'));
$form_data = [
'mail' => 'new-casuser-email@sample.com',
];
$this
->drupalPostForm('/user/' . $cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('The changes have been saved.');
$this
->drupalLogout();
$this
->drupalLogin($admin);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNotNull($page
->findField('pass[pass1]'));
$this
->assertNotNull($page
->findField('pass[pass2]'));
$edit = [
'user_accounts[restrict_password_management]' => FALSE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->assertEquals(FALSE, $this
->config('cas.settings')
->get('user_accounts.restrict_password_management'));
$this
->drupalLogout();
$this
->drupalLogin($cas_user);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNotNull($page
->findField('pass[pass1]'));
$this
->assertNotNull($page
->findField('pass[pass2]'));
$this
->assertNotNull($page
->findField('current_pass'));
$form_data = [
'pass[pass1]' => 'newpass',
'pass[pass2]' => 'newpass',
'current_pass' => 'incorrectpassword',
'mail' => 'another-new-casuser-email@sample.com',
];
$this
->drupalPostForm('/user/' . $cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('Your current password is missing or incorrect');
$form_data['current_pass'] = $cas_user->pass_raw;
$this
->drupalPostForm('/user/' . $cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('The changes have been saved.');
}
public function testRestrictedEmailManagementWorks() {
$admin = $this
->drupalCreateUser([
'administer account settings',
'administer users',
]);
$non_cas_user = $this
->drupalCreateUser();
$cas_user = $this
->drupalCreateUser();
$this->container
->get('cas.user_manager')
->setCasUsernameForAccount($cas_user, 'cas_user');
$this
->drupalLogin($admin);
$edit = [
'user_accounts[restrict_email_management]' => TRUE,
'user_accounts[prevent_normal_login]' => FALSE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->assertEquals(TRUE, $this
->config('cas.settings')
->get('user_accounts.restrict_email_management'));
$this
->drupalLogout();
$this
->drupalLogin($non_cas_user);
$this
->drupalGet('/user/' . $non_cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertNotNull($page
->findField('mail'));
$form_data = [
'current_pass' => 'incorrectpassword',
'mail' => 'new-noncasuser-email@sample.com',
];
$this
->drupalPostForm('/user/' . $non_cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('Your current password is missing or incorrect');
$form_data['current_pass'] = $non_cas_user->pass_raw;
$this
->drupalPostForm('/user/' . $non_cas_user
->id() . '/edit', $form_data, 'Save');
$this
->assertSession()
->responseContains('The changes have been saved.');
$this
->drupalLogout();
$this
->drupalLogin($cas_user);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$email_field = $page
->findField('mail');
$this
->assertNotNull($email_field);
$this
->assertEquals('disabled', $email_field
->getAttribute('disabled'));
$this
->drupalLogout();
$this
->drupalLogin($admin);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$email_field = $page
->findField('mail');
$this
->assertNotNull($email_field);
$this
->assertObjectNotHasAttribute('disabled', $email_field);
$edit = [
'user_accounts[restrict_email_management]' => FALSE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->assertEquals(FALSE, $this
->config('cas.settings')
->get('user_accounts.restrict_email_management'));
$this
->drupalLogout();
$this
->drupalLogin($cas_user);
$this
->drupalGet('/user/' . $cas_user
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$email_field = $page
->findField('mail');
$this
->assertNotNull($email_field);
$this
->assertEmpty($email_field
->getAttribute('disabled'));
}
}