comparison core/modules/jsonapi/tests/src/Functional/EntryPointTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Functional;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\user\Traits\UserCreationTrait;
9 use GuzzleHttp\RequestOptions;
10
11 /**
12 * Makes assertions about the JSON:API behavior for internal entities.
13 *
14 * @group jsonapi
15 *
16 * @internal
17 */
18 class EntryPointTest extends BrowserTestBase {
19
20 use JsonApiRequestTestTrait;
21 use UserCreationTrait;
22
23 /**
24 * {@inheritdoc}
25 */
26 protected static $modules = [
27 'node',
28 'jsonapi',
29 'basic_auth',
30 ];
31
32 /**
33 * Test GETing the entry point.
34 */
35 public function testEntryPoint() {
36 $request_options = [];
37 $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
38 $response = $this->request('GET', Url::fromUri('base://jsonapi'), $request_options);
39 $document = Json::decode((string) $response->getBody());
40 $expected_cache_contexts = [
41 'url.site',
42 'user.roles:authenticated',
43 ];
44 $this->assertTrue($response->hasHeader('X-Drupal-Cache-Contexts'));
45 $optimized_expected_cache_contexts = \Drupal::service('cache_contexts_manager')->optimizeTokens($expected_cache_contexts);
46 $this->assertSame($optimized_expected_cache_contexts, explode(' ', $response->getHeader('X-Drupal-Cache-Contexts')[0]));
47 $links = $document['links'];
48 $this->assertRegExp('/.*\/jsonapi/', $links['self']['href']);
49 $this->assertRegExp('/.*\/jsonapi\/user\/user/', $links['user--user']['href']);
50 $this->assertRegExp('/.*\/jsonapi\/node_type\/node_type/', $links['node_type--node_type']['href']);
51 $this->assertArrayNotHasKey('meta', $document);
52
53 // A `me` link must be present for authenticated users.
54 $user = $this->createUser();
55 $request_options[RequestOptions::HEADERS]['Authorization'] = 'Basic ' . base64_encode($user->name->value . ':' . $user->passRaw);
56 $response = $this->request('GET', Url::fromUri('base://jsonapi'), $request_options);
57 $document = Json::decode((string) $response->getBody());
58 $this->assertArrayHasKey('meta', $document);
59 $this->assertStringEndsWith('/jsonapi/user/user/' . $user->uuid(), $document['meta']['links']['me']['href']);
60 }
61
62 }