You are here

public function JsonApiDefaultsFunctionalTest::testRead in JSON:API Extras 8.3

Test the GET method.

File

modules/jsonapi_defaults/tests/src/Functional/JsonApiDefaultsFunctionalTest.php, line 33

Class

JsonApiDefaultsFunctionalTest
The test class for the JSON API Defaults functionality.

Namespace

Drupal\Tests\jsonapi_defaults\Functional

Code

public function testRead() {
  $this
    ->createDefaultContent(4, 5, TRUE, TRUE, static::IS_NOT_MULTILINGUAL);

  // 1. Apply default filters and includes on a resource and a related
  // resource.
  $response = $this
    ->drupalGet('/api/articles');
  $parsed_response = Json::decode($response);
  $this
    ->assertArrayHasKey('data', $parsed_response);
  $this
    ->assertCount(1, $parsed_response['data']);
  $this
    ->assertEquals(3, $parsed_response['data'][0]['attributes']['internalId']);
  $this
    ->assertArrayHasKey('included', $parsed_response);
  $this
    ->assertGreaterThan(0, count($parsed_response['included']));

  // Make sure related resources don't fail.
  $response = $this
    ->drupalGet('/api/articles/' . $this->nodes[0]
    ->uuid() . '/owner');
  $parsed_response = Json::decode($response);
  $this
    ->assertArrayHasKey('data', $parsed_response);
  $this
    ->assertEquals('user--user', $parsed_response['data']['type']);

  // 2. Merge default filters with explicit filters.
  $response = $this
    ->drupalGet('/api/articles', [
    'query' => [
      'filter' => [
        'i' => [
          'condition' => [
            'path' => 'internalId',
            'value' => '2',
          ],
        ],
      ],
    ],
  ]);
  $parsed_response = Json::decode($response);
  $this
    ->assertArrayHasKey('data', $parsed_response);

  // internalId cannot be 2 and 3 at the same time.
  $this
    ->assertCount(0, $parsed_response['data']);

  // 3. Override the default includes.
  $response = $this
    ->drupalGet('/api/articles', [
    'query' => [
      'include' => '',
    ],
  ]);
  $parsed_response = Json::decode($response);
  $this
    ->assertArrayNotHasKey('included', $parsed_response);

  // 4. Using the default sorting check the order.
  // Unset filters of resource config in this test as those limit the results.
  $this
    ->setResouceConfigValue([
    'default_filter' => [],
  ]);
  $this->nodes[0]
    ->setTitle('a');
  $this->nodes[0]
    ->save();
  $this->nodes[1]
    ->setTitle('b');
  $this->nodes[1]
    ->save();
  $this->nodes[2]
    ->setTitle('c');
  $this->nodes[2]
    ->save();
  $this->nodes[3]
    ->setTitle('d');
  $this->nodes[3]
    ->save();
  $response = $this
    ->drupalGet('/api/articles');
  $parsed_response = Json::decode($response);

  // Check if order is as expected.
  $this
    ->assertEquals('d', $parsed_response['data'][0]['attributes']['title']);
  $this
    ->assertEquals('c', $parsed_response['data'][1]['attributes']['title']);
  $this
    ->assertEquals('b', $parsed_response['data'][2]['attributes']['title']);
  $this
    ->assertEquals('a', $parsed_response['data'][3]['attributes']['title']);

  // 5. Override default sorting with explicit sorting.
  $response = $this
    ->drupalGet('/api/articles', [
    'query' => [
      'sort' => [
        'title' => [
          'path' => 'title',
          'direction' => 'ASC',
        ],
      ],
    ],
  ]);
  $parsed_response = Json::decode($response);

  // Check if order changed as expected.
  $this
    ->assertEquals('a', $parsed_response['data'][0]['attributes']['title']);
  $this
    ->assertEquals('b', $parsed_response['data'][1]['attributes']['title']);
  $this
    ->assertEquals('c', $parsed_response['data'][2]['attributes']['title']);
  $this
    ->assertEquals('d', $parsed_response['data'][3]['attributes']['title']);
}