You are here

public function EntityResourceTest::testCreateRelationship in JSON:API 8

@covers ::createRelationship

File

tests/src/Kernel/Controller/EntityResourceTest.php, line 675

Class

EntityResourceTest
@coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testCreateRelationship() {
  $parsed_field_list = $this->container
    ->get('plugin.manager.field.field_type')
    ->createFieldItemList($this->node, 'field_relationships', [
    [
      'target_id' => $this->node
        ->id(),
    ],
  ]);
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('edit any article content')
    ->save();
  $entity_resource = $this
    ->buildEntityResource('node', 'article', [
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $response = $entity_resource
    ->createRelationship($this->node, 'field_relationships', $parsed_field_list, new 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());
}