View source
<?php
namespace Drupal\Tests\farm_api\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
use Drupal\Tests\jsonapi\Functional\JsonApiRequestTestTrait;
use GuzzleHttp\RequestOptions;
class EntryPointTest extends FarmBrowserTestBase {
use JsonApiRequestTestTrait;
protected static $modules = [
'basic_auth',
'node',
'farm_api',
'views',
];
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']);
$this
->assertArrayHasKey('meta', $document);
$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']);
}
public function testFarmMeta() {
$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());
$this
->assertArrayHasKey('meta', $document);
$this
->assertArrayHasKey('farm', $document['meta']);
$expected_values = [
'name' => $this
->config('system.site')
->get('name'),
'url' => $this->baseUrl,
'version' => '2.x',
];
foreach ($expected_values as $key => $value) {
$this
->assertArrayHasKey($key, $document['meta']['farm']);
$this
->assertEquals($value, $document['meta']['farm'][$key]);
}
}
}