View source
<?php
namespace Drupal\Tests\tfa\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Tests\UnitTestCase;
use Drupal\tfa\Plugin\TfaValidationInterface;
use Drupal\tfa\TfaContext;
use Drupal\tfa\TfaLoginPluginManager;
use Drupal\tfa\TfaValidationPluginManager;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\Request;
class TfaContextTest extends UnitTestCase {
protected $tfaValidationManager;
protected $tfaLoginManager;
protected $configFactory;
protected $tfaSettings;
protected $user;
protected $userData;
protected $request;
protected function setUp() : void {
parent::setUp();
$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();
}
protected function getFixture() {
return new TfaContext($this->tfaValidationManager, $this->tfaLoginManager, $this->configFactory, $this->user, $this->userData, $this->request);
}
public function testGetUser() {
$fixture = $this
->getFixture();
$this
->assertEquals(3, $fixture
->getUser()
->id());
}
public function testIsModuleSetup() {
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->isModuleSetup());
$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());
}
public function testIsTfaRequired() {
$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());
$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());
$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());
}
public function testIsReady() {
$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());
$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());
$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());
}
public function testRemainingSkips() {
$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());
$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());
$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_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());
}
}