3 namespace Drupal\Tests\hal\Kernel;
5 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\entity_test\Entity\EntityTest;
10 * Tests HAL normalization edge cases for EntityResource.
14 class NormalizeTest extends NormalizerTestBase {
19 protected function setUp() {
22 \Drupal::service('router.builder')->rebuild();
26 * Tests the normalize function.
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();
34 // Create a German entity.
37 'name' => $this->randomMachineName(),
38 'field_test_text' => [
39 'value' => $this->randomMachineName(),
40 'format' => 'full_html',
42 'field_test_entity_reference' => [
43 'target_id' => $target_entity_de->id(),
46 // Array of translated values.
47 $translation_values = [
48 'name' => $this->randomMachineName(),
49 'field_test_entity_reference' => [
50 'target_id' => $target_entity_en->id(),
54 $entity = EntityTest::create($values);
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']]);
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();
68 'href' => '/relations',
74 'href' => $this->getEntityUri($entity),
81 'href' => $this->getEntityUri($target_entity_de),
85 'href' => $this->getEntityUri($target_entity_en),
95 'href' => $this->getEntityUri($target_entity_de),
103 'value' => $target_entity_de->uuid(),
111 'href' => $this->getEntityUri($target_entity_en),
119 'value' => $target_entity_en->uuid(),
128 'value' => $entity->id(),
133 'value' => $entity->uuid(),
143 'value' => $values['name'],
147 'value' => $translation_values['name'],
151 'field_test_text' => [
153 'value' => $values['field_test_text']['value'],
154 'format' => $values['field_test_text']['format'],
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.
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.');
172 * Constructs the entity URI.
174 * @param \Drupal\Core\Entity\EntityInterface $entity
180 protected function getEntityUri(EntityInterface $entity) {
181 $url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
182 return $url->setRouteParameter('_format', 'hal_json')->toString();