class EntryPointTest
Same name and namespace in other branches
- 10 core/modules/jsonapi/tests/src/Functional/EntryPointTest.php \Drupal\Tests\jsonapi\Functional\EntryPointTest
- 11.x core/modules/jsonapi/tests/src/Functional/EntryPointTest.php \Drupal\Tests\jsonapi\Functional\EntryPointTest
- 8.9.x core/modules/jsonapi/tests/src/Functional/EntryPointTest.php \Drupal\Tests\jsonapi\Functional\EntryPointTest
Makes assertions about the JSON:API behavior for internal entities.
@group jsonapi
@internal
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\jsonapi\Functional\EntryPointTest uses \Drupal\Tests\jsonapi\Functional\JsonApiRequestTestTrait, \Drupal\Tests\user\Traits\UserCreationTrait extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of EntryPointTest
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ EntryPointTest.php, line 18
Namespace
Drupal\Tests\jsonapi\FunctionalView source
class EntryPointTest extends BrowserTestBase {
use JsonApiRequestTestTrait;
use UserCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'jsonapi',
'basic_auth',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Test GET to the entry point.
*/
public function testEntryPoint() {
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$response = $this->request('GET', Url::fromUri('base://jsonapi'), $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->assertMatchesRegularExpression('/.*\\/jsonapi/', $links['self']['href']);
$this->assertMatchesRegularExpression('/.*\\/jsonapi\\/user\\/user/', $links['user--user']['href']);
$this->assertMatchesRegularExpression('/.*\\/jsonapi\\/node_type\\/node_type/', $links['node_type--node_type']['href']);
$this->assertArrayNotHasKey('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://jsonapi'), $request_options);
$document = Json::decode((string) $response->getBody());
$this->assertArrayHasKey('meta', $document);
$this->assertStringEndsWith('/jsonapi/user/user/' . $user->uuid(), $document['meta']['links']['me']['href']);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.