View source
<?php
namespace Drupal\Tests\content_moderation\Unit;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Drupal\content_moderation\Access\LatestRevisionCheck;
use Drupal\content_moderation\ModerationInformation;
use Drupal\Tests\UnitTestCase;
use Drupal\user\EntityOwnerInterface;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Routing\Route;
class LatestRevisionCheckTest extends UnitTestCase {
protected function setUp() : void {
parent::setUp();
$contexts_manager = $this
->prophesize(CacheContextsManager::class);
$contexts_manager
->assertValidTokens(Argument::any())
->willReturn(TRUE);
$builder = new ContainerBuilder();
$builder
->set('cache_contexts_manager', $contexts_manager
->reveal());
\Drupal::setContainer($builder);
}
public function testLatestAccessPermissions($entity_class, $entity_type, $has_pending_revision, array $account_permissions, $is_owner, $result_class) {
$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);
$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);
}
$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());
$result = $lrc
->access($route
->reveal(), $route_match
->reveal(), $account
->reveal());
$this
->assertInstanceOf($result_class, $result);
}
public function accessSituationProvider() {
return [
[
Node::class,
'node',
TRUE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultAllowed::class,
],
[
Node::class,
'node',
FALSE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
[
Node::class,
'node',
TRUE,
[
'view latest version',
'view own unpublished content',
],
TRUE,
AccessResultAllowed::class,
],
[
Node::class,
'node',
FALSE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
[
Node::class,
'node',
TRUE,
[
'view own unpublished content',
],
TRUE,
AccessResultNeutral::class,
],
[
Node::class,
'node',
TRUE,
[
'view own unpublished content',
],
FALSE,
AccessResultNeutral::class,
],
[
BlockContent::class,
'block_content',
TRUE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultAllowed::class,
],
[
BlockContent::class,
'block_content',
FALSE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
[
BlockContent::class,
'block_content',
TRUE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultNeutral::class,
],
[
BlockContent::class,
'block_content',
FALSE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
];
}
}