You are here

protected function NodeJsonOutput::provisionResource in Metatag 8

Provisions the REST resource under test.

Parameters

string $entity_type: The entity type to be enabled; defaults to 'node'.

array $formats: The allowed formats for this resource; defaults to ['json'].

array $authentication: The allowed authentication providers for this resource; defaults to ['basic_auth'].

1 call to NodeJsonOutput::provisionResource()
NodeJsonOutput::testNode in tests/src/Functional/NodeJsonOutput.php
Create an entity, view its JSON output, confirm Metatag data exists.

File

tests/src/Functional/NodeJsonOutput.php, line 95

Class

NodeJsonOutput
Verify that the JSON output from core works as intended.

Namespace

Drupal\Tests\metatag\Functional

Code

protected function provisionResource($entity_type = 'node', array $formats = [], array $authentication = []) {
  $this->resourceConfigStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('rest_resource_config');

  // Defaults.
  if (empty($formats)) {
    $formats[] = 'json';
  }
  if (empty($authentication)) {
    $authentication[] = 'basic_auth';
  }
  $this->resourceConfigStorage
    ->create([
    'id' => 'entity.' . $entity_type,
    'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
    'configuration' => [
      'methods' => [
        'GET',
        'POST',
        'PATCH',
        'DELETE',
      ],
      'formats' => $formats,
      'authentication' => $authentication,
    ],
    'status' => TRUE,
  ])
    ->save();

  // Ensure that the cache tags invalidator has its internal values reset.
  // Otherwise the http_response cache tag invalidation won't work.
  // Clear the tag cache.
  \Drupal::service('cache_tags.invalidator')
    ->resetChecksums();
  foreach (Cache::getBins() as $backend) {
    if (is_callable([
      $backend,
      'reset',
    ])) {
      $backend
        ->reset();
    }
  }
  $this->container
    ->get('config.factory')
    ->reset();
  $this->container
    ->get('state')
    ->resetCache();

  // Tests using this base class may trigger route rebuilds due to changes to
  // RestResourceConfig entities or 'rest.settings'. Ensure the test generates
  // routes using an up-to-date router.
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
}