You are here

public function EntityResourceTest::testRemoveFromRelationshipData in JSON:API 8.2

@covers ::removeFromRelationshipData @dataProvider removeFromRelationshipDataProvider

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testRemoveFromRelationshipData($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();
  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 ($deleted_rels as $deleted_rel) {
    $payload['data'][] = [
      'type' => $deleted_rel
        ->getTypeName(),
      'id' => $deleted_rel
        ->getId(),
    ];
  }
  $request = Request::create('/jsonapi/node/article/' . $this->node
    ->uuid() . '/relationships/field_relationships', 'DELETE', [], [], [], [], Json::encode($payload));
  $response = $this->entityResource
    ->removeFromRelationshipData($resource_type, $this->node, 'field_relationships', $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());
}