You are here

public function EntityResourceTest::testPatchIndividual in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Controller/EntityResourceTest.php \Drupal\Tests\jsonapi\Kernel\Controller\EntityResourceTest::testPatchIndividual()

@covers ::patchIndividual

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testPatchIndividual() {
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('edit any article content')
    ->save();
  $payload = Json::encode([
    'data' => [
      'type' => 'article',
      'id' => $this->node
        ->uuid(),
      'attributes' => [
        'title' => 'PATCHED',
      ],
      'relationships' => [
        'field_relationships' => [
          'data' => [
            'id' => Node::load(1)
              ->uuid(),
            'type' => 'node--article',
          ],
        ],
      ],
    ],
  ]);
  $request = Request::create('/jsonapi/node/article/' . $this->node
    ->uuid(), 'PATCH', [], [], [], [], $payload);

  // Create a new EntityResource that uses uuid.
  $resource_type = new ResourceType('node', 'article', Node::class);
  $resource_type
    ->setRelatableResourceTypes([
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $response = $this->entityResource
    ->patchIndividual($resource_type, $this->node, $request);

  // As a side effect, the node will also be saved.
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $updated_node = $response
    ->getResponseData()
    ->getData()
    ->getIterator()
    ->offsetGet(0);
  $this
    ->assertInstanceOf(ResourceObject::class, $updated_node);
  $this
    ->assertSame('PATCHED', $this->node
    ->getTitle());
  $this
    ->assertSame([
    [
      'target_id' => '1',
    ],
  ], $this->node
    ->get('field_relationships')
    ->getValue());
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
}