function LatestRevisionCheckTest::testLatestAccessPermissions

Same name in other branches
  1. 8.9.x core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest::testLatestAccessPermissions()
  2. 10 core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest::testLatestAccessPermissions()
  3. 11.x core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest::testLatestAccessPermissions()

Tests the access check of the LatestRevisionCheck service.

@dataProvider accessSituationProvider

Parameters

string $entity_class: The class of the entity to mock.

string $entity_type: The machine name of the entity to mock.

bool $has_pending_revision: Whether this entity should have a pending revision in the system.

array $account_permissions: An array of permissions the account has.

bool $is_owner: Indicates if the user should be the owner of the entity.

string $result_class: The AccessResult class that should result. One of AccessResultAllowed, AccessResultForbidden, AccessResultNeutral.

File

core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php, line 60

Class

LatestRevisionCheckTest
@coversDefaultClass \Drupal\content_moderation\Access\LatestRevisionCheck @group content_moderation

Namespace

Drupal\Tests\content_moderation\Unit

Code

public function testLatestAccessPermissions($entity_class, $entity_type, $has_pending_revision, array $account_permissions, $is_owner, $result_class) {
    
    /** @var \Drupal\Core\Session\AccountInterface $account */
    $account = $this->prophesize(AccountInterface::class);
    $possible_permissions = [
        'view latest version',
        'view any unpublished content',
        'view own unpublished content',
    ];
    foreach ($possible_permissions as $permission) {
        $account->hasPermission($permission)
            ->willReturn(in_array($permission, $account_permissions));
    }
    $account->id()
        ->willReturn(42);
    
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->prophesize($entity_class);
    $entity->getCacheContexts()
        ->willReturn([]);
    $entity->getCacheTags()
        ->willReturn([]);
    $entity->getCacheMaxAge()
        ->willReturn(0);
    if (is_subclass_of($entity_class, EntityOwnerInterface::class)) {
        $entity->getOwnerId()
            ->willReturn($is_owner ? 42 : 3);
    }
    
    /** @var \Drupal\content_moderation\ModerationInformation $mod_info */
    $mod_info = $this->prophesize(ModerationInformation::class);
    $mod_info->hasPendingRevision($entity->reveal())
        ->willReturn($has_pending_revision);
    $route = $this->prophesize(Route::class);
    $route->getOption('_content_moderation_entity_type')
        ->willReturn($entity_type);
    $route_match = $this->prophesize(RouteMatch::class);
    $route_match->getParameter($entity_type)
        ->willReturn($entity->reveal());
    $lrc = new LatestRevisionCheck($mod_info->reveal());
    
    /** @var \Drupal\Core\Access\AccessResult $result */
    $result = $lrc->access($route->reveal(), $route_match->reveal(), $account->reveal());
    $this->assertInstanceOf($result_class, $result);
}

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