You are here

public function EntityResourceTest::testGetRelated in JSON:API 8

Same name and namespace in other branches
  1. 8.2 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.
  $entity_resource = $this
    ->buildEntityResource('node', 'article', [
    'uid' => [
      new ResourceType('user', 'user', NULL),
    ],
    'roles' => [
      new ResourceType('user_role', 'user_role', NULL),
    ],
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $response = $entity_resource
    ->getRelated($this->node, 'uid', new Request());
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(User::class, $response
    ->getResponseData()
    ->getData());
  $this
    ->assertEquals(1, $response
    ->getResponseData()
    ->getData()
    ->id());
  $this
    ->assertEquals([
    'node:1',
    'user:1',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());

  // to-many relationship.
  $response = $entity_resource
    ->getRelated($this->node4, 'field_relationships', new Request());
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(EntityCollection::class, $response
    ->getResponseData()
    ->getData());
  $this
    ->assertEquals([
    'node:1',
    'node:2',
    'node:3',
    'node:4',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());
}