You are here

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

@covers ::getCollection

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testGetSortedCollection() {
  $request = Request::create('/jsonapi/node/article');
  $request->query = new ParameterBag([
    'sort' => '-type',
  ]);
  $entity_resource = $this
    ->createEntityResource();

  // Get the response.
  $resource_type = $this->container
    ->get('jsonapi.resource_type.repository')
    ->get('node_type', 'node_type');
  $response = $entity_resource
    ->getCollection($resource_type, $request);

  // Assertions.
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(Data::class, $response
    ->getResponseData()
    ->getData());
  $this
    ->assertCount(2, $response
    ->getResponseData()
    ->getData());

  // `drupal_internal__type` is the alias for a node_type entity's ID field.
  $this
    ->assertEquals($response
    ->getResponseData()
    ->getData()
    ->toArray()[0]
    ->getField('drupal_internal__type'), 'lorem');
  $expected_cache_tags = [
    'config:node.type.article',
    'config:node.type.lorem',
    'config:node_type_list',
  ];
  $this
    ->assertSame($expected_cache_tags, $response
    ->getCacheableMetadata()
    ->getCacheTags());
}