diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php new file mode 100644 index 00000000000..6c351596852 --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php @@ -0,0 +1,29 @@ +applyHalFieldNormalization($default_normalization); + $feed = Feed::load($this->entity->getFeedId()); + + return $normalization + [ + '_embedded' => [ + $this->baseUrl . '/rest/relation/aggregator_item/aggregator_item/fid' => [ + [ + '_links' => [ + 'self' => [ + 'href' => $this->baseUrl . '/aggregator/sources/1?_format=hal_json', + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed', + ], + ], + 'uuid' => [ + [ + 'value' => $feed->uuid(), + ], + ], + ], + ], + ], + '_links' => [ + 'self' => [ + 'href' => '', + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/aggregator_item/aggregator_item', + ], + $this->baseUrl . '/rest/relation/aggregator_item/aggregator_item/fid' => [ + [ + 'href' => $this->baseUrl . '/aggregator/sources/' . $feed->id() . '?_format=hal_json', + ], + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return parent::getNormalizedPostEntity() + [ + '_links' => [ + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/aggregator_item/aggregator_item', + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + return [ + 'url.site', + 'user.permissions', + ]; + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 4a8c304fac0..53796c8aeaf 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -695,8 +695,11 @@ abstract class EntityResourceTestBase extends ResourceTestBase { // DX: 422 when invalid entity: UUID field too long. - $response = $this->request('POST', $url, $request_options); - $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response); + // @todo Fix this in https://www.drupal.org/node/2149851. + if ($this->entity->getEntityType()->hasKey('uuid')) { + $response = $this->request('POST', $url, $request_options); + $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response); + } $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_3; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php new file mode 100644 index 00000000000..dac1b0a6d01 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php @@ -0,0 +1,24 @@ +grantPermissionsToTestedRole(['access news feeds']); + break; + + case 'POST': + case 'PATCH': + case 'DELETE': + $this->grantPermissionsToTestedRole(['administer news feeds']); + break; + } + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + // Create a "Camelids" feed. + $feed = Feed::create([ + 'title' => 'Camelids', + 'url' => 'https://groups.drupal.org/not_used/167169', + 'refresh' => 900, + 'checked' => 1389919932, + 'description' => 'Drupal Core Group feed', + ]); + $feed->save(); + + // Create a "Llama" item. + $item = Item::create(); + $item->setTitle('Llama') + ->setFeedId($feed->id()) + ->setLink('https://www.drupal.org/') + ->setPostedTime(123456789) + ->save(); + + return $item; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $feed = Feed::load($this->entity->getFeedId()); + + return [ + 'iid' => [ + [ + 'value' => 1, + ], + ], + 'langcode' => [ + [ + 'value' => 'en', + ], + ], + 'fid' => [ + [ + 'target_id' => 1, + 'target_type' => 'aggregator_feed', + 'target_uuid' => $feed->uuid(), + 'url' => base_path() . 'aggregator/sources/1', + ], + ], + 'title' => [ + [ + 'value' => 'Llama', + ], + ], + 'link' => [ + [ + 'value' => 'https://www.drupal.org/', + ], + ], + 'author' => [], + 'description' => [], + 'timestamp' => [ + [ + 'value' => 123456789, + ], + ], + 'guid' => [], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return [ + 'fid' => [ + [ + 'target_id' => 1, + ], + ], + 'title' => [ + [ + 'value' => 'Llama', + ], + ], + 'link' => [ + [ + 'value' => 'https://www.drupal.org/', + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // @see ::createEntity() + return ['user.permissions']; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) { + return parent::getExpectedUnauthorizedAccessMessage($method); + } + + switch ($method) { + case 'GET': + return "The 'access news feeds' permission is required."; + + case 'POST': + case 'PATCH': + case 'DELETE': + return "The 'administer news feeds' permission is required."; + + default: + return parent::getExpectedUnauthorizedAccessMessage($method); + } + } + +}