diff --git a/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php new file mode 100644 index 0000000..be1d67b --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php @@ -0,0 +1,175 @@ +entity, 'public://drupal.txt', FILE_EXISTS_ERROR); + $this->entity->setFileUri('public://drupal.txt'); + $this->entity->save(); + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $default_normalization = parent::getExpectedNormalizedEntity(); + $normalization = $this->applyHalFieldNormalization($default_normalization); + + $author = User::load($this->entity->getOwnerId()); + $url = file_create_url($this->entity->getFileUri()); + $normalization['uri'][0]['value'] = $url; + $uid = $author->id(); + + return $normalization + [ + '_embedded' => [ + $this->baseUrl . '/rest/relation/file/file/uid' => [ + [ + '_links' => [ + 'self' => [ + 'href' => $this->baseUrl . "/user/$uid?_format=hal_json", + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/user/user', + ], + ], + 'uuid' => [ + [ + 'value' => $author->uuid(), + ], + ], + ], + ], + ], + '_links' => [ + 'self' => [ + 'href' => $url, + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/file/file', + ], + $this->baseUrl . '/rest/relation/file/file/uid' => [ + [ + 'href' => $this->baseUrl . "/user/$uid?_format=hal_json", + ], + ], + ], + 'changed' => [ + [ + 'value' => $this->entity->getChangedTime(), + ], + ], + 'created' => [ + [ + 'value' => (int) $this->entity->getCreatedTime(), + ], + ], + 'fid' => [ + [ + 'value' => 1, + ], + ], + 'filemime' => [ + [ + 'value' => 'text/plain', + ], + ], + 'filename' => [ + [ + 'value' => 'drupal.txt', + ], + ], + 'filesize' => [ + [ + 'value' => (int) $this->entity->getSize(), + ], + ], + 'langcode' => [ + [ + 'value' => 'en', + ], + ], + 'status' => [ + [ + 'value' => TRUE, + ], + ], + 'uuid' => [ + [ + 'value' => $this->entity->uuid(), + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return parent::getNormalizedPostEntity() + [ + '_links' => [ + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/file/file', + ], + ], + 'uri' => [ + [ + 'value' => file_create_url($this->entity->getFileUri()), + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function testPost() { + $this->markTestSkipped(); + } + + /** + * {@inheritdoc} + */ + public function testPatch() { + $this->markTestSkipped(); + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + return ['url.site']; + } + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonBasicAuthTest.php new file mode 100644 index 0000000..3b1e3fe --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonBasicAuthTest.php @@ -0,0 +1,24 @@ +assertFalse($response->hasHeader('X-Drupal-Cache')); } $cache_tags_header_value = $response->getHeader('X-Drupal-Cache-Tags')[0]; - $this->assertEquals($this->getExpectedCacheTags(), empty($cache_tags_header_value) ? [] : explode(' ', $cache_tags_header_value)); + $this->assertEquals($this->getExpectedCacheTags(), empty($cache_tags_header_value) ? [] : explode(' ', $cache_tags_header_value), '', .0, 2, TRUE); $cache_contexts_header_value = $response->getHeader('X-Drupal-Cache-Contexts')[0]; $this->assertEquals($this->getExpectedCacheContexts(), empty($cache_contexts_header_value) ? [] : explode(' ', $cache_contexts_header_value)); // Sort the serialization data first so we can do an identical comparison diff --git a/core/modules/rest/tests/src/Functional/EntityResource/File/FileJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/File/FileJsonAnonTest.php new file mode 100644 index 0000000..b3adf6e --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/File/FileJsonAnonTest.php @@ -0,0 +1,24 @@ +entity->setOwnerId($account->id()); + $this->entity->setOwner($account); + $this->entity->save(); + } + + /** + * {@inheritdoc} + */ + protected function grantPermissionsToTestedRole(array $permissions) { + if ($permissions === ['restful delete entity:file']) { + $this->correctFileOwner(); + } + parent::grantPermissionsToTestedRole($permissions); + } + + /** + * {@inheritdoc} + */ + protected function setUpAuthorization($method) { + $this->correctFileOwner(); + $this->grantPermissionsToTestedRole(['administer files']); + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + $author = User::load(3); + if (!$author) { + $author = User::create([ + 'name' => 'Unknown', + 'status' => 1, + ]); + $author->save(); + } + $uid = $author->id(); + + $file = File::create([ + 'uid' => $uid, + 'filename' => 'drupal.txt', + 'uri' => 'private://drupal.txt', + 'filemime' => 'text/plain', + 'status' => FILE_STATUS_PERMANENT, + ]); + file_put_contents($file->getFileUri(), 'Drupal'); + $file->save(); + + return $file; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $author = User::load($this->entity->getOwnerId()); + return [ + 'changed' => [ + [ + 'value' => $this->entity->getChangedTime(), + ], + ], + 'created' => [ + [ + 'value' => (int) $this->entity->getCreatedTime(), + ], + ], + 'fid' => [ + [ + 'value' => 1, + ], + ], + 'filemime' => [ + [ + 'value' => 'text/plain', + ], + ], + 'filename' => [ + [ + 'value' => 'drupal.txt', + ], + ], + 'filesize' => [ + [ + 'value' => (int) $this->entity->getSize(), + ], + ], + 'langcode' => [ + [ + 'value' => 'en', + ], + ], + 'status' => [ + [ + 'value' => TRUE, + ], + ], + 'uid' => [ + [ + 'target_id' => (int) $author->id(), + 'target_type' => 'user', + 'target_uuid' => $author->uuid(), + 'url' => base_path() . 'user/' . $author->id(), + ], + ], + 'uri' => [ + [ + 'value' => 'private://drupal.txt', + ], + ], + 'uuid' => [ + [ + 'value' => $this->entity->uuid(), + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + $uid = static::$auth ? 2 : 0; + return [ + 'uid' => [ + [ + 'target_id' => $uid, + ], + ], + 'filename' => [ + [ + 'value' => 'drupal.txt', + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + return []; + } + + /** + * {@inheritdoc} + */ + public function testPost() { + $this->markTestSkipped(); + } + + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) { + return parent::getExpectedUnauthorizedAccessMessage($method); + } + if ($method === 'PATCH') { + return 'You are not authorized to update this file entity.'; + } + return parent::getExpectedUnauthorizedAccessMessage($method); + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheTags() { + return array_diff(parent::getExpectedCacheTags(), ['config:user.role.anonymous']); + } +}