View source
<?php
namespace Drupal\Tests\cas\Functional;
use Drupal\cas\CasPropertyBag;
use Drupal\Tests\cas\Traits\CasTestTrait;
class CasEventsTest extends CasBrowserTestBase {
use CasTestTrait;
protected static $modules = [
'block',
'cas',
'cas_mock_server',
'cas_test',
];
public function testSettingPropertiesOnRegistration() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer account settings',
]));
$edit = [
'user_accounts[auto_register]' => TRUE,
'user_accounts[email_hostname]' => 'sample.com',
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$cas_property_bag = new CasPropertyBag('foo');
\Drupal::service('cas.user_manager')
->login($cas_property_bag, 'fake_ticket_string');
$this
->assertFalse(user_load_by_name('foo'), 'User with name "foo" exists, but should not.');
$account = user_load_by_name('testing_foo');
$this
->assertNotFalse($account, 'User with name "testing_foo" was not found.');
$authmap = \Drupal::service('externalauth.authmap');
$this
->assertSame('foo', $authmap
->get($account
->id(), 'cas'));
}
public function testLoginCancelling() {
$account = $this
->createUser([], 'Antoine Batiste');
$this
->createCasUser('Antoine Batiste', 'antoine@example.com', 'baTistE', [], $account);
$this
->placeBlock('system_menu_block:account');
\Drupal::state()
->set('cas_test.flag', 'cancel login without message');
$this
->casLogin('antoine@example.com', 'baTistE');
$this
->assertSession()
->pageTextContains('You do not have access to log in to this website. Please contact a site administrator if you believe you should have access.');
$this
->assertSession()
->linkExists('Log in');
\Drupal::state()
->set('cas_test.flag', 'cancel login with message');
$this
->casLogin('antoine@example.com', 'baTistE');
$this
->assertSession()
->pageTextContains('Cancelled with a custom message.');
$this
->assertSession()
->linkExists('Log in');
}
public function testRegistrationCancelling() : void {
$settings = $this
->config('cas.settings');
$settings
->set('user_accounts.auto_register', TRUE)
->save();
$this
->createCasUser('Antoine Batiste', 'antoine@example.com', 'baTistE', []);
$this
->placeBlock('system_menu_block:account');
\Drupal::state()
->set('cas_test.flag', 'cancel register without message');
$this
->casLogin('antoine@example.com', 'baTistE');
$this
->assertSession()
->pageTextContains('You do not have access to log in to this website. Please contact a site administrator if you believe you should have access.');
$this
->assertSession()
->linkExists('Log in');
\Drupal::state()
->set('cas_test.flag', 'cancel register with message');
$this
->casLogin('antoine@example.com', 'baTistE');
$this
->assertSession()
->pageTextContains('Cancelled with a custom message.');
$this
->assertSession()
->linkExists('Log in');
}
}