View source
<?php
namespace Drupal\Tests\encrypt\Unit;
use Drupal\encrypt\EncryptionMethodInterface;
use Drupal\encrypt\EncryptionMethodManager;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\encrypt\Exception\EncryptionMethodCanNotDecryptException;
use Drupal\key\Entity\Key;
use Drupal\key\KeyRepositoryInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\encrypt\EncryptService;
class EncryptServiceTest extends UnitTestCase {
protected $defaultConfig = [
'encrypt.settings' => [
'check_profile_status' => TRUE,
'allow_deprecated_plugins' => FALSE,
],
];
protected $encryptionProfile;
protected $encryptManager;
protected $keyRepository;
protected $encryptionMethod;
protected $key;
protected function setUp() {
parent::setUp();
$this->encryptionProfile = $this
->prophesize(EncryptionProfileInterface::class);
$this->encryptManager = $this
->prophesize(EncryptionMethodManager::class);
$this->keyRepository = $this
->prophesize(KeyRepositoryInterface::class);
$this->encryptionMethod = $this
->prophesize(EncryptionMethodInterface::class);
$this->key = $this
->prophesize(Key::class);
}
public function testLoadEncryptionMethods() {
$definitions = [
'test_encryption_method' => [
'id' => 'test_encryption_method',
'title' => "Test encryption method",
],
];
$this->encryptManager
->getDefinitions()
->willReturn($definitions);
$service = new EncryptService($this->encryptManager
->reveal(), $this->keyRepository
->reveal(), $this
->getConfigFactoryStub($this->defaultConfig));
$methods = $service
->loadEncryptionMethods();
$this
->assertEquals([
'test_encryption_method',
], array_keys($methods));
}
public function testEncryptDecrypt($key, $valid_key) {
$this->key
->getKeyValue()
->willReturn($key);
$this->encryptionMethod
->canDecrypt()
->willReturn(TRUE);
if ($valid_key) {
$this->encryptionMethod
->encrypt('text_to_encrypt', 'validkey')
->willReturn('encrypted_text');
$this->encryptionMethod
->decrypt('text_to_decrypt', 'validkey')
->willReturn('decrypted_text');
$this->encryptionProfile
->getEncryptionKey()
->willReturn($this->key);
$this->encryptionProfile
->getEncryptionMethod()
->willReturn($this->encryptionMethod);
$this->encryptionProfile
->validate('text_to_encrypt')
->willReturn([]);
$this->encryptionProfile
->validate('text_to_decrypt')
->willReturn([]);
}
else {
$this->encryptionProfile
->getEncryptionKey()
->shouldNotBeCalled();
$this->encryptionProfile
->getEncryptionMethod()
->shouldNotBeCalled();
$this->encryptionProfile
->validate('text_to_encrypt')
->willReturn([
'Validation',
]);
$this->encryptionProfile
->validate('text_to_decrypt')
->willReturn([
'Validation',
]);
$this
->expectException('\\Drupal\\encrypt\\Exception\\EncryptException');
}
$service = new EncryptService($this->encryptManager
->reveal(), $this->keyRepository
->reveal(), $this
->getConfigFactoryStub($this->defaultConfig));
$encrypted_text = $service
->encrypt("text_to_encrypt", $this->encryptionProfile
->reveal());
$decrypted_text = $service
->decrypt("text_to_decrypt", $this->encryptionProfile
->reveal());
if ($valid_key) {
$this
->assertEquals("encrypted_text", $encrypted_text);
$this
->assertEquals("decrypted_text", $decrypted_text);
}
}
public function testEncryptionOnlyMethods() {
$this->key
->getKeyValue()
->willReturn('my-key');
$this->encryptionMethod
->encrypt('text_to_encrypt', 'my-key')
->willReturn('encrypted_text');
$this->encryptionMethod
->canDecrypt()
->willReturn(FALSE);
$this->encryptionMethod
->decrypt()
->shouldNotBeCalled();
$this->encryptionProfile
->getEncryptionKey()
->willReturn($this->key);
$this->encryptionProfile
->getEncryptionMethod()
->willReturn($this->encryptionMethod);
$this->encryptionProfile
->validate('text_to_encrypt')
->willReturn([]);
$service = new EncryptService($this->encryptManager
->reveal(), $this->keyRepository
->reveal(), $this
->getConfigFactoryStub($this->defaultConfig));
$encrypted_text = $service
->encrypt("text_to_encrypt", $this->encryptionProfile
->reveal());
$this
->assertEquals("encrypted_text", $encrypted_text);
$this
->expectException(EncryptionMethodCanNotDecryptException::class);
$service
->decrypt($encrypted_text, $this->encryptionProfile
->reveal());
}
public function encryptionDataProvider() {
return [
'normal' => [
"validkey",
TRUE,
],
'exception' => [
"invalidkey",
FALSE,
],
];
}
}