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