f217b97fbac0f14da4509308895adf0ef2bff058
[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 getExpectedNormalizedEntity() {
85     $feed = Feed::load($this->entity->getFeedId());
86
87     return [
88       'iid' => [
89         [
90           'value' => 1,
91         ],
92       ],
93       'langcode' => [
94         [
95           'value' => 'en',
96         ],
97       ],
98       'fid' => [
99         [
100           'target_id' => 1,
101           'target_type' => 'aggregator_feed',
102           'target_uuid' => $feed->uuid(),
103           'url' => base_path() . 'aggregator/sources/1',
104         ],
105       ],
106       'title' => [
107         [
108           'value' => 'Llama',
109         ],
110       ],
111       'link' => [
112         [
113           'value' => 'https://www.drupal.org/',
114         ],
115       ],
116       'author' => [],
117       'description' => [],
118       'timestamp' => [
119         $this->formatExpectedTimestampItemValues(123456789),
120       ],
121       'guid' => [],
122     ];
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   protected function getNormalizedPostEntity() {
129     return [
130       'fid' => [
131         [
132           'target_id' => 1,
133         ],
134       ],
135       'title' => [
136         [
137           'value' => 'Llama',
138         ],
139       ],
140       'link' => [
141         [
142           'value' => 'https://www.drupal.org/',
143         ],
144       ],
145     ];
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   protected function getExpectedCacheContexts() {
152     // @see ::createEntity()
153     return ['user.permissions'];
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   protected function getExpectedUnauthorizedAccessMessage($method) {
160     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
161       return parent::getExpectedUnauthorizedAccessMessage($method);
162     }
163
164     switch ($method) {
165       case 'GET':
166         return "The 'access news feeds' permission is required.";
167
168       case 'POST':
169       case 'PATCH':
170       case 'DELETE':
171         return "The 'administer news feeds' permission is required.";
172
173       default:
174         return parent::getExpectedUnauthorizedAccessMessage($method);
175     }
176   }
177
178 }