.../rest/tests/modules/rest_test/rest_test.module | 17 ++++++++------- .../EntityResource/EntityResourceTestBase.php | 24 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 13 deletions(-) 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 197b82f..7ee3aa6 100644 --- a/core/modules/rest/tests/modules/rest_test/rest_test.module +++ b/core/modules/rest/tests/modules/rest_test/rest_test.module @@ -35,14 +35,13 @@ function rest_test_entity_field_access($operation, FieldDefinitionInterface $fie } /** - * Implements hook_entity_base_field_info(). + * Implements hook_entity_bundle_field_info_alter(). + * + * @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp() + * @todo Remove this in https://www.drupal.org/node/2905890. */ -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'); - - return $fields; +function rest_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { + if (!empty($fields['rest_test_validation'])) { + $fields['rest_test_validation']->addConstraint('rest_test_validation', []); + } } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 2354ccb..9517b62 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -180,12 +180,30 @@ public function setUp() { ->setTranslatable(FALSE) ->save(); - // Reload entity so that it has the new field. + // Add field for testing validation. + // @see rest_test_entity_bundle_field_info_alter() + FieldStorageConfig::create([ + 'entity_type' => static::$entityTypeId, + 'field_name' => 'rest_test_validation', + 'type' => 'string', + ]) + ->setCardinality(2) + ->save(); + FieldConfig::create([ + 'entity_type' => static::$entityTypeId, + 'field_name' => 'rest_test_validation', + 'bundle' => $this->entity->bundle(), + 'description' => 'A text field with some special validations attached used for testing purposes', + ]) + ->setLabel('REST test validation field') + ->setTranslatable(FALSE) + ->save(); + + // Reload entity so that it has the new fields. $this->entity = $this->entityStorage->loadUnchanged($this->entity->id()); // 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']); $this->entity->save(); } @@ -471,7 +489,6 @@ public function testGet() { ] ]; } - static::recursiveKSort($expected); $actual = $this->serializer->decode((string) $response->getBody(), static::$format); static::recursiveKSort($actual); @@ -578,7 +595,6 @@ public function testGet() { // ::formatExpectedTimestampValue() to generate the timestamp value. This // will take into account the above config setting. $expected = $this->getExpectedNormalizedEntity(); - $expected += [ 'rest_test_validation' => [ [