You are here

protected function RequestTest::enableRestService in OpenAPI 8

Enables the REST service interface for a specific entity type.

Parameters

string|false $resource_type: The resource type that should get REST API enabled or FALSE to disable all resource types.

string $method: The HTTP method to enable, e.g. GET, POST etc.

string|array $format: (Optional) The serialization format, e.g. hal_json, or a list of formats.

array $auth: (Optional) The list of valid authentication methods.

1 call to RequestTest::enableRestService()
RequestTest::testRequests in tests/src/Functional/RequestTest.php
Tests OpenAPI requests.

File

tests/src/Functional/RequestTest.php, line 413

Class

RequestTest
Tests requests OpenAPI routes.

Namespace

Drupal\Tests\openapi\Functional

Code

protected function enableRestService($resource_type, $method = 'GET', $format = 'json', array $auth = [
  'csrf_token',
]) {
  if ($resource_type) {

    // Enable REST API for this entity type.
    $resource_config_id = str_replace(':', '.', $resource_type);

    // Get entity by id.

    /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
    $resource_config = RestResourceConfig::load($resource_config_id);
    if (!$resource_config) {
      $resource_config = RestResourceConfig::create([
        'id' => $resource_config_id,
        'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
        'configuration' => [],
      ]);
    }
    $configuration = $resource_config
      ->get('configuration');
    if (is_array($format)) {
      for ($i = 0; $i < count($format); $i++) {
        $configuration[$method]['supported_formats'][] = $format[$i];
      }
    }
    else {
      $configuration[$method]['supported_formats'][] = $format;
    }
    foreach ($auth as $auth_provider) {
      $configuration[$method]['supported_auth'][] = $auth_provider;
    }
    $resource_config
      ->set('configuration', $configuration);
    $resource_config
      ->save();
  }
  else {
    foreach (RestResourceConfig::loadMultiple() as $resource_config) {
      $resource_config
        ->delete();
    }
  }
}