3 namespace Drupal\Tests\hal\Kernel;
5 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\filter\Entity\FilterFormat;
11 * Tests HAL normalization edge cases for EntityResource.
15 class NormalizeTest extends NormalizerTestBase {
20 protected function setUp() {
23 FilterFormat::create([
24 'format' => 'my_text_format',
25 'name' => 'My Text Format',
32 'allowed_html' => '<p>',
44 \Drupal::service('router.builder')->rebuild();
48 * Tests the normalize function.
50 public function testNormalize() {
51 $target_entity_de = EntityTest::create((['langcode' => 'de', 'field_test_entity_reference' => NULL]));
52 $target_entity_de->save();
53 $target_entity_en = EntityTest::create((['langcode' => 'en', 'field_test_entity_reference' => NULL]));
54 $target_entity_en->save();
56 // Create a German entity.
59 'name' => $this->randomMachineName(),
60 'field_test_text' => [
61 'value' => $this->randomMachineName(),
62 'format' => 'my_text_format',
64 'field_test_entity_reference' => [
65 'target_id' => $target_entity_de->id(),
68 // Array of translated values.
69 $translation_values = [
70 'name' => $this->randomMachineName(),
71 'field_test_entity_reference' => [
72 'target_id' => $target_entity_en->id(),
76 $entity = EntityTest::create($values);
78 // Add an English value for name and entity reference properties.
79 $entity->addTranslation('en')->set('name', [0 => ['value' => $translation_values['name']]]);
80 $entity->getTranslation('en')->set('field_test_entity_reference', [0 => $translation_values['field_test_entity_reference']]);
83 $type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString();
84 $relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', ['absolute' => TRUE])->toString();
90 'href' => '/relations',
96 'href' => $this->getEntityUri($entity),
103 'href' => $this->getEntityUri($target_entity_de),
107 'href' => $this->getEntityUri($target_entity_en),
117 'href' => $this->getEntityUri($target_entity_de),
125 'value' => $target_entity_de->uuid(),
133 'href' => $this->getEntityUri($target_entity_en),
141 'value' => $target_entity_en->uuid(),
150 'value' => $entity->id(),
155 'value' => $entity->uuid(),
165 'value' => $values['name'],
169 'value' => $translation_values['name'],
173 'field_test_text' => [
175 'value' => $values['field_test_text']['value'],
176 'format' => $values['field_test_text']['format'],
177 'processed' => "<p>{$values['field_test_text']['value']}</p>",
182 $normalized = $this->serializer->normalize($entity, $this->format);
183 $this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
184 // @todo Test curies.
186 $this->assertEqual($normalized['id'], $expected_array['id'], 'Internal id is exposed.');
187 $this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.');
188 $this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.');
189 $this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.');
190 $this->assertEqual($normalized['_embedded'][$relation_uri], $expected_array['_embedded'][$relation_uri], 'Entity reference field is normalized.');
191 $this->assertEqual($normalized['_links'][$relation_uri], $expected_array['_links'][$relation_uri], 'Links are added for entity reference field.');
195 * Constructs the entity URI.
197 * @param \Drupal\Core\Entity\EntityInterface $entity
203 protected function getEntityUri(EntityInterface $entity) {
204 $url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
205 return $url->setRouteParameter('_format', 'hal_json')->toString();