public function CasUserFormFieldTest::testCasUsernameField in CAS 8
Same name and namespace in other branches
- 2.x tests/src/Functional/CasUserFormFieldTest.php \Drupal\Tests\cas\Functional\CasUserFormFieldTest::testCasUsernameField()
Tests that the CAS username field works as expected.
File
- tests/
src/ Functional/ CasUserFormFieldTest.php, line 20
Class
- CasUserFormFieldTest
- Tests modifications to the account and registration forms.
Namespace
Drupal\Tests\cas\FunctionalCode
public function testCasUsernameField() {
// First test that a normal user has no access to edit their CAS username.
$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.');
// Set the CAS username for this user, but leave the checkbox unchecked.
// This should have the same effect as not filling in a username at all.
$edit = [
'cas_enabled' => FALSE,
'cas_username' => 'test_user_1_cas',
];
$this
->drupalPostForm('/user/' . $test_user_1
->id() . '/edit', $edit, 'Save');
// Verify the field is empty.
$cas_username_field = $this
->getSession()
->getPage()
->findField('cas_username');
$this
->assertEmpty($cas_username_field
->getValue(), 'CAS username field should be empty.');
// Now fill it in and check the checkbox, which should work.
$edit = [
'cas_enabled' => TRUE,
'cas_username' => 'test_user_1_cas',
];
$this
->drupalPostForm('/user/' . $test_user_1
->id() . '/edit', $edit, 'Save');
// Check that field is still filled in with the CAS username.
$cas_username_field = $this
->getSession()
->getPage()
->findField('cas_username');
$this
->assertEquals('test_user_1_cas', $cas_username_field
->getValue());
// Verify data was stored in authmap properly as well.
$authmap = $this->container
->get('externalauth.authmap');
$this
->assertEquals('test_user_1_cas', $authmap
->get($test_user_1
->id(), 'cas'));
// Register a new user, attempting to use the same CAS username.
$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.');
// Submit with proper CAS username, and verify user was created and has the
// proper CAS username associated.
$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'));
// Should be able to clear out the CAS enabled checkbox to remove the
// authmap entry.
$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'));
// Visit the edit page for this user to ensure CAS username field empty.
$this
->drupalGet('/user/' . $test_user_2
->id() . '/edit');
$this
->assertEmpty($this
->getSession()
->getPage()
->findField('cas_username')
->getValue());
}