Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / Term / TermResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\Term;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
8
9 abstract class TermResourceTestBase extends EntityResourceTestBase {
10
11   /**
12    * {@inheritdoc}
13    */
14   public static $modules = ['taxonomy'];
15
16   /**
17    * {@inheritdoc}
18    */
19   protected static $entityTypeId = 'taxonomy_term';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected static $patchProtectedFieldNames = [
25     'changed',
26   ];
27
28   /**
29    * @var \Drupal\taxonomy\TermInterface
30    */
31   protected $entity;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUpAuthorization($method) {
37     switch ($method) {
38       case 'GET':
39         $this->grantPermissionsToTestedRole(['access content']);
40         break;
41       case 'POST':
42       case 'PATCH':
43       case 'DELETE':
44         // @todo Update once https://www.drupal.org/node/2824408 lands.
45         $this->grantPermissionsToTestedRole(['administer taxonomy']);
46         break;
47     }
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function createEntity() {
54     $vocabulary = Vocabulary::load('camelids');
55     if (!$vocabulary) {
56       // Create a "Camelids" vocabulary.
57       $vocabulary = Vocabulary::create([
58         'name' => 'Camelids',
59         'vid' => 'camelids',
60       ]);
61       $vocabulary->save();
62     }
63
64     // Create a "Llama" taxonomy term.
65     $term = Term::create(['vid' => $vocabulary->id()])
66       ->setName('Llama')
67       ->setChangedTime(123456789);
68     $term->save();
69
70     return $term;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function getExpectedNormalizedEntity() {
77     return [
78       'tid' => [
79         ['value' => 1],
80       ],
81       'uuid' => [
82         ['value' => $this->entity->uuid()],
83       ],
84       'vid' => [
85         [
86           'target_id' => 'camelids',
87           'target_type' => 'taxonomy_vocabulary',
88           'target_uuid' => Vocabulary::load('camelids')->uuid(),
89         ],
90       ],
91       'name' => [
92         ['value' => 'Llama'],
93       ],
94       'description' => [
95         [
96           'value' => NULL,
97           'format' => NULL,
98         ],
99       ],
100       'parent' => [],
101       'weight' => [
102         ['value' => 0],
103       ],
104       'langcode' => [
105         [
106           'value' => 'en',
107         ],
108       ],
109       'changed' => [
110         [
111           'value' => $this->entity->getChangedTime(),
112         ],
113       ],
114       'default_langcode' => [
115         [
116           'value' => TRUE,
117         ],
118       ],
119     ];
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function getNormalizedPostEntity() {
126     return [
127       'vid' => [
128         [
129           'target_id' => 'camelids',
130         ],
131       ],
132       'name' => [
133         [
134           'value' => 'Dramallama',
135         ],
136       ],
137     ];
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   protected function getExpectedUnauthorizedAccessMessage($method) {
144     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
145       return parent::getExpectedUnauthorizedAccessMessage($method);
146     }
147
148     switch ($method) {
149       case 'GET':
150         return "The 'access content' permission is required.";
151       case 'POST':
152         return "The 'administer taxonomy' permission is required.";
153       case 'PATCH':
154         return "The following permissions are required: 'edit terms in camelids' OR 'administer taxonomy'.";
155       case 'DELETE':
156         return "The following permissions are required: 'delete terms in camelids' OR 'administer taxonomy'.";
157       default:
158         return parent::getExpectedUnauthorizedAccessMessage($method);
159     }
160   }
161
162 }