View source
<?php
namespace Drupal\Tests\cas\Unit\Service;
use Drupal\cas\Event\CasPreLoginEvent;
use Drupal\cas\Event\CasPreRegisterEvent;
use Drupal\cas\Service\CasProxyHelper;
use Drupal\cas\Service\CasUserManager;
use Drupal\Tests\UnitTestCase;
use Drupal\cas\CasPropertyBag;
class CasUserManagerTest extends UnitTestCase {
protected $externalAuth;
protected $authmap;
protected $entityManager;
protected $session;
protected $connection;
protected $eventDispatcher;
protected $userManager;
protected $casHelper;
protected $casProxyHelper;
protected $account;
protected function setUp() : void {
parent::setUp();
$this->externalAuth = $this
->getMockBuilder('\\Drupal\\externalauth\\ExternalAuth')
->disableOriginalConstructor()
->getMock();
$this->authmap = $this
->getMockBuilder('\\Drupal\\externalauth\\Authmap')
->disableOriginalConstructor()
->getMock();
$storage = $this
->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session\\Storage\\MockArraySessionStorage')
->setMethods(NULL)
->getMock();
$this->session = $this
->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session\\Session')
->setConstructorArgs([
$storage,
])
->getMock();
$this->session
->start();
$this->connection = $this
->getMockBuilder('\\Drupal\\Core\\Database\\Connection')
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher = $this
->getMockBuilder('\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
->disableOriginalConstructor()
->getMock();
$this->casHelper = $this
->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')
->disableOriginalConstructor()
->getMock();
$this->account = $this
->getMockBuilder('Drupal\\user\\UserInterface')
->disableOriginalConstructor()
->getMock();
$this->casProxyHelper = $this
->prophesize(CasProxyHelper::class);
}
public function testUserRegister() {
$config_factory = $this
->getConfigFactoryStub([
'cas.settings' => [
'user_accounts.auto_assigned_roles' => [],
],
]);
$this->externalAuth
->method('register')
->willReturn($this->account);
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'randomPassword',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$config_factory,
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this
->assertNotEmpty($cas_user_manager
->register('test', 'test', []), 'Successfully registered user.');
}
public function testUserNotFoundAndAutoRegistrationDisabled() {
$config_factory = $this
->getConfigFactoryStub([
'cas.settings' => [
'user_accounts.auto_register' => FALSE,
],
]);
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
'register',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$config_factory,
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->externalAuth
->method('load')
->willReturn(FALSE);
$cas_user_manager
->expects($this
->never())
->method('register');
$this->externalAuth
->expects($this
->never())
->method('userLoginFinalize');
$this
->expectException('Drupal\\cas\\Exception\\CasLoginException', 'Cannot login, local Drupal user account does not exist.');
$cas_user_manager
->login(new CasPropertyBag('test'), 'ticket');
}
public function testUserNotFoundAndEventListenerDeniesAutoRegistration() {
$config_factory = $this
->getConfigFactoryStub([
'cas.settings' => [
'user_accounts.auto_register' => TRUE,
'user_accounts.email_assignment_strategy' => CasUserManager::EMAIL_ASSIGNMENT_STANDARD,
'user_accounts.email_hostname' => 'sample.com',
],
]);
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
'register',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$config_factory,
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->externalAuth
->method('load')
->willReturn(FALSE);
$this->eventDispatcher
->method('dispatch')
->willReturnCallback(function ($event_type, $event) {
if ($event instanceof CasPreRegisterEvent) {
$event
->cancelAutomaticRegistration();
}
});
$cas_user_manager
->expects($this
->never())
->method('register');
$this->externalAuth
->expects($this
->never())
->method('userLoginFinalize');
$this
->expectException('Drupal\\cas\\Exception\\CasLoginException');
$this
->expectExceptionMessage('Cannot register user, an event listener denied access.');
$cas_user_manager
->login(new CasPropertyBag('test'), 'ticket');
}
public function testUserNotFoundAndEventListenerAllowAutoRegistration() {
$config_factory = $this
->getConfigFactoryStub([
'cas.settings' => [
'user_accounts.auto_register' => TRUE,
'user_accounts.email_assignment_strategy' => CasUserManager::EMAIL_ASSIGNMENT_STANDARD,
'user_accounts.email_hostname' => 'sample.com',
'user_accounts.email_attribute' => 'email',
],
]);
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
'randomPassword',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$config_factory,
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$expected_assigned_email = 'test@sample.com';
$this->externalAuth
->method('load')
->willReturn(FALSE);
$this->account
->method('isactive')
->willReturn(TRUE);
$this->eventDispatcher
->method('dispatch')
->willReturnCallback(function ($event_type, $event) {
if ($event instanceof CasPreRegisterEvent) {
$event
->allowAutomaticRegistration();
}
});
$this->externalAuth
->expects($this
->once())
->method('register')
->with('test', 'cas', [
'name' => 'test',
'mail' => $expected_assigned_email,
'pass' => NULL,
])
->willReturn($this->account);
$this->externalAuth
->expects($this
->once())
->method('userLoginFinalize')
->willReturn($this->account);
$cas_property_bag = new CasPropertyBag('test');
$cas_property_bag
->setAttributes([
'email' => 'test@sample.com',
]);
$cas_user_manager
->login($cas_property_bag, 'ticket');
}
public function testAutomaticRegistration($email_assignment_strategy) {
$config_factory = $this
->getConfigFactoryStub([
'cas.settings' => [
'user_accounts.auto_register' => TRUE,
'user_accounts.email_assignment_strategy' => $email_assignment_strategy,
'user_accounts.email_hostname' => 'sample.com',
'user_accounts.email_attribute' => 'email',
],
]);
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
'randomPassword',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$config_factory,
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->externalAuth
->method('load')
->willReturn(FALSE);
$this->account
->method('isactive')
->willReturn(TRUE);
if ($email_assignment_strategy === CasUserManager::EMAIL_ASSIGNMENT_STANDARD) {
$expected_assigned_email = 'test@sample.com';
}
else {
$expected_assigned_email = 'test_email@foo.com';
}
$this->externalAuth
->expects($this
->once())
->method('register')
->with('test', 'cas', [
'name' => 'test',
'mail' => $expected_assigned_email,
'pass' => NULL,
])
->willReturn($this->account);
$this->externalAuth
->expects($this
->once())
->method('userLoginFinalize')
->willReturn($this->account);
$cas_property_bag = new CasPropertyBag('test');
$cas_property_bag
->setAttributes([
'email' => 'test_email@foo.com',
]);
$cas_user_manager
->login($cas_property_bag, 'ticket');
}
public function automaticRegistrationDataProvider() {
return [
[
CasUserManager::EMAIL_ASSIGNMENT_STANDARD,
],
[
CasUserManager::EMAIL_ASSIGNMENT_ATTRIBUTE,
],
];
}
public function testEventListenerPreventsLogin() {
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$this
->getConfigFactoryStub(),
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->account
->method('isactive')
->willReturn(TRUE);
$this->externalAuth
->method('load')
->willReturn($this->account);
$this->eventDispatcher
->method('dispatch')
->willReturnCallback(function ($event_type, $event) {
if ($event instanceof CasPreLoginEvent) {
$event
->cancelLogin();
}
});
$cas_user_manager
->expects($this
->never())
->method('storeLoginSessionData');
$this->externalAuth
->expects($this
->never())
->method('userLoginFinalize');
$this
->expectException('Drupal\\cas\\Exception\\CasLoginException', 'Cannot login, an event listener denied access.');
$cas_user_manager
->login(new CasPropertyBag('test'), 'ticket');
}
public function testExistingAccountIsLoggedIn() {
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$this
->getConfigFactoryStub(),
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->account
->method('isActive')
->willReturn(TRUE);
$this->externalAuth
->method('load')
->willReturn($this->account);
$cas_user_manager
->expects($this
->once())
->method('storeLoginSessionData');
$this->externalAuth
->expects($this
->once())
->method('userLoginFinalize')
->willReturn($this->account);
$attributes = [
'attr1' => 'foo',
'attr2' => 'bar',
];
$this->session
->method('set')
->withConsecutive([
'is_cas_user',
TRUE,
], [
'cas_username',
'test',
]);
$propertyBag = new CasPropertyBag('test');
$propertyBag
->setAttributes($attributes);
$cas_user_manager
->login($propertyBag, 'ticket');
}
public function testBlockedAccountIsNotLoggedIn() {
$cas_user_manager = $this
->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
->setMethods([
'storeLoginSessionData',
])
->setConstructorArgs([
$this->externalAuth,
$this->authmap,
$this
->getConfigFactoryStub(),
$this->session,
$this->connection,
$this->eventDispatcher,
$this->casHelper,
$this->casProxyHelper
->reveal(),
])
->getMock();
$this->account
->method('isactive')
->willReturn(FALSE);
$this->account
->method('getaccountname')
->willReturn('user');
$this->externalAuth
->method('load')
->willReturn($this->account);
$this->externalAuth
->expects($this
->never())
->method('userLoginFinalize');
$this
->expectException('Drupal\\cas\\Exception\\CasLoginException', 'The username user has not been activated or is blocked.');
$this->session
->method('set')
->withConsecutive([
'is_cas_user',
TRUE,
], [
'cas_username',
'test',
]);
$propertyBag = new CasPropertyBag('test');
$cas_user_manager
->login($propertyBag, 'ticket');
}
}