comparison core/modules/serialization/tests/src/Kernel/EntityResolverTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\serialization\Kernel;
4
5 use Drupal\Core\Url;
6 use Drupal\entity_test\Entity\EntityTestMulRev;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11 * Tests that entities references can be resolved.
12 *
13 * @group serialization
14 */
15 class EntityResolverTest extends NormalizerTestBase {
16
17 /**
18 * Modules to enable.
19 *
20 * @var array
21 */
22 public static $modules = ['hal', 'rest'];
23
24 /**
25 * The format being tested.
26 *
27 * @var string
28 */
29 protected $format = 'hal_json';
30
31 protected function setUp() {
32 parent::setUp();
33
34 \Drupal::service('router.builder')->rebuild();
35
36 // Create the test field storage.
37 FieldStorageConfig::create([
38 'entity_type' => 'entity_test_mulrev',
39 'field_name' => 'field_test_entity_reference',
40 'type' => 'entity_reference',
41 'settings' => [
42 'target_type' => 'entity_test_mulrev',
43 ],
44 ])->save();
45
46 // Create the test field.
47 FieldConfig::create([
48 'entity_type' => 'entity_test_mulrev',
49 'field_name' => 'field_test_entity_reference',
50 'bundle' => 'entity_test_mulrev',
51 ])->save();
52 }
53
54 /**
55 * Test that fields referencing UUIDs can be denormalized.
56 */
57 public function testUuidEntityResolver() {
58 // Create an entity to get the UUID from.
59 $entity = EntityTestMulRev::create(['type' => 'entity_test_mulrev']);
60 $entity->set('name', 'foobar');
61 $entity->set('field_test_entity_reference', [['target_id' => 1]]);
62 $entity->save();
63
64 $field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', ['absolute' => TRUE])->toString();
65
66 $data = [
67 '_links' => [
68 'type' => [
69 'href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', ['absolute' => TRUE])->toString(),
70 ],
71 $field_uri => [
72 [
73 'href' => $entity->url(),
74 ],
75 ],
76 ],
77 '_embedded' => [
78 $field_uri => [
79 [
80 '_links' => [
81 'self' => $entity->url(),
82 ],
83 'uuid' => [
84 [
85 'value' => $entity->uuid(),
86 ],
87 ],
88 ],
89 ],
90 ],
91 ];
92
93 $denormalized = $this->container->get('serializer')->denormalize($data, 'Drupal\entity_test\Entity\EntityTestMulRev', $this->format);
94 $field_value = $denormalized->get('field_test_entity_reference')->getValue();
95 $this->assertEqual($field_value[0]['target_id'], 1, 'Entity reference resolved using UUID.');
96 }
97
98 }