View source
<?php
namespace Drupal\Tests\simplesamlphp_auth\Unit\Service;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager;
use Drupal\simplesamlphp_auth\Exception\SimplesamlphpAttributeException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use SimpleSAML\Auth\Simple;
use SimpleSAML\Configuration;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
class SimplesamlphpAuthManagerTest extends UnitTestCase {
protected $configFactory;
protected $simplesamlConfig;
public $instance;
protected $currentUser;
protected $adminContext;
protected $moduleHandler;
protected $requestStack;
protected $messenger;
protected function setUp() {
parent::setUp();
$this->configFactory = $this
->getConfigFactoryStub([
'simplesamlphp_auth.settings' => [
'auth_source' => 'default-sp',
'register_users' => TRUE,
'activate' => 1,
'user_name' => 'name',
'mail_attr' => 'mail',
'unique_id' => 'uid',
],
]);
$this->instance = $this
->getMockBuilder(Simple::class)
->setMethods([
'isAuthenticated',
'requireAuth',
'getAttributes',
'logout',
])
->disableOriginalConstructor()
->getMock();
$this->currentUser = $this
->getMockBuilder(AccountInterface::class)
->disableOriginalConstructor()
->getMock();
$this->adminContext = $this
->getMockBuilder(AdminContext::class)
->disableOriginalConstructor()
->getMock();
$this->moduleHandler = $this
->getMockBuilder(ModuleHandlerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->moduleHandler
->expects($this
->any())
->method('getImplementations')
->with($this
->equalTo('simplesamlphp_auth_allow_login'))
->will($this
->returnValue([]));
$this->requestStack = $this
->getMockBuilder(RequestStack::class)
->disableOriginalConstructor()
->getMock();
$this->messenger = $this
->getMockBuilder(MessengerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->simplesamlConfig = $this
->getMockBuilder(Configuration::class)
->setMethods([
'getValue',
])
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$request = $this
->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$this->requestStack
->expects($this
->any())
->method('getCurrentRequest')
->will($this
->returnValue($request));
$container
->set('request_stack', $this->requestStack);
\Drupal::setContainer($container);
}
protected function getManagerInContext() {
return new SimplesamlphpAuthManager($this->configFactory, $this->currentUser, $this->adminContext, $this->moduleHandler, $this->requestStack, $this->messenger, $this->instance, $this->simplesamlConfig);
}
public function testIsActivated() {
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->isActivated();
$this
->assertTrue($return);
}
public function testIsAuthenticated() {
$this->instance
->expects($this
->once())
->method('isAuthenticated')
->will($this
->returnValue(TRUE));
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->isAuthenticated();
$this
->assertTrue($return);
}
public function testExternalAuthenticate() {
$this->instance
->expects($this
->once())
->method('requireAuth');
$simplesaml = $this
->getManagerInContext();
$simplesaml
->externalAuthenticate();
}
public function testGetStorage() {
$this->simplesamlConfig
->expects($this
->any())
->method('getValue')
->with($this
->equalTo('store.type'))
->will($this
->returnValue('sql'));
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->getStorage();
$this
->assertEquals('sql', $return);
}
public function testAttributes() {
$data = [
'uid' => [
'ext_user_123',
],
'name' => [
'External User',
],
'mail' => [
'ext_user_123@example.com',
],
'roles' => [
[
'employee',
'webmaster',
],
],
];
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue($data));
$simplesaml = $this
->getManagerInContext();
$this
->assertEquals('ext_user_123', $simplesaml
->getAuthname());
$this
->assertEquals('External User', $simplesaml
->getDefaultName());
$this
->assertEquals('ext_user_123@example.com', $simplesaml
->getDefaultEmail());
$this
->assertEquals([
'employee',
'webmaster',
], $simplesaml
->getAttribute('roles'));
}
public function testAttributesException() {
$this
->expectException(SimplesamlphpAttributeException::class);
$this
->expectExceptionMessage('Error in simplesamlphp_auth.module: no valid "name" attribute set.');
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue([
'uid' => [
'ext_user_123',
],
]));
$simplesaml = $this
->getManagerInContext();
$simplesaml
->getAttribute('name');
}
public function testAllowUserByAttribute() {
$data = [
'uid' => [
'ext_user_123',
],
'name' => [
'External User',
],
'mail' => [
'ext_user_123@example.com',
],
'roles' => [
[
'employee',
'webmaster',
],
],
];
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue($data));
$simplesaml = $this
->getManagerInContext();
$this
->assertTrue($simplesaml
->allowUserByAttribute());
}
public function testLogout() {
$redirect_path = '<front>';
$this->instance
->expects($this
->once())
->method('logout')
->with($this
->equalTo($redirect_path));
$simplesaml = $this
->getManagerInContext();
$simplesaml
->logout($redirect_path);
}
}