You are here

public function EncryptedFieldValueManagerTest::testGetEncryptedFieldValue in Field Encryption 8.2

Test the getEncryptedFieldValue method.

@covers ::__construct @covers ::getEncryptedFieldValue

@dataProvider getEncryptedFieldValueDataProvider

File

tests/src/Unit/EncryptedFieldValueManagerTest.php, line 186
Contains \Drupal\Tests\field_encrypt\Unit\EncryptedFieldValueManagerTest.

Class

EncryptedFieldValueManagerTest
Unit Tests for the EncryptedFieldValueManager service.

Namespace

Drupal\Tests\field_encrypt\Unit

Code

public function testGetEncryptedFieldValue($existing, $expected_value) {

  // Set up a mock for the EncryptedFieldValueManager class to mock some
  // methods.
  $service = $this
    ->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
    ->setMethods([
    'getExistingEntity',
    'getEncryptedValue',
  ])
    ->setConstructorArgs(array(
    $this->entityManager,
  ))
    ->getMock();

  // Set up expectations depending on whether an existing entity exists.
  if ($existing) {
    $this->encryptedFieldValue
      ->expects($this
      ->once())
      ->method('getEncryptedValue')
      ->will($this
      ->returnValue("encrypted text"));
    $service
      ->expects($this
      ->once())
      ->method('getExistingEntity')
      ->will($this
      ->returnValue($this->encryptedFieldValue));
  }
  else {
    $service
      ->expects($this
      ->once())
      ->method('getExistingEntity')
      ->will($this
      ->returnValue(FALSE));
    $service
      ->expects($this
      ->never())
      ->method('getEncryptedValue');
  }
  $value = $service
    ->getEncryptedFieldValue($this->entity, 'field_test', 0, 'value');
  $this
    ->assertEquals($expected_value, $value);
}