.../EntityResource/Block/BlockHalJsonAnonTest.php | 7 -- .../Block/BlockHalJsonBasicAuthTest.php | 7 -- .../ConfigTest/ConfigTestHalJsonAnonTest.php | 7 -- .../ConfigTest/ConfigTestHalJsonBasicAuthTest.php | 7 -- .../EntityTest/EntityTestHalJsonAnonTest.php | 7 -- .../EntityTest/EntityTestHalJsonBasicAuthTest.php | 7 -- .../EntityResource/Node/NodeHalJsonAnonTest.php | 7 -- .../Node/NodeHalJsonBasicAuthTest.php | 7 -- .../EntityResource/Role/RoleHalJsonAnonTest.php | 7 -- .../Role/RoleHalJsonBasicAuthTest.php | 22 ---- .../EntityResource/Term/TermHalJsonAnonTest.php | 7 -- .../Term/TermHalJsonBasicAuthTest.php | 26 +---- .../Vocabulary/VocabularyHalJsonAnonTest.php | 7 -- .../Vocabulary/VocabularyHalJsonBasicAuthTest.php | 7 -- .../tests/src/Functional/AnonResourceTestTrait.php | 12 --- .../src/Functional/BasicAuthResourceTestTrait.php | 2 +- .../EntityResource/Block/BlockJsonAnonTest.php | 7 -- .../Block/BlockJsonBasicAuthTest.php | 7 -- .../EntityResource/Block/BlockResourceTestBase.php | 7 ++ .../ConfigTest/ConfigTestJsonAnonTest.php | 7 -- .../ConfigTest/ConfigTestJsonBasicAuthTest.php | 7 -- .../ConfigTest/ConfigTestResourceTestBase.php | 7 ++ .../EntityResource/EntityResourceTestBase.php | 28 +---- .../EntityTest/EntityTestJsonAnonTest.php | 7 -- .../EntityTest/EntityTestJsonBasicAuthTest.php | 7 -- .../EntityTest/EntityTestResourceTestBase.php | 7 ++ .../EntityResource/Node/NodeJsonAnonTest.php | 7 -- .../EntityResource/Node/NodeJsonBasicAuthTest.php | 7 -- .../EntityResource/Node/NodeResourceTestBase.php | 7 ++ .../EntityResource/Role/RoleJsonAnonTest.php | 7 -- .../EntityResource/Role/RoleJsonBasicAuthTest.php | 22 ---- .../EntityResource/Role/RoleResourceTestBase.php | 7 ++ .../EntityResource/Term/TermJsonAnonTest.php | 7 -- .../EntityResource/Term/TermJsonBasicAuthTest.php | 7 -- .../EntityResource/Term/TermResourceTestBase.php | 7 ++ .../Vocabulary/VocabularyJsonAnonTest.php | 7 -- .../Vocabulary/VocabularyJsonBasicAuthTest.php | 7 -- .../Vocabulary/VocabularyResourceTestBase.php | 7 ++ .../rest/tests/src/Functional/ResourceTestBase.php | 114 +++++++++++++++++++-- 39 files changed, 161 insertions(+), 289 deletions(-) diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonAnonTest.php index f54dce2..d0758f3 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonAnonTest.php @@ -32,11 +32,4 @@ class BlockHalJsonAnonTest extends BlockResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer blocks']); - } - } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php index 6f778b9..d4f8a12 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php @@ -38,13 +38,6 @@ class BlockHalJsonBasicAuthTest extends BlockResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer blocks']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonAnonTest.php index cf687e6..45cedb2 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonAnonTest.php @@ -32,11 +32,4 @@ class ConfigTestHalJsonAnonTest extends ConfigTestResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['view config_test']); - } - } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonBasicAuthTest.php index 9c9790d..c24397a 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/ConfigTest/ConfigTestHalJsonBasicAuthTest.php @@ -38,13 +38,6 @@ class ConfigTestHalJsonBasicAuthTest extends ConfigTestResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['view config_test']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php index 8b93b9b..935a899 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php @@ -35,13 +35,6 @@ class EntityTestHalJsonAnonTest extends EntityTestResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['view test entity']); - } - /** * {@inheritdoc} */ diff --git a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonBasicAuthTest.php index bb9f420..063e1e3 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonBasicAuthTest.php @@ -22,13 +22,6 @@ class EntityTestHalJsonBasicAuthTest extends EntityTestHalJsonAnonTest { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['view test entity']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php index 94ce785..c926da7 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php @@ -36,13 +36,6 @@ class NodeHalJsonAnonTest extends NodeResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['access content']); - } - /** * {@inheritdoc} */ diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonBasicAuthTest.php index b48bb66..1132ee0 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonBasicAuthTest.php @@ -22,13 +22,6 @@ class NodeHalJsonBasicAuthTest extends NodeHalJsonAnonTest { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['access content']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonAnonTest.php index 1ecf214..cad4104 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonAnonTest.php @@ -32,11 +32,4 @@ class RoleHalJsonAnonTest extends RoleResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - public function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer permissions']); - } - } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php index 269e8f1..9b9932f 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php @@ -38,28 +38,6 @@ class RoleHalJsonBasicAuthTest extends RoleResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer permissions']); - } - - /** - * {@inheritdoc} - */ - protected function getExpectedNormalizedEntity() { - $default_normalization = parent::getExpectedNormalizedEntity(); - return [ - // Edge case: the weight is not 2 like in the default normalization, but 3 - // because setUpAuthentication() creates a new user role (which gets - // weight 2), and then the entity that is created for testing ends up with - // weight 3. - // This only affects user_role entity tests for authenticated users. - 'weight' => 3, - ] + $default_normalization; - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php index c334386..b2b2bee 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php @@ -34,13 +34,6 @@ class TermHalJsonAnonTest extends TermResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['access content']); - } - /** * {@inheritdoc} */ diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonBasicAuthTest.php index 9ba8404..59b754a 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonBasicAuthTest.php @@ -3,7 +3,6 @@ namespace Drupal\Tests\hal\Functional\EntityResource\Term; use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait; -use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait; /** @@ -11,41 +10,18 @@ */ class TermHalJsonBasicAuthTest extends TermHalJsonAnonTest { - use HalEntityNormalizationTrait; use BasicAuthResourceTestTrait; /** * {@inheritdoc} */ - public static $modules = ['hal', 'basic_auth']; - - /** - * {@inheritdoc} - */ - protected static $format = 'hal_json'; - - /** - * {@inheritdoc} - */ - protected static $mimeType = 'application/hal+json'; - - /** - * {@inheritdoc} - */ - protected static $expectedErrorMimeType = 'application/json'; + public static $modules = ['basic_auth']; /** * {@inheritdoc} */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['access content']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonAnonTest.php index 8fb9946..910da05 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonAnonTest.php @@ -32,13 +32,6 @@ class VocabularyHalJsonAnonTest extends VocabularyResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - public function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer taxonomy']); - } - // Disable the GET test coverage due to bug in taxonomy module. // @todo Fix in https://www.drupal.org/node/2805281: remove this override. public function testGet() {} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonBasicAuthTest.php index 12ef15c..03c9232 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonBasicAuthTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Vocabulary/VocabularyHalJsonBasicAuthTest.php @@ -38,13 +38,6 @@ class VocabularyHalJsonBasicAuthTest extends VocabularyResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer taxonomy']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use HalJsonBasicAuthWorkaroundFor2805281Trait { HalJsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/AnonResourceTestTrait.php b/core/modules/rest/tests/src/Functional/AnonResourceTestTrait.php index b8b04b1..cc089de 100644 --- a/core/modules/rest/tests/src/Functional/AnonResourceTestTrait.php +++ b/core/modules/rest/tests/src/Functional/AnonResourceTestTrait.php @@ -2,22 +2,10 @@ namespace Drupal\Tests\rest\Functional; -use Drupal\user\Entity\Role; -use Drupal\user\RoleInterface; use Psr\Http\Message\ResponseInterface; trait AnonResourceTestTrait { - /** - * Grants permissions to the anonymous role. - * - * @param string[] $permissions - * Permissions to grant. - */ - protected function grantPermissionsToAnonymousRole(array $permissions) { - $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), $permissions); - } - /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php b/core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php index bb1c997..da62ff8 100644 --- a/core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php +++ b/core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php @@ -24,7 +24,7 @@ protected function getAuthenticationRequestOptions() { * {@inheritdoc} */ protected function verifyResponseWhenMissingAuthentication(ResponseInterface $response) { - $this->assertErrorResponse(401, 'No authentication credentials provided.', $response); + $this->assertResourceErrorResponse(401, 'No authentication credentials provided.', $response); } } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonAnonTest.php index ebfe7ac..9c764bd 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonAnonTest.php @@ -26,11 +26,4 @@ class BlockJsonAnonTest extends BlockResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer blocks']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonBasicAuthTest.php index c5b803e..a29c38d 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockJsonBasicAuthTest.php @@ -37,13 +37,6 @@ class BlockJsonBasicAuthTest extends BlockResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer blocks']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php index 9430a35..f77a522 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php @@ -19,6 +19,13 @@ */ protected static $entityType = 'block'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['administer blocks']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonAnonTest.php index 139e17f..db79e6c 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonAnonTest.php @@ -26,11 +26,4 @@ class ConfigTestJsonAnonTest extends ConfigTestResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['view config_test']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonBasicAuthTest.php index f7365a6..bd73278 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestJsonBasicAuthTest.php @@ -37,13 +37,6 @@ class ConfigTestJsonBasicAuthTest extends ConfigTestResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['view config_test']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php index 2e93715..36315f3 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php @@ -16,6 +16,13 @@ */ protected static $entityType = 'config_test'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['view config_test']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 9ada181..80a21d2 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -86,6 +86,8 @@ public function setUp() { public function testGet() { // @todo test what happens before provisioning. Edge case: node. + $this->setUpAuthorization(); + // Provision REST resource. $this->provisionEntityResource(); @@ -204,34 +206,10 @@ public function testGet() { catch (ClientException $e) { $path = str_replace('987654321', '{' . static::$entityType . '}', $non_existing_entity_url->setAbsolute()->setOptions(['base_url' => '', 'query' => []])->toString()); $message = 'The "' . static::$entityType . '" parameter was not converted for the path "' . $path . '" (route name: "rest.entity.' . static::$entityType . '.GET.' . static::$format . '")'; - $this->assertErrorResponse(404, $message, $e->getResponse()); + $this->assertResourceErrorResponse(404, $message, $e->getResponse()); } } - /** - * Asserts that an error response has the given status code and message. - * - * (Also asserts that the expected error MIME type is present, but this is - * defined globally for the test via static::$expectedErrorMimeType, because - * all error responses should use the same MIME type.) - * - * @param int $expected_status_code - * The expected response status. - * @param string $expected_message - * The expected error message. - * @param \Psr\Http\Message\ResponseInterface $response - * The error response to assert. - */ - protected function assertErrorResponse($expected_status_code, $expected_message, ResponseInterface $response) { - $this->assertSame($expected_status_code, $response->getStatusCode()); - $this->assertSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type')); - // @todo Either add this to \Drupal\serialization\Encoder\JsonEncoder, or - // figure out how to let tests specify encoder options, and figure out - // whether they should apply to just error responses or to everything. - $encode_options = ['json_encode_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT]; - $this->assertSame($this->serializer->encode(['message' => $expected_message], static::$format, $encode_options), (string)$response->getBody()); - } - // @todo try the following request bodies: // 1. empty (only makes sense for DELETE and GET), assert 400 // 2. not parseable, assert 400 diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonAnonTest.php index 97b61bb..a7e4420 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonAnonTest.php @@ -26,11 +26,4 @@ class EntityTestJsonAnonTest extends EntityTestResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['view test entity']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonBasicAuthTest.php index 3c83852..1b45e25 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestJsonBasicAuthTest.php @@ -37,13 +37,6 @@ class EntityTestJsonBasicAuthTest extends EntityTestResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['view test entity']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php index 69a2a7b..f4fc77f 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php @@ -18,6 +18,13 @@ */ protected static $entityType = 'entity_test'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['view test entity']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonAnonTest.php index 596dd1c..24d47c4 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonAnonTest.php @@ -26,11 +26,4 @@ class NodeJsonAnonTest extends NodeResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['access content']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonBasicAuthTest.php index d293972..0200d49 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonBasicAuthTest.php @@ -38,13 +38,6 @@ class NodeJsonBasicAuthTest extends NodeResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['access content']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php index 1b4538b..e77d1ff 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php @@ -19,6 +19,13 @@ */ protected static $entityType = 'node'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['access content']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonAnonTest.php index 103a208..3ec96eb 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonAnonTest.php @@ -26,11 +26,4 @@ class RoleJsonAnonTest extends RoleResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - public function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer permissions']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonBasicAuthTest.php index 05a0150..5cc463c 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleJsonBasicAuthTest.php @@ -37,28 +37,6 @@ class RoleJsonBasicAuthTest extends RoleResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer permissions']); - } - - /** - * {@inheritdoc} - */ - protected function getExpectedNormalizedEntity() { - $default_normalization = parent::getExpectedNormalizedEntity(); - return [ - // Edge case: the weight is not 2 like in the default normalization, but 3 - // because setUpAuthentication() creates a new user role (which gets - // weight 2), and then the entity that is created for testing ends up with - // weight 3. - // This only affects user_role entity tests for authenticated users. - 'weight' => 3, - ] + $default_normalization; - } - /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php index 4255e2d..091a84a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php @@ -18,6 +18,13 @@ */ protected static $entityType = 'user_role'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['administer permissions']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonAnonTest.php index 60fa865..6e01c03 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonAnonTest.php @@ -26,11 +26,4 @@ class TermJsonAnonTest extends TermResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['access content']); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonBasicAuthTest.php index 84c5cf2..6ee100a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermJsonBasicAuthTest.php @@ -37,13 +37,6 @@ class TermJsonBasicAuthTest extends TermResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['access content']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php index f1c3bbc..cddb4c8 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php @@ -18,6 +18,13 @@ */ protected static $entityType = 'taxonomy_term'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['access content']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonAnonTest.php index 7256e81..ccda474 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonAnonTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonAnonTest.php @@ -26,13 +26,6 @@ class VocabularyJsonAnonTest extends VocabularyResourceTestBase { */ protected static $expectedErrorMimeType = 'application/json'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->grantPermissionsToAnonymousRole(['administer taxonomy']); - } - // Disable the GET test coverage due to bug in taxonomy module. // @todo Fix in https://www.drupal.org/node/2805281: remove this override. public function testGet() {} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonBasicAuthTest.php index 9308d02..8549963 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonBasicAuthTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyJsonBasicAuthTest.php @@ -38,13 +38,6 @@ class VocabularyJsonBasicAuthTest extends VocabularyResourceTestBase { */ protected static $auth = 'basic_auth'; - /** - * {@inheritdoc} - */ - protected function setUpAuthentication() { - $this->account = $this->createUser(['administer taxonomy']); - } - // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage. use JsonBasicAuthWorkaroundFor2805281Trait { JsonBasicAuthWorkaroundFor2805281Trait::verifyResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php index 8ec5fec..d2522ba 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php @@ -20,6 +20,13 @@ */ protected static $entityType = 'taxonomy_vocabulary'; + /** + * {@inheritdoc} + */ + protected function setUpAuthorization() { + $this->grantPermissionsToTestedRole(['administer taxonomy']); + } + /** * {@inheritdoc} */ diff --git a/core/modules/rest/tests/src/Functional/ResourceTestBase.php b/core/modules/rest/tests/src/Functional/ResourceTestBase.php index 22132c4..39f7da3 100644 --- a/core/modules/rest/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/ResourceTestBase.php @@ -93,17 +93,26 @@ public function setUp() { parent::setUp(); - // Ensure the anonymous user has no permissions at all. + // 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 has no permissions at all.'); + assert('[] === $user_role->getPermissions()', 'The anonymous user role has no permissions at all.'); - // Then allow concrete tests to set up authentication: grant permissions to - // the anonymous user, or create a user with the necessary permissions. - $this->setUpAuthentication(); + if (static::$auth !== FALSE) { + // 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.'); + + // Create an account. + $this->account = $this->createUser(); + } $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config'); @@ -136,7 +145,7 @@ protected function provisionResource($resource_type, $formats = [], $authenticat } /** - * Sets up the necessary authentication. + * Sets up the necessary authorization. * * In case of a test verifying publicly accessible REST resources: grant * permissions to the anonymous user role. @@ -145,8 +154,11 @@ protected function provisionResource($resource_type, $formats = [], $authenticat * provider: create a user with a particular set of permissions. * * @return void + * + * @see ::grantPermissionsToAnonymousRole() + * @see ::grantPermissionsToAuthenticatedRole() */ - abstract protected function setUpAuthentication(); + abstract protected function setUpAuthorization(); /** * Verifies the error response in case of missing authentication. @@ -167,4 +179,92 @@ protected function getAuthenticationRequestOptions() { return []; } + /** + * Grants permissions to the anonymous role. + * + * @param string[] $permissions + * Permissions to grant. + */ + protected function grantPermissionsToAnonymousRole(array $permissions) { + $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), $permissions); + } + + /** + * Grants permissions to the authenticated role. + * + * @param string[] $permissions + * Permissions to grant. + */ + protected function grantPermissionsToAuthenticatedRole(array $permissions) { + $this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), $permissions); + } + + /** + * Grants permissions to the tested role: anonymous or authenticated. + * + * @param string[] $permissions + * Permissions to grant. + * + * @see ::grantPermissionsToAuthenticatedRole() + * @see ::grantPermissionsToAnonymousRole() + */ + protected function grantPermissionsToTestedRole(array $permissions) { + if (static::$auth) { + $this->grantPermissionsToAuthenticatedRole($permissions); + } + else { + $this->grantPermissionsToAnonymousRole($permissions); + } + } + + /** + * Asserts that a resource response has the given status code and body. + * + * (Also asserts that the expected error MIME type is present, but this is + * defined globally for the test via static::$expectedErrorMimeType, because + * all error responses should use the same MIME type.) + * + * @param int $expected_status_code + * The expected response status. + * @param string|false $expected_body + * The expected response body. FALSE in case this should not be asserted. + * @param \Psr\Http\Message\ResponseInterface $response + * The response to assert. + */ + protected function assertResourceResponse($expected_status_code, $expected_body, ResponseInterface $response) { + $this->assertSame($expected_status_code, $response->getStatusCode()); + if ($expected_status_code < 400) { + $this->assertSame([static::$mimeType], $response->getHeader('Content-Type')); + } + else { + $this->assertSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type')); + } + if ($expected_body !== FALSE) { + $this->assertSame($expected_body, (string) $response->getBody()); + } + } + + /** + * Asserts that a resource error response has the given message. + * + * (Also asserts that the expected error MIME type is present, but this is + * defined globally for the test via static::$expectedErrorMimeType, because + * all error responses should use the same MIME type.) + * + * @param int $expected_status_code + * The expected response status. + * @param string $expected_message + * The expected error message. + * @param \Psr\Http\Message\ResponseInterface $response + * The error response to assert. + */ + protected function assertResourceErrorResponse($expected_status_code, $expected_message, ResponseInterface $response) { + // @todo Either add this to \Drupal\serialization\Encoder\JsonEncoder, or + // figure out how to let tests specify encoder options, and figure out + // whether they should apply to just error responses or to everything. + $encode_options = ['json_encode_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT]; + $expected_body = $this->serializer->encode(['message' => $expected_message], static::$format, $encode_options); + $this->assertResourceResponse($expected_status_code, $expected_body, $response); + } + }