You are here

class EncryptServiceTest in Encrypt 8.3

Unit tests for EncryptService class.

@group encrypt

@coversDefaultClass \Drupal\encrypt\EncryptService

Hierarchy

Expanded class hierarchy of EncryptServiceTest

File

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

Namespace

Drupal\Tests\encrypt\Unit
View source
class EncryptServiceTest extends UnitTestCase {

  /**
   * Default configuration.
   *
   * @var array[]
   */
  protected $defaultConfig = [
    'encrypt.settings' => [
      'check_profile_status' => TRUE,
      'allow_deprecated_plugins' => FALSE,
    ],
  ];

  /**
   * A mocked EncryptionProfile entity.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $encryptionProfile;

  /**
   * A mocked EncryptionMethodManager.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $encryptManager;

  /**
   * A mocked KeyRepository.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $keyRepository;

  /**
   * A mocked EncryptionMethod plugin.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $encryptionMethod;

  /**
   * A mocked Key entity.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $key;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Set up a mock EncryptionProfile entity.
    $this->encryptionProfile = $this
      ->prophesize(EncryptionProfileInterface::class);

    // Set up a mock EncryptionMethodManager.
    $this->encryptManager = $this
      ->prophesize(EncryptionMethodManager::class);

    // Set up a mock KeyRepository.
    $this->keyRepository = $this
      ->prophesize(KeyRepositoryInterface::class);

    // Set up a mock EncryptionMethod plugin.
    $this->encryptionMethod = $this
      ->prophesize(EncryptionMethodInterface::class);

    // Set up a mock Key entity.
    $this->key = $this
      ->prophesize(Key::class);
  }

  /**
   * Tests loadEncryptionMethods method.
   *
   * @covers ::__construct
   * @covers ::loadEncryptionMethods
   */
  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));
  }

  /**
   * Tests the encrypt & decrypt method.
   *
   * @covers ::__construct
   * @covers ::encrypt
   * @covers ::decrypt
   * @covers ::validate
   *
   * @dataProvider encryptionDataProvider
   */
  public function testEncryptDecrypt($key, $valid_key) {

    // Set up expectations for Key.
    $this->key
      ->getKeyValue()
      ->willReturn($key);

    // This method can decrypt.
    $this->encryptionMethod
      ->canDecrypt()
      ->willReturn(TRUE);
    if ($valid_key) {

      // Set up expectations for encryption method.
      $this->encryptionMethod
        ->encrypt('text_to_encrypt', 'validkey')
        ->willReturn('encrypted_text');
      $this->encryptionMethod
        ->decrypt('text_to_decrypt', 'validkey')
        ->willReturn('decrypted_text');

      // 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([]);
      $this->encryptionProfile
        ->validate('text_to_decrypt')
        ->willReturn([]);
    }
    else {

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

  /**
   * Test exception is thrown trying to decrypt an encryption-only method.
   *
   * @covers ::decrypt
   */
  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());
  }

  /**
   * Data provider for encrypt / decrypt method.
   *
   * @return array
   *   An array with data for the test method.
   */
  public function encryptionDataProvider() {
    return [
      'normal' => [
        "validkey",
        TRUE,
      ],
      'exception' => [
        "invalidkey",
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EncryptServiceTest::$defaultConfig protected property Default configuration.
EncryptServiceTest::$encryptionMethod protected property A mocked EncryptionMethod plugin.
EncryptServiceTest::$encryptionProfile protected property A mocked EncryptionProfile entity.
EncryptServiceTest::$encryptManager protected property A mocked EncryptionMethodManager.
EncryptServiceTest::$key protected property A mocked Key entity.
EncryptServiceTest::$keyRepository protected property A mocked KeyRepository.
EncryptServiceTest::encryptionDataProvider public function Data provider for encrypt / decrypt method.
EncryptServiceTest::setUp protected function Overrides UnitTestCase::setUp
EncryptServiceTest::testEncryptDecrypt public function Tests the encrypt & decrypt method.
EncryptServiceTest::testEncryptionOnlyMethods public function Test exception is thrown trying to decrypt an encryption-only method.
EncryptServiceTest::testLoadEncryptionMethods public function Tests loadEncryptionMethods method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.