function JsonApiRelationshipTest::testPatchHandleUUIDPropertyReferenceFieldIssue3127883
Test relationships without target_id as main property.
See also
https://www.drupal.org/project/drupal/issues/3476224
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiRelationshipTest.php, line 82
Class
- JsonApiRelationshipTest
- JSON:API resource tests.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPatchHandleUUIDPropertyReferenceFieldIssue3127883() : void {
$this->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
$user = $this->drupalCreateUser([
'administer entity_test content',
'view test entity',
]);
// Create parent and child entities.
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$parentEntity = $storage->create([
'type' => $this->bundle,
]);
$parentEntity->save();
$childUuid = $this->container
->get('uuid')
->generate();
$childEntity = $storage->create([
'type' => $this->bundle,
'uuid' => $childUuid,
]);
$childEntity->save();
$uuid = $childEntity->uuid();
$this->assertEquals($childUuid, $uuid);
// 1. Successful PATCH to the related endpoint.
$url = Url::fromUri(sprintf('internal:/jsonapi/%s/%s/%s/relationships/%s', $this->entityTypeId, $this->bundle, $parentEntity->uuid(), $this->fieldName));
$request_options = [
RequestOptions::HEADERS => [
'Content-Type' => 'application/vnd.api+json',
'Accept' => 'application/vnd.api+json',
],
RequestOptions::AUTH => [
$user->getAccountName(),
$user->pass_raw,
],
RequestOptions::JSON => [
'data' => [
'id' => $childUuid,
'type' => sprintf('%s--%s', $this->entityTypeId, $this->bundle),
],
],
];
$response = $this->request('PATCH', $url, $request_options);
$this->assertSame(204, $response->getStatusCode(), (string) $response->getBody());
$parentEntity = $storage->loadUnchanged($parentEntity->id());
$this->assertEquals($childEntity->uuid(), $parentEntity->get($this->fieldName)->target_uuid);
// Reset the relationship.
$parentEntity->set($this->fieldName, NULL)
->save();
$parentEntity = $storage->loadUnchanged($parentEntity->id());
$this->assertTrue($parentEntity->get($this->fieldName)
->isEmpty());
// 2. Successful PATCH to individual endpoint.
$url = Url::fromUri(sprintf('internal:/jsonapi/%s/%s/%s', $this->entityTypeId, $this->bundle, $parentEntity->uuid()));
$request_options[RequestOptions::JSON] = [
'data' => [
'id' => $parentEntity->uuid(),
'type' => sprintf('%s--%s', $this->entityTypeId, $this->bundle),
'relationships' => [
$this->fieldName => [
'data' => [
[
'id' => $childUuid,
'type' => sprintf('%s--%s', $this->entityTypeId, $this->bundle),
],
],
],
],
],
];
$response = $this->request('PATCH', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), (string) $response->getBody());
$parentEntity = $storage->loadUnchanged($parentEntity->id());
$this->assertEquals($childEntity->uuid(), $parentEntity->get($this->fieldName)->target_uuid);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.