class SimplesamlphpAuthManagerTest in simpleSAMLphp Authentication 8.3
SimplesamlphpAuthManager unit tests.
@group simplesamlphp_auth
@coversDefaultClass \Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\simplesamlphp_auth\Unit\Service\SimplesamlphpAuthManagerTest
Expanded class hierarchy of SimplesamlphpAuthManagerTest
File
- tests/
src/ Unit/ Service/ SimplesamlphpAuthManagerTest.php, line 27
Namespace
Drupal\Tests\simplesamlphp_auth\Unit\ServiceView source
class SimplesamlphpAuthManagerTest extends UnitTestCase {
/**
* A mocked config factory instance.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configFactory;
/**
* A mocked SimpleSAML configuration instance.
*
* @var \SimpleSAML\Configuration|\PHPUnit_Framework_MockObject_MockObject
*/
protected $simplesamlConfig;
/**
* A mocked SimpleSAML instance.
*
* @var \SimpleSAML\Auth\Simple|\PHPUnit_Framework_MockObject_MockObject
*/
public $instance;
/**
* A mocked current user.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currentUser;
/**
* A mocked AdminContext.
*
* @var \Drupal\Core\Routing\AdminContext|\PHPUnit_Framework_MockObject_MockObject
*/
protected $adminContext;
/**
* A mocked ModuleHandlerInterface.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* A mocked RequestStack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit_Framework_MockObject_MockObject
*/
protected $requestStack;
/**
* A mocked messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $messenger;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Set up default test configuration Mock object.
$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);
}
/**
* Get a new manager instance using mocked constructor arguments.
*
* @return \Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager
* A mocked manager.
*/
protected function getManagerInContext() {
return new SimplesamlphpAuthManager($this->configFactory, $this->currentUser, $this->adminContext, $this->moduleHandler, $this->requestStack, $this->messenger, $this->instance, $this->simplesamlConfig);
}
/**
* Tests isActivated() method.
*
* @covers ::__construct
* @covers ::isActivated
*/
public function testIsActivated() {
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->isActivated();
$this
->assertTrue($return);
}
/**
* Tests isAuthenticated() method.
*
* @covers ::__construct
* @covers ::isAuthenticated
*/
public function testIsAuthenticated() {
// Set expectations for instance.
$this->instance
->expects($this
->once())
->method('isAuthenticated')
->will($this
->returnValue(TRUE));
// Test isAuthenticated() method.
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->isAuthenticated();
$this
->assertTrue($return);
}
/**
* Tests externalAuthenticate() method.
*
* @covers ::__construct
* @covers ::externalAuthenticate
*/
public function testExternalAuthenticate() {
// Set expectations for instance.
$this->instance
->expects($this
->once())
->method('requireAuth');
// Test externalAuthenticate() method.
$simplesaml = $this
->getManagerInContext();
$simplesaml
->externalAuthenticate();
}
/**
* Tests getStorage() method.
*
* @covers ::__construct
* @covers ::getStorage
*/
public function testGetStorage() {
// Set expectations for config.
$this->simplesamlConfig
->expects($this
->any())
->method('getValue')
->with($this
->equalTo('store.type'))
->will($this
->returnValue('sql'));
// Test getStorage() method.
$simplesaml = $this
->getManagerInContext();
$return = $simplesaml
->getStorage();
$this
->assertEquals('sql', $return);
}
/**
* Tests attributes assignment logic.
*
* @covers ::__construct
* @covers ::getAttributes
* @covers ::getAttribute
* @covers ::getAuthname
* @covers ::getDefaultName
* @covers ::getDefaultEmail
*/
public function testAttributes() {
$data = [
'uid' => [
'ext_user_123',
],
'name' => [
'External User',
],
'mail' => [
'ext_user_123@example.com',
],
'roles' => [
[
'employee',
'webmaster',
],
],
];
// Set expectations for instance.
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue($data));
// Test attribute methods.
$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'));
}
/**
* Tests attribute assignment logic throwing exceptions.
*
* @covers ::__construct
* @covers ::getAttribute
*/
public function testAttributesException() {
$this
->expectException(SimplesamlphpAttributeException::class);
$this
->expectExceptionMessage('Error in simplesamlphp_auth.module: no valid "name" attribute set.');
// Set expectations for instance.
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue([
'uid' => [
'ext_user_123',
],
]));
$simplesaml = $this
->getManagerInContext();
$simplesaml
->getAttribute('name');
}
/**
* Tests allowUserByAttribute() method.
*
* @covers ::__construct
* @covers ::allowUserByAttribute
*/
public function testAllowUserByAttribute() {
$data = [
'uid' => [
'ext_user_123',
],
'name' => [
'External User',
],
'mail' => [
'ext_user_123@example.com',
],
'roles' => [
[
'employee',
'webmaster',
],
],
];
// Set expectations for instance.
$this->instance
->expects($this
->any())
->method('getAttributes')
->will($this
->returnValue($data));
// Test allowUserByAttribute method.
$simplesaml = $this
->getManagerInContext();
$this
->assertTrue($simplesaml
->allowUserByAttribute());
}
/**
* Tests logout() method.
*
* @covers ::__construct
* @covers ::logout
*/
public function testLogout() {
$redirect_path = '<front>';
// Set expectations for instance.
$this->instance
->expects($this
->once())
->method('logout')
->with($this
->equalTo($redirect_path));
// Test logout() method.
$simplesaml = $this
->getManagerInContext();
$simplesaml
->logout($redirect_path);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked AdminContext. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked config factory instance. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked current user. | |
SimplesamlphpAuthManagerTest:: |
public | property | A mocked SimpleSAML instance. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked messenger. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked ModuleHandlerInterface. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked RequestStack. | |
SimplesamlphpAuthManagerTest:: |
protected | property | A mocked SimpleSAML configuration instance. | |
SimplesamlphpAuthManagerTest:: |
protected | function | Get a new manager instance using mocked constructor arguments. | |
SimplesamlphpAuthManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SimplesamlphpAuthManagerTest:: |
public | function | Tests allowUserByAttribute() method. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests attributes assignment logic. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests attribute assignment logic throwing exceptions. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests externalAuthenticate() method. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests getStorage() method. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests isActivated() method. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests isAuthenticated() method. | |
SimplesamlphpAuthManagerTest:: |
public | function | Tests logout() method. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |