|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * |
| 6 | + * Contains \Drupal\Tests\field_encrypt\Unit\FieldEncryptProcessEntitiesServiceTest. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Drupal\Tests\field_encrypt\Unit; |
| 10 | + |
| 11 | +use Drupal\field_encrypt\FieldEncryptMapPluginManager; |
| 12 | +use Drupal\field_encrypt\FieldEncryptProcessEntities; |
| 13 | +use Drupal\Tests\UnitTestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit Tests for the FieldEncryptProcessEntities service. |
| 17 | + * |
| 18 | + * @group pants |
| 19 | + */ |
| 20 | +class FieldEncryptProcessEntitiesServiceTest extends UnitTestCase { |
| 21 | + |
| 22 | + /** |
| 23 | + * The tested service. |
| 24 | + * |
| 25 | + * @var \Drupal\field_encrypt\FieldEncryptProcessEntities |
| 26 | + */ |
| 27 | + public $service; |
| 28 | + |
| 29 | + /** |
| 30 | + * A test value so we don't have to mock up the whole field item list system. |
| 31 | + */ |
| 32 | + public $testValue = [ |
| 33 | + ['value' => 'world'] |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * A test map that would be provided by plugins. |
| 38 | + */ |
| 39 | + public $testMap = [ |
| 40 | + 'text' => [ |
| 41 | + 'value' => 'mock.service', |
| 42 | + ], |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * Our test field. |
| 47 | + */ |
| 48 | + public $field; |
| 49 | + |
| 50 | + /** |
| 51 | + * Our test entity. |
| 52 | + */ |
| 53 | + public $entity; |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function setUp() { |
| 59 | + parent::setUp(); |
| 60 | + |
| 61 | + // The goal of this setup is to provide a test entity with a test field. |
| 62 | + |
| 63 | + /** |
| 64 | + * Generate a test field. |
| 65 | + * |
| 66 | + * Our system uses the first storage system, so we need to mock that out too. |
| 67 | + */ |
| 68 | + // Field Storage mock. |
| 69 | + $fieldStorage = $this->getMockBuilder('\Drupal\Core\Field\FieldConfigStorageBase') |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->setMethods(['getThirdPartySetting']) |
| 72 | + ->getMock(); |
| 73 | + |
| 74 | + $fieldStorage->expects($this->any())->method('getThirdPartySetting') |
| 75 | + // We will always report that the field is encrypted. |
| 76 | + ->willReturn(TRUE); |
| 77 | + |
| 78 | + // Field Definition mock. |
| 79 | + $fieldDefinition = $this->getMockBuilder('\Drupal\Core\Field\BaseFieldDefinition') |
| 80 | + ->setMethods(['get']) |
| 81 | + ->getMock(); |
| 82 | + |
| 83 | + $fieldDefinition->expects($this->any())->method('get') |
| 84 | + ->willReturnMap([ |
| 85 | + ['field_name', 'test_field'], |
| 86 | + ['field_type', 'text'], |
| 87 | + ['fieldStorage', $fieldStorage], |
| 88 | + ]); |
| 89 | + |
| 90 | + // Test field. |
| 91 | + $this->field = $this->getMockBuilder('\Drupal\Core\Field\FieldItemList') |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->setMethods(['getFieldDefinition', 'getValue', 'setValue']) |
| 94 | + ->getMock(); |
| 95 | + |
| 96 | + $this->field->expects($this->any())->method('getFieldDefinition') |
| 97 | + ->willReturn($fieldDefinition); |
| 98 | + |
| 99 | + $this->field->expects($this->any())->method('getValue') |
| 100 | + // We use a function here so that the value is calculated at runtime. |
| 101 | + ->will($this->returnCallback( |
| 102 | + function() { |
| 103 | + return $this->testValue; |
| 104 | + } |
| 105 | + )); |
| 106 | + |
| 107 | + $this->field->expects($this->any())->method('setValue') |
| 108 | + ->will($this->returnCallback( |
| 109 | + function($value) { |
| 110 | + $this->testValue = $value; |
| 111 | + } |
| 112 | + )); |
| 113 | + |
| 114 | + // Create a test entity with our test field. |
| 115 | + $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityInterface') |
| 116 | + ->getMock(); |
| 117 | + |
| 118 | + $this->entity->expects($this->any())->method('getFields') |
| 119 | + ->willReturn([$this->field]); |
| 120 | + |
| 121 | + $this->entity->expects($this->any())->method('get') |
| 122 | + ->willReturn($this->field); |
| 123 | + |
| 124 | + $this->entity->expects($this->any())->method('save') |
| 125 | + ->willReturn(NULL); |
| 126 | + |
| 127 | + /** |
| 128 | + * Now we will create our service and pass it mocks of the necessary dependencies. |
| 129 | + */ |
| 130 | + |
| 131 | + // Plugin Manager mock. |
| 132 | + $plugin_manager = $this->getMockBuilder('\Drupal\field_encrypt\FieldEncryptMapPluginManager') |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->setMethods(['getDefinitions', 'createInstance']) |
| 135 | + ->getMock(); |
| 136 | + |
| 137 | + $plugin_manager->expects($this->any())->method('getDefinitions') |
| 138 | + ->willReturn(['mockMap' => []]); |
| 139 | + |
| 140 | + // Field Encrypt Map Plugin mock. |
| 141 | + $field_encrypt_map = $this->getMockBuilder('\Drupal\pants\FieldEncryptMapBase') |
| 142 | + ->setMethods(['getMap']) |
| 143 | + ->getMock(); |
| 144 | + |
| 145 | + $field_encrypt_map->expects($this->any())->method('getMap') |
| 146 | + ->willReturn($this->testMap); |
| 147 | + |
| 148 | + $plugin_manager->expects($this->any())->method('createInstance') |
| 149 | + ->willReturn($field_encrypt_map); |
| 150 | + |
| 151 | + // Query mock. |
| 152 | + $query = $this->getMockBuilder('\Drupal\Core\Entity\Query\QueryInterface') |
| 153 | + ->getMock(); |
| 154 | + |
| 155 | + $query->expects($this->any())->method('execute') |
| 156 | + ->willReturn([1 => 1]); |
| 157 | + |
| 158 | + // Query Factory mock. |
| 159 | + $query_factory = $this->getMockBuilder('\Drupal\Core\Entity\Query\QueryFactory') |
| 160 | + ->disableOriginalConstructor() |
| 161 | + ->getMock(); |
| 162 | + |
| 163 | + $query_factory->expects($this->any())->method('get') |
| 164 | + ->willReturn($query); |
| 165 | + |
| 166 | + // Entity storage mock. |
| 167 | + // We are using a node here, but others should work as well. |
| 168 | + $entity_storage = $this->getMockBuilder('\Drupal\node\NodeStorage') |
| 169 | + ->disableOriginalConstructor() |
| 170 | + ->getMock(); |
| 171 | + |
| 172 | + $entity_storage->expects($this->any())->method('loadRevision') |
| 173 | + ->willReturn($this->entity); |
| 174 | + |
| 175 | + // Entity Manager mock. |
| 176 | + $entity_manager = $this->getMockBuilder('\Drupal\Core\Entity\EntityManager') |
| 177 | + ->disableOriginalConstructor() |
| 178 | + ->getMock(); |
| 179 | + |
| 180 | + $entity_manager->expects($this->any())->method('getStorage') |
| 181 | + ->willReturn($entity_storage); |
| 182 | + |
| 183 | + // An Encryption service mock. |
| 184 | + $encrypt_service = $this->getMockBuilder('stdClass') |
| 185 | + ->setMethods(['encrypt', 'decrypt']) |
| 186 | + ->getMock(); |
| 187 | + |
| 188 | + $encrypt_service->expects($this->any())->method('encrypt') |
| 189 | + ->willReturn('encrypted value'); |
| 190 | + |
| 191 | + $encrypt_service->expects($this->any())->method('decrypt') |
| 192 | + ->willReturn('decrypted value'); |
| 193 | + |
| 194 | + // Service Container mock that will return our single mock service. |
| 195 | + $container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerInterface') |
| 196 | + ->getMock(); |
| 197 | + |
| 198 | + $container->expects($this->any())->method('get') |
| 199 | + ->willReturn($encrypt_service); |
| 200 | + |
| 201 | + // Store an instance of our service. |
| 202 | + $this->service = new FieldEncryptProcessEntities($plugin_manager, $query_factory, $entity_manager, $container); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test the encrypt_entity() function. |
| 207 | + */ |
| 208 | + public function testEncryptEntity() { |
| 209 | + $this->service->encrypt_entity($this->entity); |
| 210 | + |
| 211 | + $this->assertEquals('encrypted value', $this->field->getValue()[0]['value']); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Test the decrypt_entity() function. |
| 216 | + */ |
| 217 | + public function testDecryptEntity() { |
| 218 | + $this->service->decrypt_entity($this->entity); |
| 219 | + |
| 220 | + $this->assertEquals('decrypted value', $this->field->getValue()[0]['value']); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Test the encrypt_stored_field() function. |
| 225 | + */ |
| 226 | + public function testEncryptStoredField() { |
| 227 | + $this->service->encrypt_stored_field('node', 'test_field'); |
| 228 | + |
| 229 | + $this->assertEquals('encrypted value', $this->field->getValue()[0]['value']); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Test the decrypt_stored_field() function. |
| 234 | + */ |
| 235 | + public function testDecryptStoredField() { |
| 236 | + $this->service->decrypt_stored_field('node', 'test_field'); |
| 237 | + |
| 238 | + $this->assertEquals('decrypted value', $this->field->getValue()[0]['value']); |
| 239 | + } |
| 240 | + |
| 241 | +} |
0 commit comments