3 namespace Drupal\Tests\serialization\Unit\Normalizer;
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\serialization\Normalizer\EntityNormalizer;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
12 * @coversDefaultClass \Drupal\serialization\Normalizer\EntityNormalizer
13 * @group serialization
15 class EntityNormalizerTest extends UnitTestCase {
18 * The mock entity manager.
20 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
22 protected $entityManager;
25 * The mock serializer.
27 * @var \Symfony\Component\Serializer\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $serializer;
32 * The entity normalizer.
34 * @var \Drupal\serialization\Normalizer\EntityNormalizer
36 protected $entityNormalizer;
41 protected function setUp() {
42 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
43 $this->entityNormalizer = new EntityNormalizer($this->entityManager);
47 * Tests the normalize() method.
51 public function testNormalize() {
52 $list_item_1 = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
53 $list_item_2 = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
56 'field_1' => $list_item_1,
57 'field_2' => $list_item_2,
60 $content_entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
61 ->disableOriginalConstructor()
62 ->setMethods(['getFields'])
63 ->getMockForAbstractClass();
64 $content_entity->expects($this->once())
66 ->will($this->returnValue($definitions));
68 $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
69 ->disableOriginalConstructor()
70 ->setMethods(['normalize'])
72 $serializer->expects($this->at(0))
74 ->with($list_item_1, 'test_format');
75 $serializer->expects($this->at(1))
77 ->with($list_item_2, 'test_format');
79 $this->entityNormalizer->setSerializer($serializer);
81 $this->entityNormalizer->normalize($content_entity, 'test_format');
85 * Tests the denormalize() method with no entity type provided in context.
87 * @covers ::denormalize
89 public function testDenormalizeWithNoEntityType() {
90 $this->setExpectedException(UnexpectedValueException::class);
91 $this->entityNormalizer->denormalize([], 'Drupal\Core\Entity\ContentEntityBase');
95 * Tests the denormalize method with a bundle property.
97 * @covers ::denormalize
99 public function testDenormalizeWithValidBundle() {
101 'key_1' => 'value_1',
102 'key_2' => 'value_2',
104 ['name' => 'test_bundle'],
108 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
110 $entity_type->expects($this->once())
112 ->willReturn('test');
113 $entity_type->expects($this->once())
116 ->will($this->returnValue(TRUE));
117 $entity_type->expects($this->once())
120 ->will($this->returnValue('test_type'));
121 $entity_type->expects($this->once())
122 ->method('isSubClassOf')
123 ->with(FieldableEntityInterface::class)
126 $entity_type->expects($this->once())
127 ->method('getBundleEntityType')
128 ->will($this->returnValue('test_bundle'));
130 $entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
131 $entity_type_storage_definition->expects($this->once())
132 ->method('getMainPropertyName')
133 ->will($this->returnValue('name'));
135 $entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
136 $entity_type_definition->expects($this->once())
137 ->method('getFieldStorageDefinition')
138 ->will($this->returnValue($entity_type_storage_definition));
140 $base_definitions = [
141 'test_type' => $entity_type_definition,
144 $this->entityManager->expects($this->at(0))
145 ->method('getDefinition')
147 ->will($this->returnValue($entity_type));
148 $this->entityManager->expects($this->at(1))
149 ->method('getBaseFieldDefinitions')
151 ->will($this->returnValue($base_definitions));
153 $entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
154 $entity_query_mock->expects($this->once())
156 ->will($this->returnValue(['test_bundle' => 'test_bundle']));
158 $entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
159 $entity_type_storage->expects($this->once())
161 ->will($this->returnValue($entity_query_mock));
163 $this->entityManager->expects($this->at(2))
164 ->method('getStorage')
165 ->with('test_bundle')
166 ->will($this->returnValue($entity_type_storage));
168 $key_1 = $this->getMock(FieldItemListInterface::class);
169 $key_2 = $this->getMock(FieldItemListInterface::class);
171 $entity = $this->getMock(FieldableEntityInterface::class);
172 $entity->expects($this->at(0))
175 ->willReturn($key_1);
176 $entity->expects($this->at(1))
179 ->willReturn($key_2);
181 $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
182 // Create should only be called with the bundle property at first.
183 $expected_test_data = [
184 'test_type' => 'test_bundle',
187 $storage->expects($this->once())
189 ->with($expected_test_data)
190 ->will($this->returnValue($entity));
192 $this->entityManager->expects($this->at(3))
193 ->method('getStorage')
195 ->will($this->returnValue($storage));
197 // Setup expectations for the serializer. This will be called for each field
199 $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
200 ->disableOriginalConstructor()
201 ->setMethods(['denormalize'])
203 $serializer->expects($this->at(0))
204 ->method('denormalize')
205 ->with('value_1', get_class($key_1), NULL, ['target_instance' => $key_1, 'entity_type' => 'test']);
206 $serializer->expects($this->at(1))
207 ->method('denormalize')
208 ->with('value_2', get_class($key_2), NULL, ['target_instance' => $key_2, 'entity_type' => 'test']);
210 $this->entityNormalizer->setSerializer($serializer);
212 $this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));
216 * Tests the denormalize method with a bundle property.
218 * @covers ::denormalize
220 public function testDenormalizeWithInvalidBundle() {
222 'key_1' => 'value_1',
223 'key_2' => 'value_2',
225 ['name' => 'test_bundle'],
229 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
231 $entity_type->expects($this->once())
233 ->willReturn('test');
234 $entity_type->expects($this->once())
237 ->will($this->returnValue(TRUE));
238 $entity_type->expects($this->once())
241 ->will($this->returnValue('test_type'));
242 $entity_type->expects($this->once())
243 ->method('isSubClassOf')
244 ->with(FieldableEntityInterface::class)
247 $entity_type->expects($this->once())
248 ->method('getBundleEntityType')
249 ->will($this->returnValue('test_bundle'));
251 $entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
252 $entity_type_storage_definition->expects($this->once())
253 ->method('getMainPropertyName')
254 ->will($this->returnValue('name'));
256 $entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
257 $entity_type_definition->expects($this->once())
258 ->method('getFieldStorageDefinition')
259 ->will($this->returnValue($entity_type_storage_definition));
261 $base_definitions = [
262 'test_type' => $entity_type_definition,
265 $this->entityManager->expects($this->at(0))
266 ->method('getDefinition')
268 ->will($this->returnValue($entity_type));
269 $this->entityManager->expects($this->at(1))
270 ->method('getBaseFieldDefinitions')
272 ->will($this->returnValue($base_definitions));
274 $entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
275 $entity_query_mock->expects($this->once())
277 ->will($this->returnValue(['test_bundle_other' => 'test_bundle_other']));
279 $entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
280 $entity_type_storage->expects($this->once())
282 ->will($this->returnValue($entity_query_mock));
284 $this->entityManager->expects($this->at(2))
285 ->method('getStorage')
286 ->with('test_bundle')
287 ->will($this->returnValue($entity_type_storage));
289 $this->setExpectedException(UnexpectedValueException::class);
290 $this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']);
294 * Tests the denormalize method with no bundle defined.
296 * @covers ::denormalize
298 public function testDenormalizeWithNoBundle() {
300 'key_1' => 'value_1',
301 'key_2' => 'value_2',
304 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
305 $entity_type->expects($this->once())
308 ->will($this->returnValue(FALSE));
309 $entity_type->expects($this->never())
312 $this->entityManager->expects($this->once())
313 ->method('getDefinition')
315 ->will($this->returnValue($entity_type));
317 $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
318 $storage->expects($this->once())
321 ->will($this->returnValue($this->getMock('Drupal\Core\Entity\EntityInterface')));
323 $this->entityManager->expects($this->once())
324 ->method('getStorage')
326 ->will($this->returnValue($storage));
328 $this->entityManager->expects($this->never())
329 ->method('getBaseFieldDefinitions');
331 $this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));