class TfaContextTest in Two-factor Authentication (TFA) 8
@coversDefaultClass \Drupal\tfa\TfaContext
@group tfa
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\tfa\Unit\TfaContextTest
Expanded class hierarchy of TfaContextTest
File
- tests/
src/ Unit/ TfaContextTest.php, line 21
Namespace
Drupal\Tests\tfa\UnitView source
class TfaContextTest extends UnitTestCase {
/**
* Validation plugin manager.
*
* @var \Drupal\tfa\TfaValidationPluginManager
*/
protected $tfaValidationManager;
/**
* Login plugin manager.
*
* @var \Drupal\tfa\TfaLoginPluginManager
*/
protected $tfaLoginManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Tfa settings config object.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $tfaSettings;
/**
* Entity for the user that is attempting to login.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* User data service.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* Current request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Setup default mocked services. These can be overridden by
// re-instantiating them as needed prior to calling ::getFixture().
$this->tfaValidationManager = $this
->prophesize(TfaValidationPluginManager::class)
->reveal();
$this->tfaLoginManager = $this
->prophesize(TfaLoginPluginManager::class)
->reveal();
$this->tfaSettings = $this
->prophesize(ImmutableConfig::class)
->reveal();
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
$this->configFactory
->get('tfa.settings')
->willReturn($this->tfaSettings);
$this->configFactory = $this->configFactory
->reveal();
$this->user = $this
->prophesize(UserInterface::class);
$this->user
->id()
->willReturn(3);
$this->user = $this->user
->reveal();
$this->userData = $this
->prophesize(UserDataInterface::class)
->reveal();
$this->request = $this
->prophesize(Request::class)
->reveal();
}
/**
* Helper method to instantiate the test fixture.
*
* @return \Drupal\tfa\TfaContext
* TFA context.
*/
protected function getFixture() {
return new TfaContext($this->tfaValidationManager, $this->tfaLoginManager, $this->configFactory, $this->user, $this->userData, $this->request);
}
/**
* @covers ::getUser
*/
public function testGetUser() {
$fixture = $this
->getFixture();
$this
->assertEquals(3, $fixture
->getUser()
->id());
}
/**
* @covers ::isModuleSetup
*/
public function testIsModuleSetup() {
// Defaults to false with empty mocked services.
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->isModuleSetup());
// Enable.
$settings = $this
->prophesize(ImmutableConfig::class);
$settings
->get('enabled')
->willReturn(TRUE);
$settings
->get('default_validation_plugin')
->willReturn('foo');
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$fixture = $this
->getFixture();
$this
->assertTrue($fixture
->isModuleSetup());
}
/**
* @covers ::isTfaRequired
*/
public function testIsTfaRequired() {
// User has setup TFA.
$user_data = $this
->prophesize(UserDataInterface::class);
$user_data
->get('tfa', 3, 'tfa_user_settings')
->willReturn([
'status' => 1,
'saved' => FALSE,
'data' => [
'plugins' => [
'foo',
],
],
'validation_skipped' => 1,
]);
$this->userData = $user_data
->reveal();
$fixture = $this
->getFixture();
$this
->assertTrue($fixture
->isTfaRequired());
// Not setup, no required roles matching the user.
$user_data
->get('tfa', 3, 'tfa_user_settings')
->willReturn([
'status' => 0,
'saved' => FALSE,
'data' => [
'plugins' => [
'foo',
],
],
'validation_skipped' => 1,
]);
$this->userData = $user_data
->reveal();
$settings = $this
->prophesize(ImmutableConfig::class);
$settings
->get('default_validation_plugin')
->willReturn('foo');
$settings
->get('required_roles')
->willReturn([
'foo' => 'foo',
]);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$user = $this
->prophesize(UserInterface::class);
$user
->id()
->willReturn(3);
$user
->getRoles()
->willReturn([
'bar' => 'bar',
]);
$this->user = $user
->reveal();
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->isTfaRequired());
// Setup, matching roles.
$user_data
->get('tfa', 3, 'tfa_user_settings')
->willReturn([
'status' => 1,
'saved' => FALSE,
'data' => [
'plugins' => [
'foo',
],
],
'validation_skipped' => 1,
]);
$this->userData = $user_data
->reveal();
$user = $this
->prophesize(UserInterface::class);
$user
->id()
->willReturn(3);
$user
->getRoles()
->willReturn([
'foo' => 'foo',
'bar' => 'bar',
]);
$this->user = $user
->reveal();
$fixture = $this
->getFixture();
$this
->assertTrue($fixture
->isTfaRequired());
}
/**
* @covers ::isReady
*/
public function testIsReady() {
// Not ready.
$settings = $this
->prophesize(ImmutableConfig::class);
$settings
->get('default_validation_plugin')
->willReturn(FALSE);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->isReady());
// Is ready.
$settings
->get('default_validation_plugin')
->willReturn('foo');
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$validator = $this
->prophesize(TfaValidationInterface::class);
$validator
->ready()
->willReturn(TRUE);
$manager = $this
->prophesize(TfaValidationPluginManager::class);
$manager
->createInstance('foo', [
'uid' => 3,
])
->willReturn($validator
->reveal());
$this->tfaValidationManager = $manager
->reveal();
$fixture = $this
->getFixture();
$this
->assertTrue($fixture
->isReady());
// Plugin set, but not ready.
$validator = $this
->prophesize(TfaValidationInterface::class);
$validator
->ready()
->willReturn(FALSE);
$manager = $this
->prophesize(TfaValidationPluginManager::class);
$manager
->createInstance('foo', [
'uid' => 3,
])
->willReturn($validator
->reveal());
$this->tfaValidationManager = $manager
->reveal();
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->isReady());
}
/**
* @covers ::remainingSkips
*/
public function testRemainingSkips() {
// No allowed skips.
$settings = $this
->prophesize(ImmutableConfig::class);
$settings
->get('default_validation_plugin')
->willReturn(FALSE);
$settings
->get('validation_skip')
->willReturn(0);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->remainingSkips());
// 3 allowed skips, user hasn't skipped any.
$settings
->get('validation_skip')
->willReturn(3);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('tfa.settings')
->willReturn($settings
->reveal());
$this->configFactory = $config_factory
->reveal();
$fixture = $this
->getFixture();
$this
->assertEquals(3, $fixture
->remainingSkips());
// 3 allowed skips, user has skipped 2.
$user_data = $this
->prophesize(UserDataInterface::class);
$user_data
->get('tfa', 3, 'tfa_user_settings')
->willReturn([
'status' => 1,
'saved' => FALSE,
'data' => [
'plugins' => [
'foo',
],
],
'validation_skipped' => 2,
]);
$this->userData = $user_data
->reveal();
$fixture = $this
->getFixture();
$this
->assertEquals(1, $fixture
->remainingSkips());
// User has exceeded attempts, check for 0 return.
$user_data
->get('tfa', 3, 'tfa_user_settings')
->willReturn([
'status' => 1,
'saved' => FALSE,
'data' => [
'plugins' => [
'foo',
],
],
'validation_skipped' => 9,
]);
$this->userData = $user_data
->reveal();
$fixture = $this
->getFixture();
$this
->assertEquals(0, $fixture
->remainingSkips());
}
}
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. | |
TfaContextTest:: |
protected | property | The config factory. | |
TfaContextTest:: |
protected | property | Current request object. | |
TfaContextTest:: |
protected | property | Login plugin manager. | |
TfaContextTest:: |
protected | property | Tfa settings config object. | |
TfaContextTest:: |
protected | property | Validation plugin manager. | |
TfaContextTest:: |
protected | property | Entity for the user that is attempting to login. | |
TfaContextTest:: |
protected | property | User data service. | |
TfaContextTest:: |
protected | function | Helper method to instantiate the test fixture. | |
TfaContextTest:: |
protected | function |
Overrides UnitTestCase:: |
|
TfaContextTest:: |
public | function | @covers ::getUser | |
TfaContextTest:: |
public | function | @covers ::isModuleSetup | |
TfaContextTest:: |
public | function | @covers ::isReady | |
TfaContextTest:: |
public | function | @covers ::isTfaRequired | |
TfaContextTest:: |
public | function | @covers ::remainingSkips | |
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. |