diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php index 97ba3a619a..773ececb3f 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php @@ -3,7 +3,6 @@ namespace Drupal\rest\Plugin\rest\resource; use Drupal\Component\Render\PlainTextOutput; -use Drupal\Core\Entity\EntityConstraintViolationList; use Drupal\Core\Entity\EntityConstraintViolationListInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; diff --git a/core/modules/rest/tests/modules/rest_test/rest_test.module b/core/modules/rest/tests/modules/rest_test/rest_test.module index 197b82f0c2..f55eda47f0 100644 --- a/core/modules/rest/tests/modules/rest_test/rest_test.module +++ b/core/modules/rest/tests/modules/rest_test/rest_test.module @@ -39,10 +39,12 @@ function rest_test_entity_field_access($operation, FieldDefinitionInterface $fie */ function rest_test_entity_base_field_info(EntityTypeInterface $entity_type) { $fields = []; - $fields['rest_test_validation'] = BaseFieldDefinition::create('string') - ->setLabel(t('REST test validation field')) - ->setDescription(t('A text field with some special validations attached used for testing purposes')) - ->addConstraint('rest_test_validation'); + if ($entity_type->id() === 'entity_test') { + $fields['rest_test_validation'] = BaseFieldDefinition::create('string') + ->setLabel(t('REST test validation field')) + ->setDescription(t('A text field with some special validations attached used for testing purposes')) + ->addConstraint('rest_test_validation'); + } return $fields; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index e9ef9c9d6a..5b5101445a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -199,11 +199,6 @@ protected function getExpectedNormalizedEntity() { 'format' => 'plain_text', ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index a598861f57..2c9552a64d 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -185,7 +185,10 @@ public function setUp() { // Set a default value on the field. $this->entity->set('field_rest_test', ['value' => 'All the faith he had had had had no effect on the outcome of his life.']); - $this->entity->set('rest_test_validation', ['value' => 'allowed value']); + + if ($this->entity->hasField('rest_test_validation')) { + $this->entity->set('rest_test_validation', ['value' => 'allowed value']); + } $this->entity->save(); } } @@ -461,7 +464,7 @@ public function testGet() { // for the keys with the array order the same (it needs to match with // identical comparison). $expected = $this->getExpectedNormalizedEntity(); - if ($this->entity instanceof FieldableEntityInterface) { + if ($this->entity instanceof FieldableEntityInterface && $this->entity->hasField('rest_test_validation')) { $expected += [ 'rest_test_validation' => [ [ @@ -474,7 +477,7 @@ public function testGet() { static::recursiveKSort($expected); $actual = $this->serializer->decode((string) $response->getBody(), static::$format); static::recursiveKSort($actual); - $this->assertEquals($expected, $actual); + $this->assertSame($expected, $actual); // Not only assert the normalization, also assert deserialization of the // response results in the expected object. @@ -533,7 +536,7 @@ public function testGet() { // normalized entity's values to strings. This ensures the BC layer for // bc_primitives_as_strings works as expected. $expected = $this->getExpectedNormalizedEntity(); - if ($this->entity instanceof FieldableEntityInterface) { + if ($this->entity instanceof FieldableEntityInterface && $this->entity->hasField('rest_test_validation')) { $expected += [ 'rest_test_validation' => [ [ @@ -1085,19 +1088,21 @@ public function testPatch() { $modified_entity->get($patch_protected_field_name)->setValue($original_values[$patch_protected_field_name]); } - // Set the rest_test_validation field to always fail validation, which - // allows asserting that not modifying that field does not trigger - // validation errors. - $this->entity->set('rest_test_validation', 'ALWAYS_FAIL'); - $this->entity->save(); - - // Change the rest_test_validation field to prove that then its validation - // does run. In subsequent test assertions, it will not be modified, and - // then should not trigger validation errors. - $valid_request_body = $this->getNormalizedPatchEntity() + $this->serializer->normalize($this->entity, static::$format); - $request_options[RequestOptions::BODY] = $this->serializer->serialize($valid_request_body, static::$format); - $response = $this->request('PATCH', $url, $request_options); - $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nrest_test_validation: REST test validation failed\n", $response); + if ($this->entity->hasField('rest_test_validation')) { + // Set the rest_test_validation field to always fail validation, which + // allows asserting that not modifying that field does not trigger + // validation errors. + $this->entity->set('rest_test_validation', 'ALWAYS_FAIL'); + $this->entity->save(); + + // Change the rest_test_validation field to prove that then its validation + // does run. In subsequent test assertions, it will not be modified, and + // then should not trigger validation errors. + $valid_request_body = $this->getNormalizedPatchEntity() + $this->serializer->normalize($this->entity, static::$format); + $request_options[RequestOptions::BODY] = $this->serializer->serialize($valid_request_body, static::$format); + $response = $this->request('PATCH', $url, $request_options); + $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nrest_test_validation: REST test validation failed\n", $response); + } } // 200 for well-formed PATCH request that sends all fields (even including diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php index 74dacc1531..2257d6c275 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php @@ -107,11 +107,6 @@ protected function getExpectedNormalizedEntity() { 'url' => $author->toUrl()->toString(), ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; return $normalization; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php index d2105c4e1f..1e79395a60 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php @@ -128,11 +128,6 @@ protected function getExpectedNormalizedEntity() { 'modified' => [ $this->formatExpectedTimestampItemValues(123456789), ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php index 4db1b0ce7b..f217b97fba 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php @@ -119,11 +119,6 @@ protected function getExpectedNormalizedEntity() { $this->formatExpectedTimestampItemValues(123456789), ], 'guid' => [], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php index 6c042b0c30..0b1f967387 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php @@ -172,11 +172,6 @@ protected function getExpectedNormalizedEntity() { ], ], 'parent' => [], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } 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 1cb5384bf0..b4fc553f08 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php @@ -180,11 +180,6 @@ protected function getExpectedNormalizedEntity() { 'langcode' => 'en', ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php index 205431ecc6..36be5b58f0 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php @@ -114,11 +114,6 @@ protected function getExpectedNormalizedEntity() { 'value' => TRUE, ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } 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 861c9939e9..44a4e83033 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php @@ -129,11 +129,6 @@ protected function getExpectedNormalizedEntity() { 'langcode' => 'en', ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php index 55f60026f6..caf2909762 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php @@ -111,11 +111,6 @@ protected function getExpectedNormalizedEntity() { 'value' => TRUE, ], ], - 'rest_test_validation' => [ - [ - 'value' => 'allowed value', - ], - ], ]; }