function CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php \Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities()
  2. 10 core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php \Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities()
  3. 11.x core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php \Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities()

Implements ::testCollectionFilterAccess() for permission + status access.

Parameters

string $label_field_name: The entity type's label field name.

string $view_permission: The entity type's permission that grants 'view' access (for published entities of this type).

string $admin_permission: The entity type's permission that grants 'view' access (for unpublished entities of this type).

Return value

\Drupal\Core\Entity\EntityInterface The referencing entity.

4 calls to CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities()
BlockContentTest::testCollectionFilterAccess in core/modules/jsonapi/tests/src/Functional/BlockContentTest.php
CommentTest::testCollectionFilterAccess in core/modules/jsonapi/tests/src/Functional/CommentTest.php
MediaTest::testCollectionFilterAccess in core/modules/jsonapi/tests/src/Functional/MediaTest.php
NodeTest::testCollectionFilterAccess in core/modules/jsonapi/tests/src/Functional/NodeTest.php

File

core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php, line 140

Class

CommonCollectionFilterAccessTestPatternsTrait
Provides common filter access control tests.

Namespace

Drupal\Tests\jsonapi\Traits

Code

public function doTestCollectionFilterAccessForPublishableEntities($label_field_name, $view_permission, $admin_permission) {
    assert($this->entity instanceof EntityPublishedInterface);
    $this->assertTrue($this->entity
        ->isPublished());
    $referencing_entity = $this->doTestCollectionFilterAccessBasedOnPermissions($label_field_name, $view_permission);
    $collection_url = Url::fromRoute('jsonapi.entity_test--bar.collection');
    $collection_filter_url = $collection_url->setOption('query', [
        "filter[spotlight.{$label_field_name}]" => $this->entity
            ->label(),
    ]);
    $request_options = [];
    $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
    $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
    // Unpublish.
    $this->entity
        ->setUnpublished()
        ->save();
    // ?filter[spotlight.LABEL]: no result because the test entity is
    // unpublished. This proves that appropriate cache tags are bubbled.
    $response = $this->request('GET', $collection_filter_url, $request_options);
    $doc = Json::decode((string) $response->getBody());
    $this->assertCount(0, $doc['data']);
    // Grant admin permission.
    $this->grantPermissionsToTestedRole([
        $admin_permission,
    ]);
    // ?filter[spotlight.LABEL]: 1 result despite the test entity being
    // unpublished, thanks to the admin permission. This proves that the
    // appropriate cache contexts are bubbled.
    $response = $this->request('GET', $collection_filter_url, $request_options);
    $doc = Json::decode((string) $response->getBody());
    $this->assertCount(1, $doc['data']);
    $this->assertSame($referencing_entity->uuid(), $doc['data'][0]['id']);
    return $referencing_entity;
}

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