You are here

public function CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities in JSON:API 8

Same name and namespace in other branches
  1. 8.2 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.

5 calls to CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessForPublishableEntities()
BlockContentTest::testCollectionFilterAccess in tests/src/Functional/BlockContentTest.php
CommentTest::testCollectionFilterAccess in tests/src/Functional/CommentTest.php
MediaTest::testCollectionFilterAccess in tests/src/Functional/MediaTest.php
NodeTest::testCollectionFilterAccess in tests/src/Functional/NodeTest.php
TermTest::testCollectionFilterAccess in tests/src/Functional/TermTest.php

File

tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php, line 138

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;
}