Version 1
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / Shortcut / ShortcutResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\Shortcut;
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       ],
62     ]);
63     $shortcut->save();
64
65     return $shortcut;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function getExpectedNormalizedEntity() {
72     return [
73       'uuid' => [
74         [
75           'value' => $this->entity->uuid(),
76         ],
77       ],
78       'id' => [
79         [
80           'value' => (int) $this->entity->id(),
81         ],
82       ],
83       'title' => [
84         [
85           'value' => 'Comments',
86         ],
87       ],
88       'shortcut_set' => [
89         [
90           'target_id' => 'default',
91           'target_type' => 'shortcut_set',
92           'target_uuid' => ShortcutSet::load('default')->uuid(),
93         ],
94       ],
95       'link' => [
96         [
97           'uri' => 'internal:/admin/content/comment',
98           'title' => NULL,
99           'options' => [],
100         ],
101       ],
102       'weight' => [
103         [
104           'value' => -20,
105         ],
106       ],
107       'langcode' => [
108         [
109           'value' => 'en',
110         ],
111       ],
112       'default_langcode' => [
113         [
114           'value' => TRUE,
115         ],
116       ],
117     ];
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   protected function getNormalizedPostEntity() {
124     return [
125       'title' => [
126         [
127           'value' => 'Comments',
128         ],
129       ],
130       'link' => [
131         [
132           'uri' => 'internal:/',
133         ],
134       ],
135       'shortcut_set' => 'default',
136     ];
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   protected function getExpectedUnauthorizedAccessMessage($method) {
143     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
144       return parent::getExpectedUnauthorizedAccessMessage($method);
145     }
146
147     switch ($method) {
148       case 'GET':
149       case 'POST':
150       case 'PATCH':
151       case 'DELETE':
152         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.";
153
154       default:
155         return parent::getExpectedUnauthorizedAccessMessage($method);
156     }
157   }
158
159 }