diff --git a/core/modules/jsonapi/tests/src/Functional/CommentTest.php b/core/modules/jsonapi/tests/src/Functional/CommentTest.php index 28660453180..4faf84fd3ff 100644 --- a/core/modules/jsonapi/tests/src/Functional/CommentTest.php +++ b/core/modules/jsonapi/tests/src/Functional/CommentTest.php @@ -383,7 +383,7 @@ class CommentTest extends ResourceTestBase { /** * {@inheritdoc} */ - public function testPatchIndividual(): void { + protected function doTestPatchIndividual(): void { // Ensure ::getModifiedEntityForPatchTesting() can pick an alternative value // for the 'entity_id' field. EntityTest::create([ @@ -391,7 +391,7 @@ class CommentTest extends ResourceTestBase { 'type' => 'bar', ])->save(); - parent::testPatchIndividual(); + parent::doTestPatchIndividual(); } } diff --git a/core/modules/jsonapi/tests/src/Functional/FileTest.php b/core/modules/jsonapi/tests/src/Functional/FileTest.php index 899bcbdc78f..22635e56d7e 100644 --- a/core/modules/jsonapi/tests/src/Functional/FileTest.php +++ b/core/modules/jsonapi/tests/src/Functional/FileTest.php @@ -201,11 +201,16 @@ class FileTest extends ResourceTestBase { } /** - * {@inheritdoc} + * Tests POST/PATCH/DELETE for an individual resource. */ - public function testPostIndividual(): void { + public function testIndividual(): void { // @todo https://www.drupal.org/node/1927648 - $this->markTestSkipped(); + // Add doTestPostIndividual(). + $this->doTestPatchIndividual(); + $this->entity = $this->resaveEntity($this->entity, $this->account); + $this->revokePermissions(); + $this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE); + $this->doTestDeleteIndividual(); } /** diff --git a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php index 139531bb80a..b782e6d09c3 100644 --- a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php +++ b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php @@ -113,7 +113,7 @@ class FileUploadTest extends ResourceTestBase { * * @var array */ - const SKIP_METHODS = ['testGetIndividual', 'testPostIndividual', 'testPatchIndividual', 'testDeleteIndividual', 'testCollection', 'testRelationships']; + const SKIP_METHODS = ['testGetIndividual', 'testIndividual', 'testCollection', 'testRelationships']; /** * {@inheritdoc} diff --git a/core/modules/jsonapi/tests/src/Functional/MediaTest.php b/core/modules/jsonapi/tests/src/Functional/MediaTest.php index e733e773bc4..f74bf01c76f 100644 --- a/core/modules/jsonapi/tests/src/Functional/MediaTest.php +++ b/core/modules/jsonapi/tests/src/Functional/MediaTest.php @@ -360,10 +360,10 @@ class MediaTest extends ResourceTestBase { /** * {@inheritdoc} */ - public function testPostIndividual(): void { + protected function doTestPostIndividual(): void { // @todo Mimic \Drupal\Tests\rest\Functional\EntityResource\Media\MediaResourceTestBase::testPost() // @todo Later, use https://www.drupal.org/project/drupal/issues/2958554 to upload files rather than the REST module. - parent::testPostIndividual(); + parent::doTestPostIndividual(); } /** diff --git a/core/modules/jsonapi/tests/src/Functional/MessageTest.php b/core/modules/jsonapi/tests/src/Functional/MessageTest.php index ed23715b073..629aa8bfd1c 100644 --- a/core/modules/jsonapi/tests/src/Functional/MessageTest.php +++ b/core/modules/jsonapi/tests/src/Functional/MessageTest.php @@ -132,7 +132,7 @@ class MessageTest extends ResourceTestBase { /** * {@inheritdoc} */ - public function testPatchIndividual(): void { + protected function doTestPatchIndividual(): void { // Contact Message entities are not stored, so they cannot be modified. $this->expectException(RouteNotFoundException::class); $this->expectExceptionMessage('Route "jsonapi.contact_message--camelids.individual" does not exist.'); @@ -143,7 +143,7 @@ class MessageTest extends ResourceTestBase { /** * {@inheritdoc} */ - public function testDeleteIndividual(): void { + protected function doTestDeleteIndividual(): void { // Contact Message entities are not stored, so they cannot be deleted. $this->expectException(RouteNotFoundException::class); $this->expectExceptionMessage('Route "jsonapi.contact_message--camelids.individual" does not exist.'); diff --git a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php index 5960ca8a895..1b84fe615e9 100644 --- a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php @@ -229,21 +229,7 @@ abstract class ResourceTestBase extends BrowserTestBase { $this->config('system.logging')->set('error_level', ERROR_REPORTING_HIDE)->save(); - // Ensure the anonymous user role has no permissions at all. - $user_role = Role::load(RoleInterface::ANONYMOUS_ID); - foreach ($user_role->getPermissions() as $permission) { - $user_role->revokePermission($permission); - } - $user_role->save(); - assert([] === $user_role->getPermissions(), 'The anonymous user role has no permissions at all.'); - - // Ensure the authenticated user role has no permissions at all. - $user_role = Role::load(RoleInterface::AUTHENTICATED_ID); - foreach ($user_role->getPermissions() as $permission) { - $user_role->revokePermission($permission); - } - $user_role->save(); - assert([] === $user_role->getPermissions(), 'The authenticated user role has no permissions at all.'); + $this->revokePermissions(); // Create an account, which tests will use. Also ensure the @current_user // service this account, to ensure certain access check logic in tests works @@ -338,12 +324,15 @@ abstract class ResourceTestBase extends BrowserTestBase { \Drupal::service('router.builder')->rebuildIfNeeded(); + return $this->resaveEntity($entity, $account); + } + + protected function resaveEntity(EntityInterface $entity, AccountInterface $account): EntityInterface { // Reload entity so that it has the new field. $reloaded_entity = $this->entityLoadUnchanged($entity->id()); // Some entity types are not stored, hence they cannot be reloaded. if ($reloaded_entity !== NULL) { $entity = $reloaded_entity; - // Set a default value on the fields. $entity->set('field_rest_test', ['value' => 'All the faith he had had had had no effect on the outcome of his life.']); $entity->set('field_jsonapi_test_entity_ref', ['user' => $account->id()]); @@ -1973,10 +1962,25 @@ abstract class ResourceTestBase extends BrowserTestBase { return $related_response; } + /** + * Tests POST/PATCH/DELETE for an individual resource. + */ + public function testIndividual(): void { + $this->doTestPostIndividual(); + $this->entity = $this->resaveEntity($this->entity, $this->account); + $this->revokePermissions(); + $this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE); + $this->doTestPatchIndividual(); + $this->entity = $this->resaveEntity($this->entity, $this->account); + $this->revokePermissions(); + $this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE); + $this->doTestDeleteIndividual(); + } + /** * Tests POSTing an individual resource, plus edge cases to ensure good DX. */ - public function testPostIndividual(): void { + protected function doTestPostIndividual(): void { // @todo Remove this in https://www.drupal.org/node/2300677. if ($this->entity instanceof ConfigEntityInterface) { $this->markTestSkipped('POSTing config entities is not yet supported.'); @@ -2191,7 +2195,7 @@ abstract class ResourceTestBase extends BrowserTestBase { /** * Tests PATCHing an individual resource, plus edge cases to ensure good DX. */ - public function testPatchIndividual(): void { + protected function doTestPatchIndividual(): void { // @todo Remove this in https://www.drupal.org/node/2300677. if ($this->entity instanceof ConfigEntityInterface) { $this->markTestSkipped('PATCHing config entities is not yet supported.'); @@ -2519,7 +2523,7 @@ abstract class ResourceTestBase extends BrowserTestBase { /** * Tests DELETEing an individual resource, plus edge cases to ensure good DX. */ - public function testDeleteIndividual(): void { + protected function doTestDeleteIndividual(): void { // @todo Remove this in https://www.drupal.org/node/2300677. if ($this->entity instanceof ConfigEntityInterface) { $this->markTestSkipped('DELETEing config entities is not yet supported.'); @@ -3574,4 +3578,23 @@ abstract class ResourceTestBase extends BrowserTestBase { return $expected_document; } + /** + * Ensure the anonymous and authenticated roles have no permissions at all. + */ + protected function revokePermissions(): void { + $user_role = Role::load(RoleInterface::ANONYMOUS_ID); + foreach ($user_role->getPermissions() as $permission) { + $user_role->revokePermission($permission); + } + $user_role->save(); + assert([] === $user_role->getPermissions(), 'The anonymous user role has no permissions at all.'); + + $user_role = Role::load(RoleInterface::AUTHENTICATED_ID); + foreach ($user_role->getPermissions() as $permission) { + $user_role->revokePermission($permission); + } + $user_role->save(); + assert([] === $user_role->getPermissions(), 'The authenticated user role has no permissions at all.'); + } + } diff --git a/core/modules/jsonapi/tests/src/Functional/UserTest.php b/core/modules/jsonapi/tests/src/Functional/UserTest.php index 7231798f6f9..c2d177b56a8 100644 --- a/core/modules/jsonapi/tests/src/Functional/UserTest.php +++ b/core/modules/jsonapi/tests/src/Functional/UserTest.php @@ -128,10 +128,10 @@ class UserTest extends ResourceTestBase { /** * {@inheritdoc} */ - public function testDeleteIndividual(): void { + protected function doTestDeleteIndividual(): void { $this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save(TRUE); - parent::testDeleteIndividual(); + parent::doTestDeleteIndividual(); } /** @@ -195,6 +195,21 @@ class UserTest extends ResourceTestBase { ]; } + /** + * {@inheritdoc} + */ + protected function getPatchDocument() { + return [ + 'data' => [ + 'id' => $this->entity->uuid(), + 'type' => 'user--user', + 'attributes' => [ + 'name' => 'Drama llama 2', + ], + ], + ]; + } + /** * {@inheritdoc} */