bbddbdd4844d8834efcb5d40b7233eb56b580c23
[yaffs-website] / web / core / modules / serialization / tests / src / Unit / Normalizer / EntityNormalizerTest.php
1 <?php
2
3 namespace Drupal\Tests\serialization\Unit\Normalizer;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\serialization\Normalizer\EntityNormalizer
13  * @group serialization
14  */
15 class EntityNormalizerTest extends UnitTestCase {
16
17   /**
18    * The mock entity manager.
19    *
20    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $entityManager;
23
24   /**
25    * The mock serializer.
26    *
27    * @var \Symfony\Component\Serializer\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $serializer;
30
31   /**
32    * The entity normalizer.
33    *
34    * @var \Drupal\serialization\Normalizer\EntityNormalizer
35    */
36   protected $entityNormalizer;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
43     $this->entityNormalizer = new EntityNormalizer($this->entityManager);
44   }
45
46   /**
47    * Tests the normalize() method.
48    *
49    * @covers ::normalize
50    */
51   public function testNormalize() {
52     $list_item_1 = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
53     $list_item_2 = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
54
55     $definitions = [
56       'field_1' => $list_item_1,
57       'field_2' => $list_item_2,
58     ];
59
60     $content_entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
61       ->disableOriginalConstructor()
62       ->setMethods(['getFields'])
63       ->getMockForAbstractClass();
64     $content_entity->expects($this->once())
65       ->method('getFields')
66       ->will($this->returnValue($definitions));
67
68     $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
69       ->disableOriginalConstructor()
70       ->setMethods(['normalize'])
71       ->getMock();
72     $serializer->expects($this->at(0))
73       ->method('normalize')
74       ->with($list_item_1, 'test_format');
75     $serializer->expects($this->at(1))
76       ->method('normalize')
77       ->with($list_item_2, 'test_format');
78
79     $this->entityNormalizer->setSerializer($serializer);
80
81     $this->entityNormalizer->normalize($content_entity, 'test_format');
82   }
83
84   /**
85    * Tests the denormalize() method with no entity type provided in context.
86    *
87    * @covers ::denormalize
88    */
89   public function testDenormalizeWithNoEntityType() {
90     $this->setExpectedException(UnexpectedValueException::class);
91     $this->entityNormalizer->denormalize([], 'Drupal\Core\Entity\ContentEntityBase');
92   }
93
94   /**
95    * Tests the denormalize method with a bundle property.
96    *
97    * @covers ::denormalize
98    */
99   public function testDenormalizeWithValidBundle() {
100     $test_data = [
101       'key_1' => 'value_1',
102       'key_2' => 'value_2',
103       'test_type' => [
104         ['name' => 'test_bundle'],
105       ],
106     ];
107
108     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
109
110     $entity_type->expects($this->once())
111       ->method('id')
112       ->willReturn('test');
113     $entity_type->expects($this->once())
114       ->method('hasKey')
115       ->with('bundle')
116       ->will($this->returnValue(TRUE));
117     $entity_type->expects($this->once())
118       ->method('getKey')
119       ->with('bundle')
120       ->will($this->returnValue('test_type'));
121     $entity_type->expects($this->once())
122       ->method('isSubClassOf')
123       ->with(FieldableEntityInterface::class)
124       ->willReturn(TRUE);
125
126     $entity_type->expects($this->once())
127       ->method('getBundleEntityType')
128       ->will($this->returnValue('test_bundle'));
129
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'));
134
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));
139
140     $base_definitions = [
141       'test_type' => $entity_type_definition,
142     ];
143
144     $this->entityManager->expects($this->at(0))
145       ->method('getDefinition')
146       ->with('test')
147       ->will($this->returnValue($entity_type));
148     $this->entityManager->expects($this->at(1))
149       ->method('getBaseFieldDefinitions')
150       ->with('test')
151       ->will($this->returnValue($base_definitions));
152
153     $entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
154     $entity_query_mock->expects($this->once())
155       ->method('execute')
156       ->will($this->returnValue(['test_bundle' => 'test_bundle']));
157
158     $entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
159     $entity_type_storage->expects($this->once())
160       ->method('getQuery')
161       ->will($this->returnValue($entity_query_mock));
162
163     $this->entityManager->expects($this->at(2))
164       ->method('getStorage')
165       ->with('test_bundle')
166       ->will($this->returnValue($entity_type_storage));
167
168     $key_1 = $this->getMock(FieldItemListInterface::class);
169     $key_2 = $this->getMock(FieldItemListInterface::class);
170
171     $entity = $this->getMock(FieldableEntityInterface::class);
172     $entity->expects($this->at(0))
173       ->method('get')
174       ->with('key_1')
175       ->willReturn($key_1);
176     $entity->expects($this->at(1))
177       ->method('get')
178       ->with('key_2')
179       ->willReturn($key_2);
180
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',
185     ];
186
187     $storage->expects($this->once())
188       ->method('create')
189       ->with($expected_test_data)
190       ->will($this->returnValue($entity));
191
192     $this->entityManager->expects($this->at(3))
193       ->method('getStorage')
194       ->with('test')
195       ->will($this->returnValue($storage));
196
197     // Setup expectations for the serializer. This will be called for each field
198     // item.
199     $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
200       ->disableOriginalConstructor()
201       ->setMethods(['denormalize'])
202       ->getMock();
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']);
209
210     $this->entityNormalizer->setSerializer($serializer);
211
212     $this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));
213   }
214
215   /**
216    * Tests the denormalize method with a bundle property.
217    *
218    * @covers ::denormalize
219    */
220   public function testDenormalizeWithInvalidBundle() {
221     $test_data = [
222       'key_1' => 'value_1',
223       'key_2' => 'value_2',
224       'test_type' => [
225         ['name' => 'test_bundle'],
226       ],
227     ];
228
229     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
230
231     $entity_type->expects($this->once())
232       ->method('id')
233       ->willReturn('test');
234     $entity_type->expects($this->once())
235       ->method('hasKey')
236       ->with('bundle')
237       ->will($this->returnValue(TRUE));
238     $entity_type->expects($this->once())
239       ->method('getKey')
240       ->with('bundle')
241       ->will($this->returnValue('test_type'));
242     $entity_type->expects($this->once())
243       ->method('isSubClassOf')
244       ->with(FieldableEntityInterface::class)
245       ->willReturn(TRUE);
246
247     $entity_type->expects($this->once())
248       ->method('getBundleEntityType')
249       ->will($this->returnValue('test_bundle'));
250
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'));
255
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));
260
261     $base_definitions = [
262       'test_type' => $entity_type_definition,
263     ];
264
265     $this->entityManager->expects($this->at(0))
266       ->method('getDefinition')
267       ->with('test')
268       ->will($this->returnValue($entity_type));
269     $this->entityManager->expects($this->at(1))
270       ->method('getBaseFieldDefinitions')
271       ->with('test')
272       ->will($this->returnValue($base_definitions));
273
274     $entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
275     $entity_query_mock->expects($this->once())
276       ->method('execute')
277       ->will($this->returnValue(['test_bundle_other' => 'test_bundle_other']));
278
279     $entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
280     $entity_type_storage->expects($this->once())
281       ->method('getQuery')
282       ->will($this->returnValue($entity_query_mock));
283
284     $this->entityManager->expects($this->at(2))
285       ->method('getStorage')
286       ->with('test_bundle')
287       ->will($this->returnValue($entity_type_storage));
288
289     $this->setExpectedException(UnexpectedValueException::class);
290     $this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']);
291   }
292
293   /**
294    * Tests the denormalize method with no bundle defined.
295    *
296    * @covers ::denormalize
297    */
298   public function testDenormalizeWithNoBundle() {
299     $test_data = [
300       'key_1' => 'value_1',
301       'key_2' => 'value_2',
302     ];
303
304     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
305     $entity_type->expects($this->once())
306       ->method('hasKey')
307       ->with('bundle')
308       ->will($this->returnValue(FALSE));
309     $entity_type->expects($this->never())
310       ->method('getKey');
311
312     $this->entityManager->expects($this->once())
313       ->method('getDefinition')
314       ->with('test')
315       ->will($this->returnValue($entity_type));
316
317     $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
318     $storage->expects($this->once())
319       ->method('create')
320       ->with($test_data)
321       ->will($this->returnValue($this->getMock('Drupal\Core\Entity\EntityInterface')));
322
323     $this->entityManager->expects($this->once())
324       ->method('getStorage')
325       ->with('test')
326       ->will($this->returnValue($storage));
327
328     $this->entityManager->expects($this->never())
329       ->method('getBaseFieldDefinitions');
330
331     $this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));
332   }
333
334 }