.../EntityTest/EntityTestMapItemNormalizerTest.php | 99 ++++++++++++++++------ .../entity_test/src/Entity/EntityTestMapField.php | 37 ++++++++ 2 files changed, 110 insertions(+), 26 deletions(-) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestMapItemNormalizerTest.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestMapItemNormalizerTest.php index 754f7e1..f7386da 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestMapItemNormalizerTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestMapItemNormalizerTest.php @@ -2,9 +2,9 @@ namespace Drupal\Tests\rest\Functional\EntityResource\EntityTest; -use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; +use Drupal\entity_test\Entity\EntityTestMapField; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; +use Drupal\user\Entity\User; /** * Test that MapItem are correctly exposed in REST. @@ -20,6 +20,11 @@ class EntityTestMapItemNormalizerTest extends EntityTestResourceTestBase { */ protected static $format = 'json'; + /** + * {@inheritdoc} + */ + protected static $entityTypeId = 'entity_test_map_field'; + protected static $mapValue = [ 'key1' => 'value', 'key2' => 'no, val you', @@ -37,36 +42,55 @@ class EntityTestMapItemNormalizerTest extends EntityTestResourceTestBase { /** * {@inheritdoc} */ + protected function setUpAuthorization($method) { + $this->grantPermissionsToTestedRole(['administer entity_test content']); + } + + /** + * {@inheritdoc} + */ protected function getExpectedNormalizedEntity() { - $expected = parent::getExpectedNormalizedEntity(); - $expected['field_map'] = [ - 0 => static::$mapValue + $author = User::load(0); + return [ + 'id' => [ + [ + 'value' => 1, + ], + ], + 'name' => [ + [ + 'value' => 'Llama', + ], + ], + 'created' => [ + $this->formatExpectedTimestampItemValues((int) $this->entity->get('created')->value), + ], + 'user_id' => [ + [ + 'target_id' => (int) $author->id(), + 'target_type' => 'user', + 'target_uuid' => $author->uuid(), + 'url' => $author->toUrl()->toString(), + ], + ], + 'data' => [ + 0 => static::$mapValue, + ], ]; - return $expected; } /** * {@inheritdoc} */ protected function createEntity() { - if (!FieldStorageConfig::loadByName('entity_test', 'field_map')) { - FieldStorageConfig::create([ - 'entity_type' => 'entity_test', - 'field_name' => 'field_map', - 'type' => 'map', - 'cardinality' => 1, - 'translatable' => FALSE, - ])->save(); - FieldConfig::create([ - 'entity_type' => 'entity_test', - 'field_name' => 'field_map', - 'bundle' => 'entity_test', - 'label' => 'Test field with map property', - ])->save(); - } - - $entity = parent::createEntity(); - $entity->get('field_map')->set(0, static::$mapValue); + $entity = EntityTestMapField::create([ + 'name' => 'Llama', + 'type' => 'entity_test_map_field', + 'data' => [ + 0 => static::$mapValue, + ] + ]); + $entity->setOwnerId(0); $entity->save(); return $entity; } @@ -75,11 +99,34 @@ protected function createEntity() { * {@inheritdoc} */ protected function getNormalizedPostEntity() { - return parent::getNormalizedPostEntity() + [ - 'field_map' => [ + return [ + 'name' => [ + [ + 'value' => 'Dramallama', + ], + ], + 'data' => [ 0 => static::$mapValue, ], ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) { + return parent::getExpectedUnauthorizedAccessMessage($method); + } + + return "The 'administer entity_test content' permission is required."; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + return ['user.permissions']; + } + } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php new file mode 100644 index 0000000..82bba32 --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php @@ -0,0 +1,37 @@ +setLabel(t('Data')) + ->setDescription(t('A serialized array of additional data.')); + + return $fields; + } + +}