View source
<?php
namespace Drupal\Tests\field_encrypt\Unit;
use Drupal\field_encrypt\EncryptedFieldValueManager;
use Drupal\Tests\UnitTestCase;
class EncryptedFieldValueManagerTest extends UnitTestCase {
protected $entityManager;
protected $entity;
protected $encryptedFieldValue;
protected $storage;
public function setUp() {
parent::setup();
$this->entityManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->storage = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
->disableOriginalConstructor()
->getMock();
$this->entityManager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValue($this->storage));
$this->entity = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\ContentEntityInterface')
->disableOriginalConstructor()
->getMock();
$language = $this
->getMockBuilder('\\Drupal\\Core\\Language\\Language')
->setMethods([
'getId',
])
->disableOriginalConstructor()
->getMock();
$language
->expects($this
->any())
->method('getId')
->will($this
->returnValue('en'));
$this->entity
->expects($this
->any())
->method('language')
->will($this
->returnValue($language));
$this->entity
->expects($this
->any())
->method('getEntityTypeId')
->will($this
->returnValue('node'));
$this->entity
->expects($this
->any())
->method('getRevisionId')
->will($this
->returnValue(2));
$this->encryptedFieldValue = $this
->getMockBuilder('\\Drupal\\field_encrypt\\Entity\\EncryptedFieldValue')
->setMethods([
'getEncryptedValue',
'hasTranslation',
'getTranslation',
'setEncryptedValue',
'save',
])
->disableOriginalConstructor()
->getMock();
$this->encryptedFieldValue
->expects($this
->any())
->method('hasTranslation')
->will($this
->returnValue(TRUE));
$this->encryptedFieldValue
->expects($this
->any())
->method('getTranslation')
->will($this
->returnSelf());
}
public function testSaveEncryptedFieldValue($existing) {
$service = $this
->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
->setMethods([
'getExistingEntity',
'getEntityRevisionId',
])
->setConstructorArgs(array(
$this->entityManager,
))
->getMock();
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');
}
public function saveEncryptedFieldValueDataProvider() {
return [
'existing' => [
TRUE,
],
'not_existing' => [
FALSE,
],
];
}
public function testGetEncryptedFieldValue($existing, $expected_value) {
$service = $this
->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
->setMethods([
'getExistingEntity',
'getEncryptedValue',
])
->setConstructorArgs(array(
$this->entityManager,
))
->getMock();
if ($existing) {
$this->encryptedFieldValue
->expects($this
->once())
->method('getEncryptedValue')
->will($this
->returnValue("encrypted text"));
$service
->expects($this
->once())
->method('getExistingEntity')
->will($this
->returnValue($this->encryptedFieldValue));
}
else {
$service
->expects($this
->once())
->method('getExistingEntity')
->will($this
->returnValue(FALSE));
$service
->expects($this
->never())
->method('getEncryptedValue');
}
$value = $service
->getEncryptedFieldValue($this->entity, 'field_test', 0, 'value');
$this
->assertEquals($expected_value, $value);
}
public function getEncryptedFieldValueDataProvider() {
return [
'existing' => [
TRUE,
"encrypted text",
],
'not_existing' => [
FALSE,
FALSE,
],
];
}
public function testDeleteEntityEncryptedFieldValues() {
$this->storage
->expects($this
->once())
->method('loadByProperties')
->will($this
->returnValue([
$this->encryptedFieldValue,
]));
$this->storage
->expects($this
->once())
->method('delete');
$service = new EncryptedFieldValueManager($this->entityManager);
$service
->deleteEntityEncryptedFieldValues($this->entity);
}
public function testDeleteEntityEncryptedFieldValuesForField() {
$this->storage
->expects($this
->once())
->method('loadByProperties')
->will($this
->returnValue([
$this->encryptedFieldValue,
]));
$this->storage
->expects($this
->once())
->method('delete');
$entity_type = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$entity_type
->expects($this
->once())
->method('hasKey')
->will($this
->returnValue(TRUE));
$this->entity
->expects($this
->any())
->method('getEntityType')
->will($this
->returnValue($entity_type));
$this->entity
->expects($this
->once())
->method('getRevisionId')
->will($this
->returnValue(1));
$this->entity
->expects($this
->never())
->method('id')
->will($this
->returnValue(1));
$service = new EncryptedFieldValueManager($this->entityManager);
$service
->deleteEntityEncryptedFieldValuesForField($this->entity, 'field_test');
}
public function testDeleteEncryptedFieldValuesForField() {
$this->storage
->expects($this
->once())
->method('loadByProperties')
->will($this
->returnValue([
$this->encryptedFieldValue,
]));
$this->storage
->expects($this
->once())
->method('delete');
$service = new EncryptedFieldValueManager($this->entityManager);
$service
->deleteEncryptedFieldValuesForField('node', 'field_test');
}
}