You are here

public function EncryptedFieldValueManagerTest::testSaveEncryptedFieldValue in Field Encryption 8.2

Test the saveEncryptedFieldValue method.

@covers ::__construct @covers ::saveEncryptedFieldValues

@dataProvider saveEncryptedFieldValueDataProvider

File

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

Class

EncryptedFieldValueManagerTest
Unit Tests for the EncryptedFieldValueManager service.

Namespace

Drupal\Tests\field_encrypt\Unit

Code

public function testSaveEncryptedFieldValue($existing) {

  // Set up a mock for the EncryptedFieldValueManager class to mock
  // some methods.

  /** @var \Drupal\field_encrypt\EncryptedFieldValueManager $service */
  $service = $this
    ->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
    ->setMethods([
    'getExistingEntity',
    'getEntityRevisionId',
  ])
    ->setConstructorArgs(array(
    $this->entityManager,
  ))
    ->getMock();

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