public function EntityResourceTest::testAddToRelationshipData in JSON:API 8.2
@covers ::addToRelationshipData
File
- tests/
src/ Kernel/ Controller/ EntityResourceTest.php, line 550
Class
- EntityResourceTest
- @coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @group jsonapi @group legacy
Namespace
Drupal\Tests\jsonapi\Kernel\ControllerCode
public function testAddToRelationshipData() {
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 = Json::encode([
'data' => [
[
'type' => 'node--article',
'id' => $this->node
->uuid(),
],
],
]);
$request = Request::create('/jsonapi/node/article/' . $this->node
->uuid() . '/relationships/field_relationships', 'POST', [], [], [], [], $payload);
$response = $this->entityResource
->addToRelationshipData($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([
[
'target_id' => 1,
],
], $field_list
->getValue());
$this
->assertEquals(204, $response
->getStatusCode());
}