f0ff8f85f9059ef55fbf6832d565de7f7815146f
[yaffs-website] / web / core / modules / contact / tests / src / Functional / Rest / MessageResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\contact\Functional\Rest;
4
5 use Drupal\contact\Entity\ContactForm;
6 use Drupal\contact\Entity\Message;
7 use Drupal\Core\Url;
8 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
9 use Symfony\Component\Routing\Exception\RouteNotFoundException;
10
11 abstract class MessageResourceTestBase extends EntityResourceTestBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public static $modules = ['contact'];
17
18   /**
19    * {@inheritdoc}
20    */
21   protected static $entityTypeId = 'contact_message';
22
23   /**
24    * {@inheritdoc}
25    */
26   protected static $labelFieldName = 'subject';
27
28   /**
29    * The Message entity.
30    *
31    * @var \Drupal\contact\MessageInterface
32    */
33   protected $entity;
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUpAuthorization($method) {
39     $this->grantPermissionsToTestedRole(['access site-wide contact form']);
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function createEntity() {
46     if (!ContactForm::load('camelids')) {
47       // Create a "Camelids" contact form.
48       ContactForm::create([
49         'id' => 'camelids',
50         'label' => 'Llama',
51         'message' => 'Let us know what you think about llamas',
52         'reply' => 'Llamas are indeed awesome!',
53         'recipients' => [
54           'llama@example.com',
55           'contact@example.com',
56         ],
57       ])->save();
58     }
59
60     $message = Message::create([
61       'contact_form' => 'camelids',
62       'subject' => 'Llama Gabilondo',
63       'message' => 'Llamas are awesome!',
64     ]);
65     $message->save();
66
67     return $message;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   protected function getNormalizedPostEntity() {
74     return [
75       'subject' => [
76         [
77           'value' => 'Dramallama',
78         ],
79       ],
80       'contact_form' => [
81         [
82           'target_id' => 'camelids',
83         ],
84       ],
85       'message' => [
86         [
87           'value' => 'http://www.urbandictionary.com/define.php?term=drama%20llama',
88         ],
89       ],
90     ];
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   protected function getExpectedNormalizedEntity() {
97     throw new \Exception('Not yet supported.');
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   protected function getExpectedUnauthorizedAccessMessage($method) {
104     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
105       return parent::getExpectedUnauthorizedAccessMessage($method);
106     }
107
108     if ($method === 'POST') {
109       return "The 'access site-wide contact form' permission is required.";
110     }
111     return parent::getExpectedUnauthorizedAccessMessage($method);
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function testGet() {
118     // Contact Message entities are not stored, so they cannot be retrieved.
119     $this->setExpectedException(RouteNotFoundException::class, 'Route "rest.entity.contact_message.GET" does not exist.');
120
121     $this->provisionEntityResource();
122     Url::fromRoute('rest.entity.contact_message.GET')->toString(TRUE);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function testPatch() {
129     // Contact Message entities are not stored, so they cannot be modified.
130     $this->setExpectedException(RouteNotFoundException::class, 'Route "rest.entity.contact_message.PATCH" does not exist.');
131
132     $this->provisionEntityResource();
133     Url::fromRoute('rest.entity.contact_message.PATCH')->toString(TRUE);
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function testDelete() {
140     // Contact Message entities are not stored, so they cannot be deleted.
141     $this->setExpectedException(RouteNotFoundException::class, 'Route "rest.entity.contact_message.DELETE" does not exist.');
142
143     $this->provisionEntityResource();
144     Url::fromRoute('rest.entity.contact_message.DELETE')->toString(TRUE);
145   }
146
147 }