You are here

public function EncryptionProfileTest::testValidate in Encrypt 8.3

Tests the EncryptionProfile validate method.

@covers ::__construct @covers ::validate

@dataProvider validateDataProvider

File

tests/src/Unit/Entity/EncryptionProfileTest.php, line 109

Class

EncryptionProfileTest
Unit tests for EncryptionProfile class.

Namespace

Drupal\Tests\encrypt\Unit\Entity

Code

public function testValidate($enc_method_id, $enc_key, $enc_method_def, $expected_errors) {

  // Set up a mock for the EncryptionProfile class to mock some methods.
  $encryption_profile = $this
    ->getMockBuilder('\\Drupal\\encrypt\\Entity\\EncryptionProfile')
    ->setMethods([
    'getEncryptionMethod',
    'getEncryptionMethodId',
    'getEncryptionKey',
    'getEncryptionKeyId',
  ])
    ->disableOriginalConstructor()
    ->getMock();

  // Set expectations for the EncryptionMethod.
  $this->encryptionMethod
    ->expects($this
    ->any())
    ->method('getPluginDefinition')
    ->will($this
    ->returnValue($enc_method_def));

  // Set expectations for EncryptionProfile entity.
  $encryption_profile
    ->expects($this
    ->any())
    ->method('getEncryptionMethodId')
    ->will($this
    ->returnValue($enc_method_id));
  $encryption_profile
    ->expects($this
    ->any())
    ->method('getEncryptionKeyId')
    ->will($this
    ->returnValue($enc_key));
  if ($enc_method_id == "test_encryption_method") {
    $encryption_profile
      ->expects($this
      ->any())
      ->method('getEncryptionMethod')
      ->will($this
      ->returnValue($this->encryptionMethod));
  }
  else {
    $encryption_profile
      ->expects($this
      ->any())
      ->method('getEncryptionMethod')
      ->will($this
      ->returnValue(FALSE));
  }
  if ($enc_key == "test_key") {
    $encryption_profile
      ->expects($this
      ->any())
      ->method('getEncryptionKey')
      ->will($this
      ->returnValue($this->key));
  }
  if ($enc_key == "wrong_key") {
    $encryption_profile
      ->expects($this
      ->any())
      ->method('getEncryptionKey')
      ->will($this
      ->returnValue(FALSE));
  }
  $errors = $encryption_profile
    ->validate();
  $this
    ->assertEquals($expected_errors, $errors);
}