public function EntityResourceTest::testReplaceRelationshipData in JSON:API 8.2
@covers ::replaceRelationshipData @dataProvider replaceRelationshipDataProvider
File
- tests/
src/ Kernel/ Controller/ EntityResourceTest.php, line 584
Class
- EntityResourceTest
- @coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @group jsonapi @group legacy
Namespace
Drupal\Tests\jsonapi\Kernel\ControllerCode
public function testReplaceRelationshipData($relationships) {
$this->node->field_relationships
->appendItem([
'target_id' => $this->node
->id(),
]);
$this->node
->save();
Role::load(Role::ANONYMOUS_ID)
->grantPermission('edit any article content')
->save();
$resource_type = new ResourceType('node', 'article', NULL);
$resource_type
->setRelatableResourceTypes([
'field_relationships' => [
new ResourceType('node', 'article', NULL),
],
]);
$payload = [
'data' => [],
];
foreach ($relationships as $relationship) {
$payload['data'][] = [
'type' => $relationship
->getTypeName(),
'id' => $relationship
->getId(),
];
}
$request = Request::create('/jsonapi/node/article/' . $this->node
->uuid() . '/relationships/field_relationships', 'PATCH', [], [], [], [], Json::encode($payload));
$response = $this->entityResource
->replaceRelationshipData($resource_type, $this->node, 'field_relationships', $request);
// As a side effect, the node will also be saved.
$this
->assertNotEmpty($this->node
->id());
$this
->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
->getResponseData());
$field_list = $response
->getResponseData()
->getData();
$this
->assertInstanceOf(EntityReferenceFieldItemListInterface::class, $field_list);
$this
->assertSame('field_relationships', $field_list
->getName());
$this
->assertEquals(array_map(function (ResourceIdentifier $identifier) {
return $identifier
->getId();
}, $relationships), array_map(function (EntityInterface $entity) {
return $entity
->uuid();
}, $field_list
->referencedEntities()));
$this
->assertEquals(204, $response
->getStatusCode());
}