You are here

public function EntityResourceTest::testGetRelated 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::testGetRelated()

@covers ::getRelated

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testGetRelated() {

  // to-one relationship.
  $resource_type = new ResourceType('node', 'article', NULL);
  $resource_type
    ->setRelatableResourceTypes([
    'uid' => [
      new ResourceType('user', 'user', NULL),
    ],
    'roles' => [
      new ResourceType('user_role', 'user_role', NULL),
    ],
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $response = $this->entityResource
    ->getRelated($resource_type, $this->node, 'uid', Request::create('/jsonapi/node/article/' . $this->node
    ->uuid(), '/uid'));
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(ResourceObject::class, $response
    ->getResponseData()
    ->getData()
    ->toArray()[0]);
  $this
    ->assertEquals($this->user
    ->uuid(), $response
    ->getResponseData()
    ->getData()
    ->toArray()[0]
    ->getId());
  $this
    ->assertEquals([
    'node:1',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());

  // to-many relationship.
  $response = $this->entityResource
    ->getRelated($resource_type, $this->node4, 'field_relationships', Request::create('/jsonapi/node/article/' . $this->node4
    ->uuid(), '/field_relationships'));
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(Data::class, $response
    ->getResponseData()
    ->getData());
  $this
    ->assertEquals([
    'node:4',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());
}