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