class LatestRevisionCheckTest
Same name in other branches
- 9 core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest
- 8.9.x core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest
- 11.x core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest
@coversDefaultClass \Drupal\content_moderation\Access\LatestRevisionCheck @group content_moderation
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\content_moderation\Unit\LatestRevisionCheckTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LatestRevisionCheckTest
File
-
core/
modules/ content_moderation/ tests/ src/ Unit/ LatestRevisionCheckTest.php, line 27
Namespace
Drupal\Tests\content_moderation\UnitView source
class LatestRevisionCheckTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Initialize Drupal container since the cache context manager is needed.
$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);
}
/**
* Tests the access check of the LatestRevisionCheck service.
*
* @param string $entity_class
* The class of the entity to mock.
* @param string $entity_type
* The machine name of the entity to mock.
* @param bool $has_pending_revision
* Whether this entity should have a pending revision in the system.
* @param array $account_permissions
* An array of permissions the account has.
* @param bool $is_owner
* Indicates if the user should be the owner of the entity.
* @param string $result_class
* The AccessResult class that should result. One of AccessResultAllowed,
* AccessResultForbidden, AccessResultNeutral.
*
* @dataProvider accessSituationProvider
*/
public function testLatestAccessPermissions($entity_class, $entity_type, $has_pending_revision, array $account_permissions, $is_owner, $result_class) : void {
/** @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);
}
/**
* Data provider for testLastAccessPermissions().
*/
public static function accessSituationProvider() {
return [
// Node with global permissions and latest version.
[
Node::class,
'node',
TRUE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultAllowed::class,
],
// Node with global permissions and no latest version.
[
Node::class,
'node',
FALSE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
// Node with own content permissions and latest version.
[
Node::class,
'node',
TRUE,
[
'view latest version',
'view own unpublished content',
],
TRUE,
AccessResultAllowed::class,
],
// Node with own content permissions and no latest version.
[
Node::class,
'node',
FALSE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
// Node with own content permissions and latest version, but no perms to
// view latest version.
[
Node::class,
'node',
TRUE,
[
'view own unpublished content',
],
TRUE,
AccessResultNeutral::class,
],
// Node with own content permissions and no latest version, but no perms
// to view latest version.
[
Node::class,
'node',
TRUE,
[
'view own unpublished content',
],
FALSE,
AccessResultNeutral::class,
],
// Block with pending revision, and permissions to view any.
[
BlockContent::class,
'block_content',
TRUE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultAllowed::class,
],
// Block with no pending revision.
[
BlockContent::class,
'block_content',
FALSE,
[
'view latest version',
'view any unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
// Block with pending revision, but no permission to view any.
[
BlockContent::class,
'block_content',
TRUE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultNeutral::class,
],
// Block with no pending revision.
[
BlockContent::class,
'block_content',
FALSE,
[
'view latest version',
'view own unpublished content',
],
FALSE,
AccessResultForbidden::class,
],
];
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
LatestRevisionCheckTest::accessSituationProvider | public static | function | Data provider for testLastAccessPermissions(). | |||
LatestRevisionCheckTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
LatestRevisionCheckTest::testLatestAccessPermissions | public | function | Tests the access check of the LatestRevisionCheck service. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.