function ResourceTestBase::alterExpectedDocumentForRevision

Same name in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::alterExpectedDocumentForRevision()

Alters the expected JSON:API document for revisions.

Default revision tests assume a non-privileged user is performing the GET request and as such the expected document may not include the revision log or other fields that require elevated permissions. This method is an extension point where child classes can modify the expected document to take into account these changes.

Parameters

array $expected_document: Expected document for the default revision.

Return value

array[] Modified document for a revision or user with access to edit a revision AND/OR view revision information.

1 call to ResourceTestBase::alterExpectedDocumentForRevision()
ResourceTestBase::testRevisions in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests individual and collection revisions.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 3560

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function alterExpectedDocumentForRevision(array $expected_document) : array {
    $entity_type = $this->entity
        ->getEntityType();
    if ($entity_type instanceof ContentEntityTypeInterface && ($field_name = $entity_type->getRevisionMetadataKey('revision_log_message'))) {
        // The default entity access control handler assumes that permissions do not
        // change during the lifetime of a request and caches access results.
        // However, we're changing permissions during a test run and need fresh
        // results, so reset the cache.
        \Drupal::entityTypeManager()->getAccessControlHandler($this->entity
            ->getEntityTypeId())
            ->resetCache();
        $revisionLogAccess = $this->entity
            ->access('view revision', $this->account, TRUE)
            ->orIf($this->entity
            ->access('update', $this->account, TRUE));
        if ($revisionLogAccess->isAllowed()) {
            $expected_document['data']['attributes'][$field_name] = NULL;
            return $expected_document;
        }
        unset($expected_document['data']['attributes'][$field_name]);
    }
    return $expected_document;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.