tests/src/Functional/EntityTestTest.php | 177 ++++++++++++++++++++++++++++++++ tests/src/Functional/TermTest.php | 2 +- 2 files changed, 178 insertions(+), 1 deletion(-) diff --git a/tests/src/Functional/EntityTestTest.php b/tests/src/Functional/EntityTestTest.php new file mode 100644 index 0000000..fcc3e40 --- /dev/null +++ b/tests/src/Functional/EntityTestTest.php @@ -0,0 +1,177 @@ +grantPermissionsToTestedRole(['access content']); + + // @todo Remove this in + if ($method === 'PATCH' || $method === 'POST') { + $this->setUpAuthorization('GET'); + } + + switch ($method) { + case 'GET': + $this->grantPermissionsToTestedRole(['view test entity']); + break; + case 'POST': + $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities']); + break; + case 'PATCH': + case 'DELETE': + $this->grantPermissionsToTestedRole(['administer entity_test content']); + break; + } + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + // Set flag so that internal field 'internal_string_field' is created. + // @see entity_test_entity_base_field_info() + $this->container->get('state')->set('entity_test.internal_field', TRUE); + \Drupal::entityDefinitionUpdateManager()->applyUpdates(); + + $entity_test = EntityTest::create([ + 'name' => 'Llama', + 'type' => 'entity_test', + // Set a value for the internal field to confirm that it will not be + // returned in normalization. + // @see entity_test_entity_base_field_info(). + 'internal_string_field' => [ + 'value' => 'This value shall not be internal!', + ], + ]); + $entity_test->setOwnerId(0); + $entity_test->save(); + + return $entity_test; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $self_url = Url::fromUri('base:/jsonapi/entity_test/entity_test/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl(); + $author = User::load(0); + return [ + 'jsonapi' => [ + 'meta' => [ + 'links' => [ + 'self' => 'http://jsonapi.org/format/1.0/', + ], + ], + 'version' => '1.0', + ], + 'links' => [ + 'self' => $self_url, + ], + 'data' => [ + 'id' => $this->entity->uuid(), + 'type' => 'entity_test--entity_test', + 'links' => [ + 'self' => $self_url, + ], + 'attributes' => [ + 'id' => 1, + 'created' => (int) $this->entity->get('created')->value, + // @todo uncomment this in https://www.drupal.org/project/jsonapi/issues/2929932 +// 'created' => $this->formatExpectedTimestampItemValues((int) $this->entity->get('created')->value), + 'field_test_text' => NULL, + // @todo Remove this in https://www.drupal.org/project/jsonapi/issues/2921257. + 'internal_string_field' => 'This value shall not be internal!', + 'langcode' => 'en', + 'name' => 'Llama', + 'type' => 'entity_test', + 'uuid' => $this->entity->uuid(), + ], + 'relationships' => [ + 'user_id' => [ + 'data' => [ + 'id' => $author->uuid(), + 'type' => 'user--user', + ], + 'links' => [ + 'related' => $self_url . '/user_id', + 'self' => $self_url . '/relationships/user_id', + ], + ], + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return [ + 'data' => [ + 'type' => 'entity_test--entity_test', + 'attributes' => [ + 'name' => 'Dramallama', + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + // @todo Remove this in https://www.drupal.org/project/jsonapi/issues/2940336 + return "The 'access content' permission is required."; + + switch ($method) { + case 'GET': + return "The 'view test entity' permission is required."; + case 'POST': + return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test entity_test_with_bundle entities'."; + default: + return parent::getExpectedUnauthorizedAccessMessage($method); + } + } + +} diff --git a/tests/src/Functional/TermTest.php b/tests/src/Functional/TermTest.php index 4394791..b3782c7 100644 --- a/tests/src/Functional/TermTest.php +++ b/tests/src/Functional/TermTest.php @@ -250,7 +250,7 @@ class TermTest extends ResourceTestBase { * {@inheritdoc} */ protected function getExpectedUnauthorizedAccessMessage($method) { - // @todo Remove this in + // @todo Remove this in https://www.drupal.org/project/jsonapi/issues/2940336 return "The 'access content' permission is required."; switch ($method) {