You are here

public function FieldEncryptProcessEntitiesTest::testUpdateStoredField in Field Encryption 8.2

Tests the updateStoredField method.

@covers ::__construct @covers ::updateStoredField

@dataProvider updateStoredFieldDataProvider

File

tests/src/Unit/FieldEncryptProcessEntitiesTest.php, line 564
Contains \Drupal\Tests\field_encrypt\Unit\FieldEncryptProcessEntitiesTest.

Class

FieldEncryptProcessEntitiesTest
Unit Tests for the FieldEncryptProcessEntities service.

Namespace

Drupal\Tests\field_encrypt\Unit

Code

public function testUpdateStoredField($field_name, $field_entity_type, $original_encryption_settings, $entity_id) {

  // Set up entity storage mock.
  $entity_storage = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
    ->disableOriginalConstructor()
    ->getMock();

  // Set up a mock entity type.
  $entity_type = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
    ->disableOriginalConstructor()
    ->getMock();

  // Set up expectations for entity type.
  $entity_type
    ->expects($this
    ->once())
    ->method('hasKey')
    ->will($this
    ->returnValue(TRUE));

  // Set up expectations for entity storage.
  $entity_storage
    ->expects($this
    ->any())
    ->method('loadRevision')
    ->will($this
    ->returnValue($this->entity));
  $entity_storage
    ->expects($this
    ->never())
    ->method('load');

  // Set up expectations for entity manager.
  $this->entityManager
    ->expects($this
    ->once())
    ->method('getStorage')
    ->will($this
    ->returnValue($entity_storage));
  $this->entityManager
    ->expects($this
    ->once())
    ->method('getDefinition')
    ->will($this
    ->returnValue($entity_type));

  // Set up expectations for entity.
  $this->entity
    ->expects($this
    ->once())
    ->method('get')
    ->with($field_name)
    ->will($this
    ->returnValue($this->field));
  $this->entity
    ->expects($this
    ->once())
    ->method('save');

  // Set up a mock for the EncryptionProfile class to mock some methods.
  $service = $this
    ->getMockBuilder('\\Drupal\\field_encrypt\\FieldEncryptProcessEntities')
    ->setMethods([
    'checkField',
    'processField',
    'allowEncryption',
  ])
    ->setConstructorArgs(array(
    $this->entityManager,
    $this->encryptService,
    $this->encryptionProfileManager,
    $this->encryptedFieldValueManager,
  ))
    ->getMock();
  if (!empty($original_encryption_settings)) {
    $service
      ->expects($this
      ->once())
      ->method('processField');
  }
  else {
    $service
      ->expects($this
      ->never())
      ->method('processField');
  }
  $service
    ->expects($this
    ->any())
    ->method('checkField')
    ->will($this
    ->returnValue(TRUE));
  $service
    ->expects($this
    ->any())
    ->method('allowEncryption')
    ->will($this
    ->returnValue(TRUE));
  $service
    ->updateStoredField($field_name, $field_entity_type, $original_encryption_settings, $entity_id);
}