class EncryptedFieldValueManagerTest in Field Encryption 8.2
Unit Tests for the EncryptedFieldValueManager service.
@group field_encrypt
@coversDefaultClass \Drupal\field_encrypt\EncryptedFieldValueManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\field_encrypt\Unit\EncryptedFieldValueManagerTest
Expanded class hierarchy of EncryptedFieldValueManagerTest
File
- tests/
src/ Unit/ EncryptedFieldValueManagerTest.php, line 22 - Contains \Drupal\Tests\field_encrypt\Unit\EncryptedFieldValueManagerTest.
Namespace
Drupal\Tests\field_encrypt\UnitView source
class EncryptedFieldValueManagerTest extends UnitTestCase {
/**
* A mock entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
/**
* A mock entity.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $entity;
/**
* A mock EncryptedFieldValue entity.
*
* @var \Drupal\field_encrypt\Entity\EncryptedFieldValueInterface
*/
protected $encryptedFieldValue;
/**
* A mock EntityStorage instance.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setup();
// Set up a mock entity type manager service.
$this->entityManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
// Set up a mock EntityStorage.
$this->storage = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
->disableOriginalConstructor()
->getMock();
// Set up expectations for the entity type manager.
$this->entityManager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValue($this->storage));
// Set up a mock entity.
$this->entity = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\ContentEntityInterface')
->disableOriginalConstructor()
->getMock();
// Set up language object.
$language = $this
->getMockBuilder('\\Drupal\\Core\\Language\\Language')
->setMethods([
'getId',
])
->disableOriginalConstructor()
->getMock();
// Set up expectations for language.
$language
->expects($this
->any())
->method('getId')
->will($this
->returnValue('en'));
// Set up expectations for entity.
$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));
// Set up EncryptedFieldValue.
$this->encryptedFieldValue = $this
->getMockBuilder('\\Drupal\\field_encrypt\\Entity\\EncryptedFieldValue')
->setMethods([
'getEncryptedValue',
'hasTranslation',
'getTranslation',
'setEncryptedValue',
'save',
])
->disableOriginalConstructor()
->getMock();
// Set up expectations for EncryptedFieldValue.
$this->encryptedFieldValue
->expects($this
->any())
->method('hasTranslation')
->will($this
->returnValue(TRUE));
$this->encryptedFieldValue
->expects($this
->any())
->method('getTranslation')
->will($this
->returnSelf());
}
/**
* Test the saveEncryptedFieldValue method.
*
* @covers ::__construct
* @covers ::saveEncryptedFieldValues
*
* @dataProvider saveEncryptedFieldValueDataProvider
*/
public function testSaveEncryptedFieldValue($existing) {
// Set up a mock for the EncryptedFieldValueManager class to mock
// some methods.
/** @var \Drupal\field_encrypt\EncryptedFieldValueManager $service */
$service = $this
->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
->setMethods([
'getExistingEntity',
'getEntityRevisionId',
])
->setConstructorArgs(array(
$this->entityManager,
))
->getMock();
// Set up expectations depending on whether an existing entity exists.
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');
}
/**
* Data provider for testSaveEncryptedFieldValue method.
*
* @return array
* An array with data for the test method.
*/
public function saveEncryptedFieldValueDataProvider() {
return [
'existing' => [
TRUE,
],
'not_existing' => [
FALSE,
],
];
}
/**
* Test the getEncryptedFieldValue method.
*
* @covers ::__construct
* @covers ::getEncryptedFieldValue
*
* @dataProvider getEncryptedFieldValueDataProvider
*/
public function testGetEncryptedFieldValue($existing, $expected_value) {
// Set up a mock for the EncryptedFieldValueManager class to mock some
// methods.
$service = $this
->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
->setMethods([
'getExistingEntity',
'getEncryptedValue',
])
->setConstructorArgs(array(
$this->entityManager,
))
->getMock();
// Set up expectations depending on whether an existing entity exists.
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);
}
/**
* Data provider for testGetEncryptedFieldValue method.
*
* @return array
* An array with data for the test method.
*/
public function getEncryptedFieldValueDataProvider() {
return [
'existing' => [
TRUE,
"encrypted text",
],
'not_existing' => [
FALSE,
FALSE,
],
];
}
/**
* Test the deleteEntityEncryptedFieldValues method.
*
* @covers ::__construct
* @covers ::deleteEntityEncryptedFieldValues
*/
public function testDeleteEntityEncryptedFieldValues() {
// Set up expectations for storage.
$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);
}
/**
* Test the deleteEntityEncryptedFieldValuesForField method.
*
* @covers ::__construct
* @covers ::deleteEntityEncryptedFieldValuesForField
* @covers ::getEntityRevisionId
*/
public function testDeleteEntityEncryptedFieldValuesForField() {
// Set up expectations for storage.
$this->storage
->expects($this
->once())
->method('loadByProperties')
->will($this
->returnValue([
$this->encryptedFieldValue,
]));
$this->storage
->expects($this
->once())
->method('delete');
// Set up entity type object.
$entity_type = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
// Set up expectations for entity type object.
$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');
}
/**
* Test the deleteEncryptedFieldValuesForField method.
*
* @covers ::__construct
* @covers ::deleteEncryptedFieldValuesForField
*/
public function testDeleteEncryptedFieldValuesForField() {
// Set up expectations for storage.
$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');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EncryptedFieldValueManagerTest:: |
protected | property | A mock EncryptedFieldValue entity. | |
EncryptedFieldValueManagerTest:: |
protected | property | A mock entity. | |
EncryptedFieldValueManagerTest:: |
protected | property | A mock entity type manager service. | |
EncryptedFieldValueManagerTest:: |
protected | property | A mock EntityStorage instance. | |
EncryptedFieldValueManagerTest:: |
public | function | Data provider for testGetEncryptedFieldValue method. | |
EncryptedFieldValueManagerTest:: |
public | function | Data provider for testSaveEncryptedFieldValue method. | |
EncryptedFieldValueManagerTest:: |
public | function |
Overrides UnitTestCase:: |
|
EncryptedFieldValueManagerTest:: |
public | function | Test the deleteEncryptedFieldValuesForField method. | |
EncryptedFieldValueManagerTest:: |
public | function | Test the deleteEntityEncryptedFieldValues method. | |
EncryptedFieldValueManagerTest:: |
public | function | Test the deleteEntityEncryptedFieldValuesForField method. | |
EncryptedFieldValueManagerTest:: |
public | function | Test the getEncryptedFieldValue method. | |
EncryptedFieldValueManagerTest:: |
public | function | Test the saveEncryptedFieldValue method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |