c7bd57a5c6186148c1670d18b5458290bb7b1417
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / Item / ItemResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\Item;
4
5 use Drupal\aggregator\Entity\Feed;
6 use Drupal\aggregator\Entity\Item;
7 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
8 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
9
10 /**
11  * ResourceTestBase for Item entity.
12  */
13 abstract class ItemResourceTestBase extends EntityResourceTestBase {
14
15   use BcTimestampNormalizerUnixTestTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['aggregator'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected static $entityTypeId = 'aggregator_item';
26
27   /**
28    * {@inheritdoc}
29    */
30   protected static $patchProtectedFieldNames = [];
31
32   /**
33    * The Item entity.
34    *
35    * @var \Drupal\aggregator\ItemInterface
36    */
37   protected $entity;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUpAuthorization($method) {
43     switch ($method) {
44       case 'GET':
45         $this->grantPermissionsToTestedRole(['access news feeds']);
46         break;
47
48       case 'POST':
49       case 'PATCH':
50       case 'DELETE':
51         $this->grantPermissionsToTestedRole(['administer news feeds']);
52         break;
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function createEntity() {
60     // Create a "Camelids" feed.
61     $feed = Feed::create([
62       'title' => 'Camelids',
63       'url' => 'https://groups.drupal.org/not_used/167169',
64       'refresh' => 900,
65       'checked' => 1389919932,
66       'description' => 'Drupal Core Group feed',
67     ]);
68     $feed->save();
69
70     // Create a "Llama" item.
71     $item = Item::create();
72     $item->setTitle('Llama')
73       ->setFeedId($feed->id())
74       ->setLink('https://www.drupal.org/')
75       ->setPostedTime(123456789)
76       ->save();
77
78     return $item;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   protected function createAnotherEntity() {
85     $entity = $this->entity->createDuplicate();
86     $entity->setLink('https://www.example.org/');
87     $label_key = $entity->getEntityType()->getKey('label');
88     if ($label_key) {
89       $entity->set($label_key, $entity->label() . '_dupe');
90     }
91     $entity->save();
92     return $entity;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   protected function getExpectedNormalizedEntity() {
99     $feed = Feed::load($this->entity->getFeedId());
100
101     return [
102       'iid' => [
103         [
104           'value' => 1,
105         ],
106       ],
107       'langcode' => [
108         [
109           'value' => 'en',
110         ],
111       ],
112       'fid' => [
113         [
114           'target_id' => 1,
115           'target_type' => 'aggregator_feed',
116           'target_uuid' => $feed->uuid(),
117           'url' => base_path() . 'aggregator/sources/1',
118         ],
119       ],
120       'title' => [
121         [
122           'value' => 'Llama',
123         ],
124       ],
125       'link' => [
126         [
127           'value' => 'https://www.drupal.org/',
128         ],
129       ],
130       'author' => [],
131       'description' => [],
132       'timestamp' => [
133         $this->formatExpectedTimestampItemValues(123456789),
134       ],
135       'guid' => [],
136     ];
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   protected function getNormalizedPostEntity() {
143     return [
144       'fid' => [
145         [
146           'target_id' => 1,
147         ],
148       ],
149       'title' => [
150         [
151           'value' => 'Llama',
152         ],
153       ],
154       'link' => [
155         [
156           'value' => 'https://www.drupal.org/',
157         ],
158       ],
159     ];
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   protected function getExpectedCacheContexts() {
166     // @see ::createEntity()
167     return ['user.permissions'];
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   protected function getExpectedUnauthorizedAccessMessage($method) {
174     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
175       return parent::getExpectedUnauthorizedAccessMessage($method);
176     }
177
178     switch ($method) {
179       case 'GET':
180         return "The 'access news feeds' permission is required.";
181
182       case 'POST':
183       case 'PATCH':
184       case 'DELETE':
185         return "The 'administer news feeds' permission is required.";
186
187       default:
188         return parent::getExpectedUnauthorizedAccessMessage($method);
189     }
190   }
191
192 }