View source
<?php
namespace Drupal\Tests\tfa\Unit\Plugin\TfaValidation;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Form\FormState;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\encrypt\EncryptionProfileManagerInterface;
use Drupal\encrypt\EncryptServiceInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\tfa\Plugin\TfaValidation\TfaRecoveryCode;
use Drupal\user\UserDataInterface;
class TfaRecoveryCodeTest extends UnitTestCase {
protected $userData;
protected $encryptionProfileManager;
protected $encryptionService;
protected $configFactory;
protected $tfaSettings;
protected $encryptionProfile;
protected $configuration = [
'uid' => 3,
];
protected function setUp() : void {
parent::setUp();
$this->userData = $this
->prophesize(UserDataInterface::class);
$this->encryptionProfileManager = $this
->prophesize(EncryptionProfileManagerInterface::class);
$this->encryptionService = $this
->prophesize(EncryptServiceInterface::class);
$this->tfaSettings = $this
->prophesize(ImmutableConfig::class);
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
$this->encryptionProfile = $this
->prophesize(EncryptionProfileInterface::class);
}
protected function getFixture() {
$this->configFactory
->get('tfa.settings')
->willReturn($this->tfaSettings
->reveal());
$container = new ContainerBuilder();
$container
->set('config.factory', $this->configFactory
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($container);
return new TfaRecoveryCode($this->configuration, 'tfa_recovery_code', [], $this->userData
->reveal(), $this->encryptionProfileManager
->reveal(), $this->encryptionService
->reveal(), $container
->get('config.factory'));
}
public function testReadyCodesValidate() {
$fixture = $this
->getFixture();
$this
->assertFalse($fixture
->ready());
$this->userData
->get('tfa', 3, 'tfa_recovery_code')
->willReturn([
'foo',
'bar',
]);
$this->encryptionService
->decrypt('foo', $this->encryptionProfile
->reveal())
->willReturn('foo_decrypted');
$this->encryptionService
->decrypt('bar', $this->encryptionProfile
->reveal())
->willReturn('bar_decrypted');
$this->tfaSettings
->get('validation_plugin_settings.tfa_recovery_code.recovery_codes_amount')
->willReturn(10);
$this->tfaSettings
->get('encryption')
->willReturn('foo');
$this->tfaSettings
->get('default_validation_plugin')
->willReturn('bar');
$this->encryptionProfileManager
->getEncryptionProfile('foo')
->willReturn($this->encryptionProfile
->reveal());
$fixture = $this
->getFixture();
$this
->assertTrue($fixture
->ready());
$this
->assertEquals([
'foo_decrypted',
'bar_decrypted',
], $fixture
->getCodes());
$this->userData
->delete('tfa', 3, 'tfa_recovery_code')
->shouldBeCalled();
$fixture = $this
->getFixture();
$form_state = new FormState();
$form_state
->setValues([
'code' => 'bad_code',
]);
$this
->assertFalse($fixture
->validateForm([], $form_state));
$this
->assertCount(1, $fixture
->getErrorMessages());
$this->encryptionService
->encrypt('bar_decrypted', $this->encryptionProfile
->reveal())
->willReturn('bar');
$this->userData
->set('tfa', 3, 'tfa_recovery_code', [
1 => 'bar',
])
->shouldBeCalled();
$fixture = $this
->getFixture();
$form_state
->setValues([
'code' => 'foo_decrypted',
]);
$this
->assertTrue($fixture
->validateForm([], $form_state));
$this
->assertEmpty($fixture
->getErrorMessages());
}
}