1939e04364bfa5d33858d5e51f2726b111556044
[yaffs-website] / web / core / modules / hal / tests / src / Functional / EntityResource / Comment / CommentHalJsonTestBase.php
1 <?php
2
3 namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
7 use Drupal\Tests\rest\Functional\EntityResource\Comment\CommentResourceTestBase;
8 use Drupal\user\Entity\User;
9
10 abstract class CommentHalJsonTestBase extends CommentResourceTestBase {
11
12   use HalEntityNormalizationTrait;
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['hal'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $format = 'hal_json';
23
24   /**
25    * {@inheritdoc}
26    */
27   protected static $mimeType = 'application/hal+json';
28
29   /**
30    * {@inheritdoc}
31    *
32    * The HAL+JSON format causes different PATCH-protected fields. For some
33    * reason, the 'pid' and 'homepage' fields are NOT PATCH-protected, even
34    * though they are for non-HAL+JSON serializations.
35    *
36    * @todo fix in https://www.drupal.org/node/2824271
37    */
38   protected static $patchProtectedFieldNames = [
39     'status',
40     'created',
41     'changed',
42     'thread',
43     'entity_type',
44     'field_name',
45     'entity_id',
46     'uid',
47   ];
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function getExpectedNormalizedEntity() {
53     $default_normalization = parent::getExpectedNormalizedEntity();
54
55     $normalization = $this->applyHalFieldNormalization($default_normalization);
56
57     // Because \Drupal\comment\Entity\Comment::getOwner() generates an in-memory
58     // User entity without a UUID, we cannot use it.
59     $author = User::load($this->entity->getOwnerId());
60     $commented_entity = EntityTest::load(1);
61     return $normalization + [
62       '_links' => [
63         'self' => [
64           'href' => $this->baseUrl . '/comment/1?_format=hal_json',
65         ],
66         'type' => [
67           'href' => $this->baseUrl . '/rest/type/comment/comment',
68         ],
69         $this->baseUrl . '/rest/relation/comment/comment/entity_id' => [
70           [
71             'href' => $this->baseUrl . '/entity_test/1?_format=hal_json',
72           ],
73         ],
74         $this->baseUrl . '/rest/relation/comment/comment/uid' => [
75           [
76             'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
77             'lang' => 'en',
78           ],
79         ],
80       ],
81       '_embedded' => [
82         $this->baseUrl . '/rest/relation/comment/comment/entity_id' => [
83           [
84             '_links' => [
85               'self' => [
86                 'href' => $this->baseUrl . '/entity_test/1?_format=hal_json',
87               ],
88               'type' => [
89                 'href' => $this->baseUrl . '/rest/type/entity_test/bar',
90               ],
91             ],
92             'uuid' => [
93               ['value' => $commented_entity->uuid()]
94             ],
95           ],
96         ],
97         $this->baseUrl . '/rest/relation/comment/comment/uid' => [
98           [
99             '_links' => [
100               'self' => [
101                 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
102               ],
103               'type' => [
104                 'href' => $this->baseUrl . '/rest/type/user/user',
105               ],
106             ],
107             'uuid' => [
108               ['value' => $author->uuid()]
109             ],
110             'lang' => 'en',
111           ],
112         ],
113       ],
114     ];
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   protected function getNormalizedPostEntity() {
121     return parent::getNormalizedPostEntity() + [
122       '_links' => [
123         'type' => [
124           'href' => $this->baseUrl . '/rest/type/comment/comment',
125         ],
126       ],
127     ];
128   }
129
130 }