d837b79a02eef84c3165782b337b5b94685633eb
[yaffs-website] / web / core / modules / hal / tests / src / Kernel / NormalizeTest.php
1 <?php
2
3 namespace Drupal\Tests\hal\Kernel;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Url;
7 use Drupal\entity_test\Entity\EntityTest;
8
9 /**
10  * Tests HAL normalization edge cases for EntityResource.
11  *
12  * @group hal
13  */
14 class NormalizeTest extends NormalizerTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     parent::setUp();
21
22     \Drupal::service('router.builder')->rebuild();
23   }
24
25   /**
26    * Tests the normalize function.
27    */
28   public function testNormalize() {
29     $target_entity_de = EntityTest::create((['langcode' => 'de', 'field_test_entity_reference' => NULL]));
30     $target_entity_de->save();
31     $target_entity_en = EntityTest::create((['langcode' => 'en', 'field_test_entity_reference' => NULL]));
32     $target_entity_en->save();
33
34     // Create a German entity.
35     $values = [
36       'langcode' => 'de',
37       'name' => $this->randomMachineName(),
38       'field_test_text' => [
39         'value' => $this->randomMachineName(),
40         'format' => 'full_html',
41       ],
42       'field_test_entity_reference' => [
43         'target_id' => $target_entity_de->id(),
44       ],
45     ];
46     // Array of translated values.
47     $translation_values = [
48       'name' => $this->randomMachineName(),
49       'field_test_entity_reference' => [
50         'target_id' => $target_entity_en->id(),
51       ]
52     ];
53
54     $entity = EntityTest::create($values);
55     $entity->save();
56     // Add an English value for name and entity reference properties.
57     $entity->addTranslation('en')->set('name', [0 => ['value' => $translation_values['name']]]);
58     $entity->getTranslation('en')->set('field_test_entity_reference', [0 => $translation_values['field_test_entity_reference']]);
59     $entity->save();
60
61     $type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString();
62     $relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', ['absolute' => TRUE])->toString();
63
64     $expected_array = [
65       '_links' => [
66         'curies' => [
67           [
68             'href' => '/relations',
69             'name' => 'site',
70             'templated' => TRUE,
71           ],
72         ],
73         'self' => [
74           'href' => $this->getEntityUri($entity),
75         ],
76         'type' => [
77           'href' => $type_uri,
78         ],
79         $relation_uri => [
80           [
81             'href' => $this->getEntityUri($target_entity_de),
82             'lang' => 'de',
83           ],
84           [
85             'href' => $this->getEntityUri($target_entity_en),
86             'lang' => 'en',
87           ],
88         ],
89       ],
90       '_embedded' => [
91         $relation_uri => [
92           [
93             '_links' => [
94               'self' => [
95                 'href' => $this->getEntityUri($target_entity_de),
96               ],
97               'type' => [
98                 'href' => $type_uri,
99               ],
100             ],
101             'uuid' => [
102               [
103                 'value' => $target_entity_de->uuid(),
104               ],
105             ],
106             'lang' => 'de',
107           ],
108           [
109             '_links' => [
110               'self' => [
111                 'href' => $this->getEntityUri($target_entity_en),
112               ],
113               'type' => [
114                 'href' => $type_uri,
115               ],
116             ],
117             'uuid' => [
118               [
119                 'value' => $target_entity_en->uuid(),
120               ],
121             ],
122             'lang' => 'en',
123           ],
124         ],
125       ],
126       'id' => [
127         [
128           'value' => $entity->id(),
129         ],
130       ],
131       'uuid' => [
132         [
133           'value' => $entity->uuid(),
134         ],
135       ],
136       'langcode' => [
137         [
138           'value' => 'de',
139         ],
140       ],
141       'name' => [
142         [
143           'value' => $values['name'],
144           'lang' => 'de',
145         ],
146         [
147           'value' => $translation_values['name'],
148           'lang' => 'en',
149         ],
150       ],
151       'field_test_text' => [
152         [
153           'value' => $values['field_test_text']['value'],
154           'format' => $values['field_test_text']['format'],
155         ],
156       ],
157     ];
158
159     $normalized = $this->serializer->normalize($entity, $this->format);
160     $this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
161     // @todo Test curies.
162     // @todo Test type.
163     $this->assertEqual($normalized['id'], $expected_array['id'], 'Internal id is exposed.');
164     $this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.');
165     $this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.');
166     $this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.');
167     $this->assertEqual($normalized['_embedded'][$relation_uri], $expected_array['_embedded'][$relation_uri], 'Entity reference field is normalized.');
168     $this->assertEqual($normalized['_links'][$relation_uri], $expected_array['_links'][$relation_uri], 'Links are added for entity reference field.');
169   }
170
171   /**
172    * Constructs the entity URI.
173    *
174    * @param \Drupal\Core\Entity\EntityInterface $entity
175    *   The entity.
176    *
177    * @return string
178    *   The entity URI.
179    */
180   protected function getEntityUri(EntityInterface $entity) {
181     $url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
182     return $url->setRouteParameter('_format', 'hal_json')->toString();
183   }
184
185 }