You are here

public function FieldEncryptProcessEntitiesTest::testEncyptDecryptEntity in Field Encryption 8.2

Tests the encryptEntity / decryptEntity methods.

@covers ::__construct @covers ::encryptEntity @covers ::decryptEntity @covers ::processEntity @covers ::processField @covers ::processValue @covers ::getUnencryptedPlaceholderValue

@dataProvider encyptDecryptEntityDataProvider

File

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

Class

FieldEncryptProcessEntitiesTest
Unit Tests for the FieldEncryptProcessEntities service.

Namespace

Drupal\Tests\field_encrypt\Unit

Code

public function testEncyptDecryptEntity($field_type, $property_definitions, $properties, $field_value, $expected_placeholder, $encrypted) {

  // Set up field definition.
  $definition = $this
    ->getMockBuilder('\\Drupal\\Core\\Field\\BaseFieldDefinition')
    ->setMethods([
    'get',
    'getType',
  ])
    ->disableOriginalConstructor()
    ->getMock();

  // Set up field storage.
  $storage = $this
    ->getMockBuilder('\\Drupal\\Core\\Field\\FieldConfigStorageBase')
    ->setMethods([
    'getThirdPartySetting',
    'getPropertyDefinitions',
  ])
    ->disableOriginalConstructor()
    ->getMock();

  // Set up expectations for storage.
  $storage_map = [
    [
      'field_encrypt',
      'encrypt',
      FALSE,
      $encrypted,
    ],
    [
      'field_encrypt',
      'encryption_profile',
      [],
      'test_encryption_profile',
    ],
    [
      'field_encrypt',
      'properties',
      [],
      $properties,
    ],
  ];
  $storage
    ->expects($this
    ->any())
    ->method('getThirdPartySetting')
    ->will($this
    ->returnValueMap($storage_map));
  $storage
    ->expects($this
    ->any())
    ->method('getPropertyDefinitions')
    ->will($this
    ->returnValue($property_definitions));

  // Set up expectations for definition.
  $definition
    ->expects($this
    ->any())
    ->method('get')
    ->willReturnMap([
    [
      'field_name',
      'test_field',
    ],
    [
      'fieldStorage',
      $storage,
    ],
  ]);
  $definition
    ->expects($this
    ->any())
    ->method('getType')
    ->will($this
    ->returnValue($field_type));

  // Set up expectations for field.
  $this->field
    ->expects($this
    ->any())
    ->method('getFieldDefinition')
    ->will($this
    ->returnValue($definition));
  if ($encrypted) {
    $this->field
      ->expects($this
      ->once())
      ->method('getValue')
      ->will($this
      ->returnValue($field_value));
    $this->field
      ->expects($this
      ->once())
      ->method('setValue')
      ->with($expected_placeholder);
  }
  else {
    $this->field
      ->expects($this
      ->never())
      ->method('getValue');
    $this->field
      ->expects($this
      ->never())
      ->method('setValue');
  }

  // Set expectations for entity.
  $this->entity
    ->expects($this
    ->once())
    ->method('getFields')
    ->will($this
    ->returnValue([
    $this->field,
  ]));

  // Set up a mock for the EncryptionProfile class to mock some methods.
  $service = $this
    ->getMockBuilder('\\Drupal\\field_encrypt\\FieldEncryptProcessEntities')
    ->setMethods([
    'checkField',
    'allowEncryption',
  ])
    ->setConstructorArgs(array(
    $this->entityManager,
    $this->encryptService,
    $this->encryptionProfileManager,
    $this->encryptedFieldValueManager,
  ))
    ->getMock();

  // Mock some methods on FieldEncryptProcessEntities, since they are out of
  // scope of this specific unit test.
  $service
    ->expects($this
    ->once())
    ->method('checkField')
    ->will($this
    ->returnValue(TRUE));
  $service
    ->expects($this
    ->any())
    ->method('allowEncryption')
    ->will($this
    ->returnValue(TRUE));
  $service
    ->encryptEntity($this->entity);
}