Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\Tests\jsonapi\Functional;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Component\Utility\NestedArray;
|
Chris@18
|
6 use Drupal\contact\Entity\ContactForm;
|
Chris@18
|
7 use Drupal\contact\Entity\Message;
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@18
|
9 use GuzzleHttp\RequestOptions;
|
Chris@18
|
10 use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
Chris@18
|
11
|
Chris@18
|
12 /**
|
Chris@18
|
13 * JSON:API integration test for the "Message" content entity type.
|
Chris@18
|
14 *
|
Chris@18
|
15 * @group jsonapi
|
Chris@18
|
16 */
|
Chris@18
|
17 class MessageTest extends ResourceTestBase {
|
Chris@18
|
18
|
Chris@18
|
19 /**
|
Chris@18
|
20 * {@inheritdoc}
|
Chris@18
|
21 */
|
Chris@18
|
22 public static $modules = ['contact'];
|
Chris@18
|
23
|
Chris@18
|
24 /**
|
Chris@18
|
25 * {@inheritdoc}
|
Chris@18
|
26 */
|
Chris@18
|
27 protected static $entityTypeId = 'contact_message';
|
Chris@18
|
28
|
Chris@18
|
29 /**
|
Chris@18
|
30 * {@inheritdoc}
|
Chris@18
|
31 */
|
Chris@18
|
32 protected static $resourceTypeName = 'contact_message--camelids';
|
Chris@18
|
33
|
Chris@18
|
34 /**
|
Chris@18
|
35 * {@inheritdoc}
|
Chris@18
|
36 *
|
Chris@18
|
37 * @var \Drupal\contact\MessageInterface
|
Chris@18
|
38 */
|
Chris@18
|
39 protected $entity;
|
Chris@18
|
40
|
Chris@18
|
41 /**
|
Chris@18
|
42 * {@inheritdoc}
|
Chris@18
|
43 */
|
Chris@18
|
44 protected static $labelFieldName = 'subject';
|
Chris@18
|
45
|
Chris@18
|
46 /**
|
Chris@18
|
47 * {@inheritdoc}
|
Chris@18
|
48 */
|
Chris@18
|
49 protected function setUpAuthorization($method) {
|
Chris@18
|
50 $this->grantPermissionsToTestedRole(['access site-wide contact form']);
|
Chris@18
|
51 }
|
Chris@18
|
52
|
Chris@18
|
53 /**
|
Chris@18
|
54 * {@inheritdoc}
|
Chris@18
|
55 */
|
Chris@18
|
56 protected function createEntity() {
|
Chris@18
|
57 if (!ContactForm::load('camelids')) {
|
Chris@18
|
58 // Create a "Camelids" contact form.
|
Chris@18
|
59 ContactForm::create([
|
Chris@18
|
60 'id' => 'camelids',
|
Chris@18
|
61 'label' => 'Llama',
|
Chris@18
|
62 'message' => 'Let us know what you think about llamas',
|
Chris@18
|
63 'reply' => 'Llamas are indeed awesome!',
|
Chris@18
|
64 'recipients' => [
|
Chris@18
|
65 'llama@example.com',
|
Chris@18
|
66 'contact@example.com',
|
Chris@18
|
67 ],
|
Chris@18
|
68 ])->save();
|
Chris@18
|
69 }
|
Chris@18
|
70
|
Chris@18
|
71 $message = Message::create([
|
Chris@18
|
72 'contact_form' => 'camelids',
|
Chris@18
|
73 'subject' => 'Llama Gabilondo',
|
Chris@18
|
74 'message' => 'Llamas are awesome!',
|
Chris@18
|
75 ]);
|
Chris@18
|
76 $message->save();
|
Chris@18
|
77
|
Chris@18
|
78 return $message;
|
Chris@18
|
79 }
|
Chris@18
|
80
|
Chris@18
|
81 /**
|
Chris@18
|
82 * {@inheritdoc}
|
Chris@18
|
83 */
|
Chris@18
|
84 protected function getExpectedDocument() {
|
Chris@18
|
85 throw new \Exception('Not yet supported.');
|
Chris@18
|
86 }
|
Chris@18
|
87
|
Chris@18
|
88 /**
|
Chris@18
|
89 * {@inheritdoc}
|
Chris@18
|
90 */
|
Chris@18
|
91 protected function getPostDocument() {
|
Chris@18
|
92 return [
|
Chris@18
|
93 'data' => [
|
Chris@18
|
94 'type' => 'contact_message--camelids',
|
Chris@18
|
95 'attributes' => [
|
Chris@18
|
96 'subject' => 'Dramallama',
|
Chris@18
|
97 'message' => 'http://www.urbandictionary.com/define.php?term=drama%20llama',
|
Chris@18
|
98 ],
|
Chris@18
|
99 ],
|
Chris@18
|
100 ];
|
Chris@18
|
101 }
|
Chris@18
|
102
|
Chris@18
|
103 /**
|
Chris@18
|
104 * {@inheritdoc}
|
Chris@18
|
105 */
|
Chris@18
|
106 protected function getExpectedUnauthorizedAccessMessage($method) {
|
Chris@18
|
107 if ($method === 'POST') {
|
Chris@18
|
108 return "The 'access site-wide contact form' permission is required.";
|
Chris@18
|
109 }
|
Chris@18
|
110 return parent::getExpectedUnauthorizedAccessMessage($method);
|
Chris@18
|
111 }
|
Chris@18
|
112
|
Chris@18
|
113 /**
|
Chris@18
|
114 * {@inheritdoc}
|
Chris@18
|
115 */
|
Chris@18
|
116 public function testGetIndividual() {
|
Chris@18
|
117 // Contact Message entities are not stored, so they cannot be retrieved.
|
Chris@18
|
118 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
Chris@18
|
119
|
Chris@18
|
120 Url::fromRoute('jsonapi.contact_message--camelids.individual')->toString(TRUE);
|
Chris@18
|
121 }
|
Chris@18
|
122
|
Chris@18
|
123 /**
|
Chris@18
|
124 * {@inheritdoc}
|
Chris@18
|
125 */
|
Chris@18
|
126 public function testPatchIndividual() {
|
Chris@18
|
127 // Contact Message entities are not stored, so they cannot be modified.
|
Chris@18
|
128 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
Chris@18
|
129
|
Chris@18
|
130 Url::fromRoute('jsonapi.contact_message--camelids.individual')->toString(TRUE);
|
Chris@18
|
131 }
|
Chris@18
|
132
|
Chris@18
|
133 /**
|
Chris@18
|
134 * {@inheritdoc}
|
Chris@18
|
135 */
|
Chris@18
|
136 public function testDeleteIndividual() {
|
Chris@18
|
137 // Contact Message entities are not stored, so they cannot be deleted.
|
Chris@18
|
138 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
Chris@18
|
139
|
Chris@18
|
140 Url::fromRoute('jsonapi.contact_message--camelids.individual')->toString(TRUE);
|
Chris@18
|
141 }
|
Chris@18
|
142
|
Chris@18
|
143 /**
|
Chris@18
|
144 * {@inheritdoc}
|
Chris@18
|
145 */
|
Chris@18
|
146 public function testRelated() {
|
Chris@18
|
147 // Contact Message entities are not stored, so they cannot be retrieved.
|
Chris@18
|
148 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.related" does not exist.');
|
Chris@18
|
149
|
Chris@18
|
150 Url::fromRoute('jsonapi.contact_message--camelids.related')->toString(TRUE);
|
Chris@18
|
151 }
|
Chris@18
|
152
|
Chris@18
|
153 /**
|
Chris@18
|
154 * {@inheritdoc}
|
Chris@18
|
155 */
|
Chris@18
|
156 public function testRelationships() {
|
Chris@18
|
157 // Contact Message entities are not stored, so they cannot be retrieved.
|
Chris@18
|
158 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.relationship.get" does not exist.');
|
Chris@18
|
159
|
Chris@18
|
160 Url::fromRoute('jsonapi.contact_message--camelids.relationship.get')->toString(TRUE);
|
Chris@18
|
161 }
|
Chris@18
|
162
|
Chris@18
|
163 /**
|
Chris@18
|
164 * {@inheritdoc}
|
Chris@18
|
165 */
|
Chris@18
|
166 public function testCollection() {
|
Chris@18
|
167 $collection_url = Url::fromRoute('jsonapi.contact_message--camelids.collection.post')->setAbsolute(TRUE);
|
Chris@18
|
168 $request_options = [];
|
Chris@18
|
169 $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
|
Chris@18
|
170 $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
|
Chris@18
|
171
|
Chris@18
|
172 // 405 because Message entities are not stored, so they cannot be retrieved,
|
Chris@18
|
173 // yet the same URL can be used to POST them.
|
Chris@18
|
174 $response = $this->request('GET', $collection_url, $request_options);
|
Chris@18
|
175 $this->assertSame(405, $response->getStatusCode());
|
Chris@18
|
176 $this->assertSame(['POST'], $response->getHeader('Allow'));
|
Chris@18
|
177 }
|
Chris@18
|
178
|
Chris@18
|
179 /**
|
Chris@18
|
180 * {@inheritdoc}
|
Chris@18
|
181 */
|
Chris@18
|
182 public function testRevisions() {
|
Chris@18
|
183 // Contact Message entities are not stored, so they cannot be retrieved.
|
Chris@18
|
184 $this->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
Chris@18
|
185
|
Chris@18
|
186 Url::fromRoute('jsonapi.contact_message--camelids.individual')->toString(TRUE);
|
Chris@18
|
187 }
|
Chris@18
|
188
|
Chris@18
|
189 }
|