You are here

public function EntityResourceTest::testDeleteRelationship in JSON:API 8

@covers ::deleteRelationship @dataProvider deleteRelationshipProvider

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testDeleteRelationship($deleted_rels, $kept_rels) {
  $this->node->field_relationships
    ->appendItem([
    'target_id' => $this->node
      ->id(),
  ]);
  $this->node->field_relationships
    ->appendItem([
    'target_id' => $this->node2
      ->id(),
  ]);
  $this->node
    ->save();
  $parsed_field_list = $this->container
    ->get('plugin.manager.field.field_type')
    ->createFieldItemList($this->node, 'field_relationships', $deleted_rels);
  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
    ->deleteRelationship($this->node, 'field_relationships', $parsed_field_list, new Request());

  // As a side effect, the node will also be saved.
  $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($kept_rels, $field_list
    ->getValue());
  $this
    ->assertEquals(204, $response
    ->getStatusCode());
}