1f588a250098b8ea028c598dce5630f2234fba8e
[yaffs-website] / web / core / modules / shortcut / tests / src / Functional / Rest / ShortcutResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\shortcut\Functional\Rest;
4
5 use Drupal\shortcut\Entity\Shortcut;
6 use Drupal\shortcut\Entity\ShortcutSet;
7 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
8
9 /**
10  * ResourceTestBase for Shortcut entity.
11  */
12 abstract class ShortcutResourceTestBase extends EntityResourceTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['comment', 'shortcut'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $entityTypeId = 'shortcut';
23
24   /**
25    * {@inheritdoc}
26    */
27   protected static $patchProtectedFieldNames = [];
28
29   /**
30    * The Shortcut entity.
31    *
32    * @var \Drupal\shortcut\ShortcutInterface
33    */
34   protected $entity;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUpAuthorization($method) {
40     switch ($method) {
41       case 'GET':
42       case 'POST':
43       case 'PATCH':
44       case 'DELETE':
45         $this->grantPermissionsToTestedRole(['access shortcuts', 'customize shortcut links']);
46         break;
47     }
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function createEntity() {
54     // Create shortcut.
55     $shortcut = Shortcut::create([
56       'shortcut_set' => 'default',
57       'title' => t('Comments'),
58       'weight' => -20,
59       'link' => [
60         'uri' => 'internal:/admin/content/comment',
61         'options' => [
62           'fragment' => 'new',
63         ],
64       ],
65     ]);
66     $shortcut->save();
67
68     return $shortcut;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function getExpectedNormalizedEntity() {
75     return [
76       'uuid' => [
77         [
78           'value' => $this->entity->uuid(),
79         ],
80       ],
81       'id' => [
82         [
83           'value' => (int) $this->entity->id(),
84         ],
85       ],
86       'title' => [
87         [
88           'value' => 'Comments',
89         ],
90       ],
91       'shortcut_set' => [
92         [
93           'target_id' => 'default',
94           'target_type' => 'shortcut_set',
95           'target_uuid' => ShortcutSet::load('default')->uuid(),
96         ],
97       ],
98       'link' => [
99         [
100           'uri' => 'internal:/admin/content/comment',
101           'title' => NULL,
102           'options' => [
103             'fragment' => 'new',
104           ],
105         ],
106       ],
107       'weight' => [
108         [
109           'value' => -20,
110         ],
111       ],
112       'langcode' => [
113         [
114           'value' => 'en',
115         ],
116       ],
117       'default_langcode' => [
118         [
119           'value' => TRUE,
120         ],
121       ],
122     ];
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   protected function getNormalizedPostEntity() {
129     return [
130       'title' => [
131         [
132           'value' => 'Comments',
133         ],
134       ],
135       'link' => [
136         [
137           'uri' => 'internal:/',
138         ],
139       ],
140       'shortcut_set' => [
141         [
142           'target_id' => 'default',
143         ],
144       ],
145     ];
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   protected function getExpectedUnauthorizedAccessMessage($method) {
152     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
153       return parent::getExpectedUnauthorizedAccessMessage($method);
154     }
155
156     switch ($method) {
157       case 'GET':
158       case 'POST':
159       case 'PATCH':
160       case 'DELETE':
161         return "The shortcut set must be the currently displayed set for the user and the user must have 'access shortcuts' AND 'customize shortcut links' permissions.";
162
163       default:
164         return parent::getExpectedUnauthorizedAccessMessage($method);
165     }
166   }
167
168 }