tests/src/Functional/ResourceTestBase.php | 41 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/tests/src/Functional/ResourceTestBase.php b/tests/src/Functional/ResourceTestBase.php index 5600c11..8332cce 100644 --- a/tests/src/Functional/ResourceTestBase.php +++ b/tests/src/Functional/ResourceTestBase.php @@ -8,6 +8,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\Core\Entity\ContentEntityNullStorage; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Url; use Drupal\field\Entity\FieldConfig; @@ -110,12 +111,20 @@ abstract class ResourceTestBase extends BrowserTestBase { protected $serializer; /** + * The Entity-to-JSON-API service. + * + * @var \Drupal\jsonapi\EntityToJsonApi + */ + protected $entityToJsonApi; + + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->serializer = $this->container->get('serializer'); + $this->entityToJsonApi = $this->container->get('jsonapi.entity.to_jsonapi'); // Ensure the anonymous user role has no permissions at all. $user_role = Role::load(RoleInterface::ANONYMOUS_ID); @@ -811,36 +820,30 @@ abstract class ResourceTestBase extends BrowserTestBase { // 201 for well-formed request. $response = $this->request('POST', $url, $request_options); - $this->assertResourceResponse(201, FALSE, $response); - if ($has_canonical_url) { - $location = $this->entityStorage->load(static::$firstCreatedEntityId)->toUrl('canonical')->setAbsolute(TRUE)->toString(); - $this->assertSame([$location], $response->getHeader('Location')); - } - else { - $this->assertSame([], $response->getHeader('Location')); - } + $this->assertResourceResponse(201, FALSE, $response, ['http_response', 'node:2'], $this->getExpectedCacheContexts()); + // @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463. + $location = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [static::$entityTypeId => $this->entityStorage->load(static::$firstCreatedEntityId)->uuid()])->setAbsolute(TRUE)->toString(); + //$location = $this->entityStorage->load(static::$firstCreatedEntityId)->toUrl('jsonapi')->setAbsolute(TRUE)->toString(); + $this->assertSame([$location], $response->getHeader('Location')); $this->assertFalse($response->hasHeader('X-Drupal-Cache')); // If the entity is stored, perform extra checks. if (get_class($this->entityStorage) !== ContentEntityNullStorage::class) { // Assert that the entity was indeed created, and that the response body // contains the serialized created entity. $created_entity = $this->entityStorage->loadUnchanged(static::$firstCreatedEntityId); - $created_entity_normalization = $this->serializer->normalize($created_entity, static::$format, ['account' => $this->account]); + $created_entity_normalization = $this->entityToJsonApi->normalize($created_entity); // @todo Remove this if-test in https://www.drupal.org/node/2543726: execute // its body unconditionally. if (static::$entityTypeId !== 'taxonomy_term') { - $this->assertSame($created_entity_normalization, $this->serializer->decode((string) $response->getBody(), static::$format)); + $decoded_response_body = $this->serializer->decode((string) $response->getBody(), 'api_json'); + // @todo Remove the two lines below once https://www.drupal.org/project/jsonapi/issues/2925043 lands. + unset($created_entity_normalization['links']); + unset($decoded_response_body['links']); + $this->assertSame($created_entity_normalization, $decoded_response_body); } // Assert that the entity was indeed created using the POSTed values. - foreach ($this->getNormalizedPostEntity() as $field_name => $field_normalization) { - // Some top-level keys in the normalization may not be fields on the - // entity (for example '_links' and '_embedded' in the HAL normalization). - if ($created_entity->hasField($field_name)) { - // Subset, not same, because we can e.g. send just the target_id for the - // bundle in a POST request; the response will include more properties. - $this->assertArraySubset(static::castToString($field_normalization), $created_entity->get($field_name) - ->getValue(), TRUE); - } + foreach ($this->getNormalizedPostEntity()['data']['attributes'] as $field_name => $field_normalization) { + $this->assertSame($field_normalization, $created_entity_normalization['data']['attributes'][$field_name]); } } }