You are here

protected function ApiDocsJsonApi::verifyAccess in Apigee API Catalog 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ApiDocsJsonApi.php \Drupal\Tests\apigee_api_catalog\Functional\ApiDocsJsonApi::verifyAccess()

Verify the account has access when making JSON:API call.

Parameters

\Drupal\Core\Session\AccountInterface $account: The account to send the call.

array $apidocs_expected: An array of the expected API Docs.

string $url: The URL to call.

array $request_options: Any request parameters to pass in such as filter query params.

5 calls to ApiDocsJsonApi::verifyAccess()
ApiDocsJsonApi::testFilterAdminAccess in tests/src/Functional/ApiDocsJsonApi.php
Make sure admin can filter and get results back.
ApiDocsJsonApi::testFilterViewAccessViewPublished in tests/src/Functional/ApiDocsJsonApi.php
View published permission can filter published docs.
ApiDocsJsonApi::testFilterViewPublishedWithViewPublishedAndUnpublishedPermissions in tests/src/Functional/ApiDocsJsonApi.php
View published and unpublished permissions can see published docs.
ApiDocsJsonApi::testFilterViewUnpublishedWithViewPublishedAndUnpublishedPermissions in tests/src/Functional/ApiDocsJsonApi.php
View published and unpublished permissions can see unpublished docs.
ApiDocsJsonApi::testListAdminAccess in tests/src/Functional/ApiDocsJsonApi.php
Test listing API Docs as an admin.

File

tests/src/Functional/ApiDocsJsonApi.php, line 209

Class

ApiDocsJsonApi
Tests listing API Docs using JSON:API.

Namespace

Drupal\Tests\apigee_api_catalog\Functional

Code

protected function verifyAccess(AccountInterface $account, array $apidocs_expected, string $url, array $request_options = []) {

  // Need this header to make calls.
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';

  // Add request options and basic auth header together.
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions($account));
  $client = $this
    ->getSession()
    ->getDriver()
    ->getClient()
    ->getClient();

  // Make the API call.
  $response = $client
    ->request('GET', $url, $request_options);
  $this
    ->assertSame([
    'application/vnd.api+json',
  ], $response
    ->getHeader('Content-Type'));
  $response_document = Json::decode((string) $response
    ->getBody());

  // Get the API Docs from response and create array of names fetched.
  $apidocs_response = $response_document['data'];
  $names = [];
  foreach ($apidocs_response as $apidoc) {
    $names[] = $apidoc['attributes']['name'];
  }

  // Sort expected and actual response results by name for comparison.
  usort($apidocs_expected, function ($a, $b) {
    return strcmp($a
      ->getName(), $b
      ->getName());
  });
  usort($apidocs_response, function ($a, $b) {
    return strcmp($a['attributes']['name'], $b['attributes']['name']);
  });
  for ($i = 0; $i < count($apidocs_response); $i++) {
    $this
      ->assertEqual($apidocs_expected[$i]
      ->getName(), $apidocs_response[$i]['attributes']['name']);
  }

  // Make sure the count is the same.
  $this
    ->assertCount(count($apidocs_expected), $apidocs_response, 'Count of API Docs returned does not match count of expected.');
}