You are here

public function ProcessEntitiesTest::testEncryptDecryptEntity in Field Encryption 3.0.x

Tests the encryptEntity / decryptEntity methods.

@covers ::__construct @covers ::encryptEntity @covers ::decryptEntity @covers ::encryptField @covers ::encryptFieldValue @covers ::getUnencryptedPlaceholderValue

@dataProvider encryptDecryptEntityDataProvider

File

tests/src/Unit/ProcessEntitiesTest.php, line 107

Class

ProcessEntitiesTest
Unit Tests for the ProcessEntities service.

Namespace

Drupal\Tests\field_encrypt\Unit

Code

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

  // Set up field definition.
  $definition = $this
    ->getMockBuilder(FieldConfig::class)
    ->onlyMethods([
    'getName',
    'getFieldStorageDefinition',
    'getType',
  ])
    ->disableOriginalConstructor()
    ->getMock();

  // Set up field storage.
  $storage = $this
    ->getMockBuilder(FieldStorageConfig::class)
    ->onlyMethods([
    'getThirdPartySetting',
    'getPropertyDefinitions',
    'isBaseField',
  ])
    ->disableOriginalConstructor()
    ->getMock();

  // Set up expectations for storage.
  $storage_map = [
    [
      'field_encrypt',
      'encrypt',
      FALSE,
      $encrypted,
    ],
    [
      '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));
  $storage
    ->expects($this
    ->any())
    ->method('isBaseField')
    ->willReturn(FALSE);

  // Set up expectations for definition.
  $definition
    ->expects($this
    ->any())
    ->method('getName')
    ->willReturn('test_field');
  $definition
    ->expects($this
    ->any())
    ->method('getFieldStorageDefinition')
    ->willReturn($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));
  $this->field
    ->expects($this
    ->any())
    ->method('getName')
    ->willReturn('test_field');
  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('hasField')
    ->with(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME)
    ->willReturn(TRUE);
  $this->entity
    ->expects($this
    ->once())
    ->method('getFields')
    ->will($this
    ->returnValue([
    'test_field' => $this->field,
  ]));
  $this->entity
    ->expects($this
    ->any())
    ->method('get')
    ->with(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME)
    ->will($this
    ->returnValue($this->storageField));

  // Set up a mock for the EncryptionProfile class to mock some methods.
  $module_handler = $this
    ->createMock(ModuleHandlerInterface::class);
  $module_handler
    ->expects($this
    ->atMost(1))
    ->method('getImplementations')
    ->with('field_encrypt_allow_encryption')
    ->willReturn([]);
  $service = new ProcessEntities($module_handler);
  $service
    ->encryptEntity($this->entity);
}