View source
<?php
namespace Drupal\Tests\field_encrypt\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\field_encrypt\FieldEncryptProcessEntities;
use Drupal\Tests\UnitTestCase;
class FieldEncryptProcessEntitiesTest extends UnitTestCase {
protected $entity;
protected $field;
protected $entityManager;
protected $encryptService;
protected $encryptionProfileManager;
protected $encryptionProfile;
protected $encryptedFieldValueManager;
public function setUp() {
parent::setUp();
$this->entity = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\ContentEntityInterface')
->disableOriginalConstructor()
->getMock();
$language = $this
->getMockBuilder('\\Drupal\\Core\\Language\\LanguageInterface')
->disableOriginalConstructor()
->getMock();
$language
->expects($this
->any())
->method('getId')
->will($this
->returnValue('en'));
$this->entity
->expects($this
->any())
->method('getTranslationLanguages')
->will($this
->returnValue([
$language,
]));
$this->entity
->expects($this
->any())
->method('getTranslation')
->will($this
->returnSelf());
$this->field = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldItemListInterface')
->disableOriginalConstructor()
->getMock();
$this->entityManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->encryptService = $this
->getMockBuilder('\\Drupal\\encrypt\\EncryptServiceInterface')
->disableOriginalConstructor()
->getMock();
$this->encryptService
->expects($this
->any())
->method('encrypt')
->will($this
->returnValue('encrypted text'));
$this->encryptService
->expects($this
->any())
->method('decrypt')
->will($this
->returnValue('decrypted text'));
$this->encryptionProfileManager = $this
->getMockBuilder('\\Drupal\\encrypt\\EncryptionProfileManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->encryptionProfile = $this
->getMockBuilder(EncryptionProfileInterface::class)
->disableOriginalConstructor()
->getMock();
$this->encryptionProfileManager
->expects($this
->any())
->method('getEncryptionProfile')
->will($this
->returnValue($this->encryptionProfile));
$this->encryptedFieldValueManager = $this
->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManagerInterface')
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$module_handler = $this
->createMock(ModuleHandlerInterface::class);
$module_handler
->expects($this
->any())
->method('alter');
$container
->set('module_handler', $module_handler);
\Drupal::setContainer($container);
}
public function testEntityHasEncryptedFields($encrypted, $expected) {
$definition = $this
->getMockBuilder('\\Drupal\\Core\\Field\\BaseFieldDefinition')
->setMethods([
'get',
])
->disableOriginalConstructor()
->getMock();
$storage = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldConfigStorageBase')
->setMethods([
'getThirdPartySetting',
])
->disableOriginalConstructor()
->getMock();
$storage
->expects($this
->once())
->method('getThirdPartySetting')
->will($this
->returnValue($encrypted));
$definition
->expects($this
->once())
->method('get')
->will($this
->returnValue($storage));
$this->field
->expects($this
->once())
->method('getFieldDefinition')
->will($this
->returnValue($definition));
$this->entity
->expects($this
->once())
->method('getFields')
->will($this
->returnValue([
$this->field,
]));
$service = new FieldEncryptProcessEntities($this->entityManager, $this->encryptService, $this->encryptionProfileManager, $this->encryptedFieldValueManager);
$return = $service
->entityHasEncryptedFields($this->entity);
$this
->assertEquals($expected, $return);
}
public function entityHasEncryptedFieldsDataProvider() {
return [
'encrypted_fields' => [
TRUE,
TRUE,
],
'no_encrypted_fields' => [
FALSE,
FALSE,
],
];
}
public function testEncyptDecryptEntity($field_type, $property_definitions, $properties, $field_value, $expected_placeholder, $encrypted) {
$definition = $this
->getMockBuilder('\\Drupal\\Core\\Field\\BaseFieldDefinition')
->setMethods([
'get',
'getType',
])
->disableOriginalConstructor()
->getMock();
$storage = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldConfigStorageBase')
->setMethods([
'getThirdPartySetting',
'getPropertyDefinitions',
])
->disableOriginalConstructor()
->getMock();
$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));
$definition
->expects($this
->any())
->method('get')
->willReturnMap([
[
'field_name',
'test_field',
],
[
'fieldStorage',
$storage,
],
]);
$definition
->expects($this
->any())
->method('getType')
->will($this
->returnValue($field_type));
$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');
}
$this->entity
->expects($this
->once())
->method('getFields')
->will($this
->returnValue([
$this->field,
]));
$service = $this
->getMockBuilder('\\Drupal\\field_encrypt\\FieldEncryptProcessEntities')
->setMethods([
'checkField',
'allowEncryption',
])
->setConstructorArgs(array(
$this->entityManager,
$this->encryptService,
$this->encryptionProfileManager,
$this->encryptedFieldValueManager,
))
->getMock();
$service
->expects($this
->once())
->method('checkField')
->will($this
->returnValue(TRUE));
$service
->expects($this
->any())
->method('allowEncryption')
->will($this
->returnValue(TRUE));
$service
->encryptEntity($this->entity);
}
public function encyptDecryptEntityDataProvider() {
return [
'encrypted_string' => [
'string',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
'settings' => [
'case_sensitive' => FALSE,
],
]),
],
[
'value' => 'value',
],
[
[
'value' => 'unencrypted text',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_string_long' => [
'string_long',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
'settings' => [
'case_sensitive' => FALSE,
],
]),
],
[
'value' => 'value',
],
[
[
'value' => 'unencrypted text',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_text' => [
'text',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
'format' => new DataDefinition([
'type' => 'filter_format',
]),
'processed' => new DataDefinition([
'type' => 'string',
'computed' => TRUE,
'class' => '\\Drupal\\text\\TextProcessed',
'settings' => [
'text source' => 'value',
],
]),
],
[
'value' => 'value',
'format' => 'format',
],
[
[
'value' => '<p>unencrypted text</p>',
'format' => 'basic_html',
],
],
[
[
'value' => '[ENCRYPTED]',
'format' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_text_long' => [
'text_long',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
'format' => new DataDefinition([
'type' => 'filter_format',
]),
'processed' => new DataDefinition([
'type' => 'string',
'computed' => TRUE,
'class' => '\\Drupal\\text\\TextProcessed',
'settings' => [
'text source' => 'value',
],
]),
],
[
'value' => 'value',
'format' => 'format',
],
[
[
'value' => '<p>unencrypted text</p>',
'format' => 'basic_html',
],
],
[
[
'value' => '[ENCRYPTED]',
'format' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_text_with_summary' => [
'text_with_summary',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
'format' => new DataDefinition([
'type' => 'filter_format',
]),
'processed' => new DataDefinition([
'type' => 'string',
'computed' => TRUE,
'class' => '\\Drupal\\text\\TextProcessed',
'settings' => [
'text source' => 'value',
],
]),
'summary' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
'summary_processed' => new DataDefinition([
'type' => 'string',
'computed' => TRUE,
'class' => '\\Drupal\\text\\TextProcessed',
'settings' => [
'text source' => 'summarys',
],
]),
],
[
'value' => 'value',
'summary' => 'summary',
'format' => 'format',
],
[
[
'value' => '<p>unencrypted text</p>',
'summary' => 'summary',
'format' => 'basic_html',
],
],
[
[
'value' => '[ENCRYPTED]',
'summary' => '[ENCRYPTED]',
'format' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_list_string' => [
'list_string',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
'constraints' => [
'Length' => [
'max' => 255,
],
],
]),
],
[
'value' => 'value',
],
[
[
'value' => 'value1',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_email' => [
'email',
[
'value' => new DataDefinition([
'type' => 'email',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => 'test@example.com',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_date' => [
'datetime',
[
'value' => new DataDefinition([
'type' => 'datetime_iso8601',
'required' => TRUE,
]),
'date' => new DataDefinition([
'type' => 'any',
'computed' => TRUE,
'class' => '\\Drupal\\datetime\\DateTimeComputed',
'settings' => [
'date source' => 'value',
],
]),
],
[
'value' => 'value',
],
[
[
'value' => '1984-10-04T00:00:00',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_link' => [
'link',
[
'uri' => new DataDefinition([
'type' => 'uri',
]),
'title' => new DataDefinition([
'type' => 'string',
]),
'options' => new DataDefinition([
'type' => 'map',
]),
],
[
'uri' => 'uri',
'title' => 'title',
],
[
[
'title' => 'Drupal.org',
'attributes' => [],
'options' => [],
'uri' => 'https://drupal.org',
],
],
[
[
'title' => '[ENCRYPTED]',
'uri' => '[ENCRYPTED]',
'options' => [],
'attributes' => [],
],
],
TRUE,
],
'encrypted_int' => [
'integer',
[
'value' => new DataDefinition([
'type' => 'integer',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => '42',
],
],
[
[
'value' => 0,
],
],
TRUE,
],
'encrypted_float' => [
'float',
[
'value' => new DataDefinition([
'type' => 'float',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => '3.14',
],
],
[
[
'value' => 0,
],
],
TRUE,
],
'encrypted_decimal' => [
'decimal',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => '3.14',
],
],
[
[
'value' => 0,
],
],
TRUE,
],
'encrypted_boolean' => [
'boolean',
[
'value' => new DataDefinition([
'type' => 'boolean',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => 1,
],
],
[
[
'value' => 0,
],
],
TRUE,
],
'encrypted_telephone' => [
'telephone',
[
'value' => new DataDefinition([
'type' => 'string',
'required' => TRUE,
]),
],
[
'value' => 'value',
],
[
[
'value' => '+1-202-555-0161',
],
],
[
[
'value' => '[ENCRYPTED]',
],
],
TRUE,
],
'encrypted_entity_reference' => [
'entity_reference',
[
'target_id' => new DataDefinition([
'type' => 'integer',
'settings' => [
'unsigned' => TRUE,
],
'required' => TRUE,
]),
'entity' => new DataDefinition([
'type' => 'entity_reference',
'computed' => TRUE,
'read-only' => FALSE,
'constraints' => [
'EntityType' => 'user',
],
]),
],
[
'target_id' => 'target_id',
],
[
[
'target_id' => 1,
],
],
[
[
'target_id' => 0,
],
],
TRUE,
],
'not_encrypted' => [
'text',
[],
[],
'unencrypted text',
NULL,
FALSE,
],
];
}
public function testUpdateStoredField($field_name, $field_entity_type, $original_encryption_settings, $entity_id) {
$entity_storage = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
->disableOriginalConstructor()
->getMock();
$entity_type = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$entity_type
->expects($this
->once())
->method('hasKey')
->will($this
->returnValue(TRUE));
$entity_storage
->expects($this
->any())
->method('loadRevision')
->will($this
->returnValue($this->entity));
$entity_storage
->expects($this
->never())
->method('load');
$this->entityManager
->expects($this
->once())
->method('getStorage')
->will($this
->returnValue($entity_storage));
$this->entityManager
->expects($this
->once())
->method('getDefinition')
->will($this
->returnValue($entity_type));
$this->entity
->expects($this
->once())
->method('get')
->with($field_name)
->will($this
->returnValue($this->field));
$this->entity
->expects($this
->once())
->method('save');
$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);
}
public function updateStoredFieldDataProvider() {
return [
'no_decrypt' => [
'field_test',
'node',
[],
1,
],
'decrypt' => [
'field_test',
'node',
[
'field_encrypt' => TRUE,
'properties' => [
'value',
],
'encryption_profile' => 'test_encryption_profile',
],
1,
],
];
}
}