You are here

public function EncryptServiceTest::testEncryptionOnlyMethods in Encrypt 8.3

Test exception is thrown trying to decrypt an encryption-only method.

@covers ::decrypt

File

tests/src/Unit/EncryptServiceTest.php, line 176

Class

EncryptServiceTest
Unit tests for EncryptService class.

Namespace

Drupal\Tests\encrypt\Unit

Code

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();

  // Set up expectations for encryption profile.
  $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);

  // Assert exception is thrown when a method can NOT decrypt.
  $this
    ->expectException(EncryptionMethodCanNotDecryptException::class);
  $service
    ->decrypt($encrypted_text, $this->encryptionProfile
    ->reveal());
}