You are here

public function EntryPointTest::testEntryPoint in farmOS 2.x

Test GET to the entry point.

We extend the core JSONAPI EntryPoint controller to include a 'meta' key in the root controller. We need to test that we aren't changing anything else.

This test is a copy of the core JSONAPI testEntryPoint test with the following modifications:

  • The base path is /api, not /jsonapi
  • The root document MUST have a 'meta' key.

See also

\Drupal\Tests\jsonapi\Functional\EntryPointTest

File

modules/core/api/tests/src/Functional/EntryPointTest.php, line 44

Class

EntryPointTest
Tests the API entry point functionality.

Namespace

Drupal\Tests\farm_api\Functional

Code

public function testEntryPoint() {
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $response = $this
    ->request('GET', Url::fromUri('base://api'), $request_options);
  $document = Json::decode((string) $response
    ->getBody());
  $expected_cache_contexts = [
    'url.site',
    'user.roles:authenticated',
  ];
  $this
    ->assertTrue($response
    ->hasHeader('X-Drupal-Cache-Contexts'));
  $optimized_expected_cache_contexts = \Drupal::service('cache_contexts_manager')
    ->optimizeTokens($expected_cache_contexts);
  $this
    ->assertSame($optimized_expected_cache_contexts, explode(' ', $response
    ->getHeader('X-Drupal-Cache-Contexts')[0]));
  $links = $document['links'];
  $this
    ->assertRegExp('/.*\\/api/', $links['self']['href']);
  $this
    ->assertRegExp('/.*\\/api\\/user\\/user/', $links['user--user']['href']);
  $this
    ->assertRegExp('/.*\\/api\\/node_type\\/node_type/', $links['node_type--node_type']['href']);

  // farm_api alters the root document to include a 'meta' key.
  $this
    ->assertArrayHasKey('meta', $document);

  // A `me` link must be present for authenticated users.
  $user = $this
    ->createUser();
  $request_options[RequestOptions::HEADERS]['Authorization'] = 'Basic ' . base64_encode($user->name->value . ':' . $user->passRaw);
  $response = $this
    ->request('GET', Url::fromUri('base://api'), $request_options);
  $document = Json::decode((string) $response
    ->getBody());
  $this
    ->assertArrayHasKey('meta', $document);
  $this
    ->assertStringEndsWith('/api/user/user/' . $user
    ->uuid(), $document['meta']['links']['me']['href']);
}