Skip to content
Snippets Groups Projects
Commit a50bb3f9 authored by Kristiaan Van den Eynde's avatar Kristiaan Van den Eynde
Browse files

Issue #3450357 by kristiaanvandeneynde: Create a converter to Access Policy API

parent b223cf30
No related branches found
No related tags found
1 merge request!9Start off with converter methods on the CalculatedPermissions value objects
Pipeline #185127 passed
Showing
with 513 additions and 10 deletions
services:
access_policy.flexible_permissions:
class: 'Drupal\flexible_permissions\AccessPolicy'
arguments: ['@flexible_permissions.chain_calculator']
tags:
- { name: 'access_policy' }
flexible_permissions.chain_calculator:
class: 'Drupal\flexible_permissions\ChainPermissionCalculator'
arguments: ['@variation_cache.flexible_permissions', '@variation_cache.flexible_permissions_memory', '@cache.flexible_permissions_memory', '@account_switcher']
......
<?php
declare(strict_types=1);
namespace Drupal\flexible_permissions;
use Drupal\Core\Session\AccessPolicyBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\RefinableCalculatedPermissionsInterface as CoreRefinableCalculatedPermissionsInterface;
/**
* Converts FP policies into core ones.
*/
class AccessPolicy extends AccessPolicyBase {
public function __construct(protected ChainPermissionCalculatorInterface $chainCalculator) {}
/**
* The last checked scope.
*
* @var string
*/
protected string $lastCheckedScope;
/**
* {@inheritdoc}
*/
public function applies(string $scope): bool {
$this->lastCheckedScope = $scope;
return TRUE;
}
/**
* {@inheritdoc}
*/
public function calculatePermissions(AccountInterface $account, string $scope): CoreRefinableCalculatedPermissionsInterface {
$calculated_permissions = RefinableCalculatedPermissions::fromCore(parent::calculatePermissions($account, $scope));
foreach ($this->chainCalculator->getCalculators() as $calculator) {
$calculated_permissions->merge($calculator->calculatePermissions($account, $scope));
}
return $calculated_permissions->toCore();
}
/**
* {@inheritdoc}
*/
public function alterPermissions(AccountInterface $account, string $scope, CoreRefinableCalculatedPermissionsInterface $calculated_permissions): void {
parent::alterPermissions($account, $scope, $calculated_permissions);
$converted = RefinableCalculatedPermissions::fromCore($calculated_permissions);
$converted->disableBuildMode();
foreach ($this->chainCalculator->getCalculators() as $calculator) {
if ($calculator instanceof PermissionCalculatorAlterInterfaceV2) {
$calculator->alterPermissions($account, $scope, $converted);
}
elseif ($calculator instanceof PermissionCalculatorAlterInterface) {
$calculator->alterPermissions($converted);
}
}
$calculated_permissions->removeItems();
$calculated_permissions->merge($converted->toCore());
}
/**
* {@inheritdoc}
*/
public function getPersistentCacheContexts(): array {
$cache_context_sets = [];
foreach ($this->chainCalculator->getCalculators() as $calculator) {
$cache_context_sets[] = $calculator->getPersistentCacheContexts($this->lastCheckedScope);
}
return array_merge(...$cache_context_sets);
}
}
......@@ -3,6 +3,8 @@
namespace Drupal\flexible_permissions;
use Drupal\Core\Cache\CacheableDependencyTrait;
use Drupal\Core\Session\CalculatedPermissions as CoreCalculatedPermissions;
use Drupal\Core\Session\CalculatedPermissionsInterface as CoreCalculatedPermissionsInterface;
/**
* Represents a calculated set of permissions with cacheable metadata.
......@@ -33,4 +35,20 @@ class CalculatedPermissions implements CalculatedPermissionsInterface {
$this->cacheContexts = [];
}
/**
* {@inheritdoc}
*/
public function toCore(): CoreCalculatedPermissionsInterface {
$converted = (new RefinableCalculatedPermissions())->merge($this)->toCore();
return new CoreCalculatedPermissions($converted);
}
/**
* {@inheritdoc}
*/
public static function fromCore(CoreCalculatedPermissionsInterface $core_object): self {
$converted = RefinableCalculatedPermissions::fromCore($core_object);
return new self($converted);
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\flexible_permissions;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Session\CalculatedPermissionsInterface as CoreCalculatedPermissionsInterface;
/**
* Defines the calculated permissions interface.
......@@ -49,4 +50,23 @@ interface CalculatedPermissionsInterface extends CacheableDependencyInterface {
*/
public function getItemsByScope($scope);
/**
* Converts an FP version into an Access Policy API version.
*
* @return \Drupal\Core\Session\CalculatedPermissionsInterface
* The Access Policy API counterpart.
*/
public function toCore(): CoreCalculatedPermissionsInterface;
/**
* Converts an Access Policy API version into an FP version.
*
* @param \Drupal\Core\Session\CalculatedPermissionsInterface $core_object
* The Drupal core version of this object.
*
* @return self
* The Flexible Permissions counterpart.
*/
public static function fromCore(CoreCalculatedPermissionsInterface $core_object): self;
}
......@@ -2,6 +2,9 @@
namespace Drupal\flexible_permissions;
use Drupal\Core\Session\CalculatedPermissionsItem as CoreCalculatedPermissionsItem;
use Drupal\Core\Session\CalculatedPermissionsItemInterface as CoreCalculatedPermissionsItemInterface;
/**
* Represents a single entry for the calculated permissions.
*
......@@ -91,4 +94,28 @@ class CalculatedPermissionsItem implements CalculatedPermissionsItemInterface {
return $this->isAdmin() || in_array($permission, $this->permissions, TRUE);
}
/**
* {@inheritdoc}
*/
public function toCore(): CoreCalculatedPermissionsItemInterface {
return new CoreCalculatedPermissionsItem(
$this->getPermissions(),
$this->isAdmin(),
$this->getScope(),
$this->getIdentifier()
);
}
/**
* {@inheritdoc}
*/
public static function fromCore(CoreCalculatedPermissionsItemInterface $core_object): self {
return new self(
$core_object->getScope(),
$core_object->getIdentifier(),
$core_object->getPermissions(),
$core_object->isAdmin()
);
}
}
......@@ -2,6 +2,8 @@
namespace Drupal\flexible_permissions;
use Drupal\Core\Session\CalculatedPermissionsItemInterface as CoreCalculatedPermissionsItemInterface;
/**
* Defines the calculated permissions item interface.
*/
......@@ -52,4 +54,23 @@ interface CalculatedPermissionsItemInterface {
*/
public function hasPermission($permission);
/**
* Converts an FP version into an Access Policy API version.
*
* @return \Drupal\Core\Session\CalculatedPermissionsItemInterface
* The Access Policy API counterpart.
*/
public function toCore(): CoreCalculatedPermissionsItemInterface;
/**
* Converts an Access Policy API version into an FP version.
*
* @param \Drupal\Core\Session\CalculatedPermissionsItemInterface $core_object
* The Drupal core version of this object.
*
* @return self
* The Flexible Permissions counterpart.
*/
public static function fromCore(CoreCalculatedPermissionsItemInterface $core_object): self;
}
......@@ -3,6 +3,9 @@
namespace Drupal\flexible_permissions;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Session\CalculatedPermissionsInterface as CoreCalculatedPermissionsInterface;
use Drupal\Core\Session\RefinableCalculatedPermissions as CoreRefinableCalculatedPermissions;
use Drupal\Core\Session\RefinableCalculatedPermissionsInterface as CoreRefinableCalculatedPermissionsInterface;
/**
* Represents a calculated set of permissions with cacheable metadata.
......@@ -118,4 +121,26 @@ class RefinableCalculatedPermissions implements RefinableCalculatedPermissionsIn
return new CalculatedPermissionsItem($a->getScope(), $a->getIdentifier(), $permissions, $is_admin);
}
/**
* {@inheritdoc}
*/
public function toCore(): CoreRefinableCalculatedPermissionsInterface {
$converted = (new CoreRefinableCalculatedPermissions())->addCacheableDependency($this);
foreach ($this->getItems() as $item) {
$converted->addItem($item->toCore());
}
return $converted;
}
/**
* {@inheritdoc}
*/
public static function fromCore(CoreCalculatedPermissionsInterface $core_object): self {
$converted = (new self())->addCacheableDependency($core_object);
foreach ($core_object->getItems() as $item) {
$converted->addItem(CalculatedPermissionsItem::fromCore($item));
}
return $converted;
}
}
name: 'Flexible Permissions test'
description: 'Support module for Flexible Permissions tests.'
package: 'Testing'
type: 'module'
core_version_requirement: ^10.3
services:
access_policy.flexible_permissions_test:
class: 'Drupal\flexible_permissions_test\NewAccessPolicy'
tags:
- { name: 'access_policy' }
old_access_policy.flexible_permissions_test:
class: 'Drupal\flexible_permissions_test\OldAccessPolicy'
tags:
- { name: 'flexible_permission_calculator' }
<?php
namespace Drupal\flexible_permissions_test;
use Drupal\Core\Session\AccessPolicyBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\CalculatedPermissionsItem;
use Drupal\Core\Session\RefinableCalculatedPermissionsInterface;
/**
* Access Policy API policy.
*/
class NewAccessPolicy extends AccessPolicyBase {
/**
* {@inheritdoc}
*/
public function applies(string $scope): bool {
return $scope === 'flexible_permissions_test';
}
/**
* {@inheritdoc}
*/
public function calculatePermissions(AccountInterface $account, string $scope): RefinableCalculatedPermissionsInterface {
return parent::calculatePermissions($account, $scope)->addItem(new CalculatedPermissionsItem(
['foo', 'bar'],
FALSE,
'flexible_permissions_test',
'flexible_permissions_test',
));
}
/**
* {@inheritdoc}
*/
public function alterPermissions(AccountInterface $account, string $scope, RefinableCalculatedPermissionsInterface $calculated_permissions): void {
parent::alterPermissions($account, $scope, $calculated_permissions);
$item = $calculated_permissions->getItem('flexible_permissions_test', 'flexible_permissions_test');
$calculated_permissions->removeItem('flexible_permissions_test', 'flexible_permissions_test');
$calculated_permissions->addItem(new CalculatedPermissionsItem(
array_diff($item->getPermissions(), ['baz']),
FALSE,
'flexible_permissions_test',
'flexible_permissions_test',
));
}
}
<?php
namespace Drupal\flexible_permissions_test;
use Drupal\Core\Session\AccountInterface;
use Drupal\flexible_permissions\CalculatedPermissionsItem;
use Drupal\flexible_permissions\PermissionCalculatorAlterInterfaceV2;
use Drupal\flexible_permissions\PermissionCalculatorInterface;
use Drupal\flexible_permissions\RefinableCalculatedPermissions;
use Drupal\flexible_permissions\RefinableCalculatedPermissionsInterface;
/**
* Flexible Permissions policy.
*/
class OldAccessPolicy implements PermissionCalculatorInterface, PermissionCalculatorAlterInterfaceV2 {
/**
* {@inheritdoc}
*/
public function calculatePermissions(AccountInterface $account, $scope) {
return (new RefinableCalculatedPermissions())->addItem(new CalculatedPermissionsItem(
'flexible_permissions_test',
'flexible_permissions_test',
['baz', 'foobar']
));
}
/**
* {@inheritdoc}
*/
public function alterPermissions(AccountInterface $account, $scope, RefinableCalculatedPermissionsInterface $calculated_permissions) {
$item = $calculated_permissions->getItem('flexible_permissions_test', 'flexible_permissions_test');
$calculated_permissions->removeItem('flexible_permissions_test', 'flexible_permissions_test');
$calculated_permissions->addItem(new CalculatedPermissionsItem(
'flexible_permissions_test',
'flexible_permissions_test',
array_diff($item->getPermissions(), ['bar'])
));
}
/**
* {@inheritdoc}
*/
public function getPersistentCacheContexts($scope) {
return [];
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\flexible_permissions\Kernel;
use Drupal\Core\Session\AccessPolicyProcessorInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the behavior of the convertor access policy.
*
* @covers \Drupal\flexible_permissions\AccessPolicy
* @group flexible_permissions
*/
class AccessPolicyTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['flexible_permissions', 'flexible_permissions_test'];
/**
* Tests that both FP and core access policies work alongside each other.
*/
public function testAccessPolicy(): void {
$processor = $this->container->get('access_policy_processor');
assert($processor instanceof AccessPolicyProcessorInterface);
$account = \Drupal::currentUser();
$items = $processor->processAccessPolicies($account, 'flexible_permissions_test')->getItems();
$this->assertEqualsCanonicalizing(['foo', 'foobar'], reset($items)->getPermissions());
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\flexible_permissions\Unit;
use Drupal\Core\Session\CalculatedPermissionsItem as CoreCalculatedPermissionsItem;
use Drupal\flexible_permissions\CalculatedPermissionsItem;
use Drupal\Tests\UnitTestCase;
......@@ -22,7 +25,7 @@ class CalculatedPermissionsItemTest extends UnitTestCase {
* @covers ::getPermissions
* @covers ::isAdmin
*/
public function testConstructor() {
public function testConstructor(): void {
$scope = 'some_scope';
$item = new CalculatedPermissionsItem($scope, 'foo', ['bar', 'baz', 'bar'], FALSE);
......@@ -42,7 +45,7 @@ class CalculatedPermissionsItemTest extends UnitTestCase {
* @covers ::hasPermission
* @depends testConstructor
*/
public function testHasPermission() {
public function testHasPermission(): void {
$item = new CalculatedPermissionsItem('some_scope', 'foo', ['bar'], FALSE);
$this->assertFalse($item->hasPermission('baz'), 'Missing permission was not found.');
$this->assertTrue($item->hasPermission('bar'), 'Existing permission was found.');
......@@ -54,10 +57,39 @@ class CalculatedPermissionsItemTest extends UnitTestCase {
* @covers ::hasPermission
* @depends testConstructor
*/
public function testHasPermissionWithAdminFlag() {
public function testHasPermissionWithAdminFlag(): void {
$item = new CalculatedPermissionsItem('some_scope', 'foo', ['bar'], TRUE);
$this->assertTrue($item->hasPermission('baz'), 'Missing permission was found.');
$this->assertTrue($item->hasPermission('bar'), 'Existing permission was found.');
}
/**
* Tests the conversion to Access Policy API.
*
* @covers ::toCore
* @depends testConstructor
*/
public function testToCore(): void {
$item = new CalculatedPermissionsItem('some_scope', 'foo', ['bar'], TRUE);
$converted = $item->toCore();
$this->assertSame($item->getScope(), $converted->getScope());
$this->assertSame($item->getIdentifier(), $converted->getIdentifier());
$this->assertSame($item->getPermissions(), $converted->getPermissions());
$this->assertSame($item->isAdmin(), $converted->isAdmin());
}
/**
* Tests the conversion from the Access Policy API.
*
* @covers ::fromCore
*/
public function testFromCore(): void {
$item = new CoreCalculatedPermissionsItem(['bar'], TRUE, 'some_scope', 'foo');
$converted = CalculatedPermissionsItem::fromCore($item);
$this->assertSame($item->getScope(), $converted->getScope());
$this->assertSame($item->getIdentifier(), $converted->getIdentifier());
$this->assertSame($item->getPermissions(), $converted->getPermissions());
$this->assertSame($item->isAdmin(), $converted->isAdmin());
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\flexible_permissions\Unit;
use Drupal\Core\Session\CalculatedPermissions as CoreCalculatedPermissions;
use Drupal\Core\Session\CalculatedPermissionsInterface as CoreCalculatedPermissionsInterface;
use Drupal\Core\Session\CalculatedPermissionsItem as CoreCalculatedPermissionsItem;
use Drupal\flexible_permissions\CalculatedPermissions;
use Drupal\flexible_permissions\CalculatedPermissionsInterface;
use Drupal\flexible_permissions\CalculatedPermissionsItem;
......@@ -24,7 +29,7 @@ class CalculatedPermissionsTest extends UnitTestCase {
* @covers ::getScopes
* @covers ::getItemsByScope
*/
public function testConstructor() {
public function testConstructor(): void {
$item_a = new CalculatedPermissionsItem('scope_a', 'foo', ['baz']);
$item_b = new CalculatedPermissionsItem('scope_b', 1, ['bob', 'charlie']);
......@@ -46,4 +51,61 @@ class CalculatedPermissionsTest extends UnitTestCase {
$this->assertSame(1986, $calculated_permissions->getCacheMaxAge(), 'Successfully inherited cache max-age.');
}
/**
* Tests the conversion to Access Policy API.
*
* @covers ::toCore
* @depends testConstructor
*/
public function testToCore(): void {
$item_old = new CalculatedPermissionsItem('scope', 'foo', ['baz']);
$calculated_permissions = $this->prophesize(CalculatedPermissionsInterface::class);
$calculated_permissions->getItems()->willReturn([$item_old]);
$calculated_permissions->getCacheTags()->willReturn(['24']);
$calculated_permissions->getCacheContexts()->willReturn(['Oct']);
$calculated_permissions->getCacheMaxAge()->willReturn(1986);
$calculated_permissions = new CalculatedPermissions($calculated_permissions->reveal());
$converted = $calculated_permissions->toCore();
$this->assertSame($calculated_permissions->getCacheTags(), $converted->getCacheTags());
$this->assertSame($calculated_permissions->getCacheContexts(), $converted->getCacheContexts());
$this->assertSame($calculated_permissions->getCacheMaxAge(), $converted->getCacheMaxAge());
$item_new = $converted->getItem('scope', 'foo');
$this->assertInstanceOf(CoreCalculatedPermissionsItem::class, $item_new);
$this->assertSame($item_old->getScope(), $item_new->getScope());
$this->assertSame($item_old->getIdentifier(), $item_new->getIdentifier());
$this->assertSame($item_old->getPermissions(), $item_new->getPermissions());
$this->assertSame($item_old->isAdmin(), $item_new->isAdmin());
}
/**
* Tests the conversion from the Access Policy API.
*
* @covers ::fromCore
*/
public function testFromCore(): void {
$item_old = new CoreCalculatedPermissionsItem(['baz'], FALSE, 'scope', 'foo',);
$calculated_permissions = $this->prophesize(CoreCalculatedPermissionsInterface::class);
$calculated_permissions->getItems()->willReturn([$item_old]);
$calculated_permissions->getCacheTags()->willReturn(['24']);
$calculated_permissions->getCacheContexts()->willReturn(['Oct']);
$calculated_permissions->getCacheMaxAge()->willReturn(1986);
$calculated_permissions = new CoreCalculatedPermissions($calculated_permissions->reveal());
$converted = CalculatedPermissions::fromCore($calculated_permissions);
$this->assertSame($calculated_permissions->getCacheTags(), $converted->getCacheTags());
$this->assertSame($calculated_permissions->getCacheContexts(), $converted->getCacheContexts());
$this->assertSame($calculated_permissions->getCacheMaxAge(), $converted->getCacheMaxAge());
$item_new = $converted->getItem('scope', 'foo');
$this->assertInstanceOf(CalculatedPermissionsItem::class, $item_new);
$this->assertSame($item_old->getScope(), $item_new->getScope());
$this->assertSame($item_old->getIdentifier(), $item_new->getIdentifier());
$this->assertSame($item_old->getPermissions(), $item_new->getPermissions());
$this->assertSame($item_old->isAdmin(), $item_new->isAdmin());
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\flexible_permissions\Unit;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Session\CalculatedPermissionsItem as CoreCalculatedPermissionsItem;
use Drupal\Core\Session\RefinableCalculatedPermissions as CoreRefinableCalculatedPermissions;
use Drupal\flexible_permissions\CalculatedPermissionsItem;
use Drupal\flexible_permissions\RefinableCalculatedPermissions;
use Drupal\Tests\UnitTestCase;
......@@ -23,7 +28,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::addItem
* @covers ::getItem
*/
public function testAddItem() {
public function testAddItem(): void {
$calculated_permissions = new RefinableCalculatedPermissions();
$scope = 'some_scope';
......@@ -48,7 +53,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::getItem
* @depends testAddItem
*/
public function testAddItemOverwrite() {
public function testAddItemOverwrite(): void {
$calculated_permissions = new RefinableCalculatedPermissions();
$scope = 'some_scope';
......@@ -70,7 +75,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::removeItem
* @depends testAddItem
*/
public function testRemoveItem() {
public function testRemoveItem(): void {
$scope = 'some_scope';
$item = new CalculatedPermissionsItem($scope, 'foo', ['bar']);
......@@ -91,7 +96,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::removeItems
* @depends testAddItem
*/
public function testRemoveItems() {
public function testRemoveItems(): void {
$scope = 'some_scope';
$item = new CalculatedPermissionsItem($scope, 'foo', ['bar']);
......@@ -112,7 +117,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::removeItemsByScope
* @depends testAddItem
*/
public function testRemoveItemsByScope() {
public function testRemoveItemsByScope(): void {
$scope_a = 'cat';
$scope_b = 'dog';
......@@ -138,7 +143,7 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
* @covers ::merge
* @depends testAddItem
*/
public function testMerge() {
public function testMerge(): void {
$scope = 'some_scope';
$cache_context_manager = $this->prophesize(CacheContextsManager::class);
......@@ -175,4 +180,70 @@ class RefinableCalculatedPermissionsTest extends UnitTestCase {
$this->assertEqualsCanonicalizing(['bar', 'foo'], $calculated_permissions->getCacheTags(), 'Cache tags were merged properly');
}
/**
* Tests the conversion to Access Policy API.
*
* @covers ::toCore
*/
public function testToCore(): void {
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens(Argument::any())->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
\Drupal::setContainer($container);
$item_old = new CalculatedPermissionsItem('scope', 'foo', ['baz']);
$calculated_permissions = (new RefinableCalculatedPermissions())
->addItem($item_old)
->addCacheTags(['24'])
->addCacheContexts(['Oct'])
->mergeCacheMaxAge(1986);
$converted = $calculated_permissions->toCore();
$this->assertSame($calculated_permissions->getCacheTags(), $converted->getCacheTags());
$this->assertSame($calculated_permissions->getCacheContexts(), $converted->getCacheContexts());
$this->assertSame($calculated_permissions->getCacheMaxAge(), $converted->getCacheMaxAge());
$item_new = $converted->getItem('scope', 'foo');
$this->assertInstanceOf(CoreCalculatedPermissionsItem::class, $item_new);
$this->assertSame($item_old->getScope(), $item_new->getScope());
$this->assertSame($item_old->getIdentifier(), $item_new->getIdentifier());
$this->assertSame($item_old->getPermissions(), $item_new->getPermissions());
$this->assertSame($item_old->isAdmin(), $item_new->isAdmin());
}
/**
* Tests the conversion from the Access Policy API.
*
* @covers ::fromCore
*/
public function testFromCore(): void {
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens(Argument::any())->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
\Drupal::setContainer($container);
$item_old = new CoreCalculatedPermissionsItem(['baz'], FALSE, 'scope', 'foo',);
$calculated_permissions = (new CoreRefinableCalculatedPermissions())
->addItem($item_old)
->addCacheTags(['24'])
->addCacheContexts(['Oct'])
->mergeCacheMaxAge(1986);
$converted = RefinableCalculatedPermissions::fromCore($calculated_permissions);
$this->assertSame($calculated_permissions->getCacheTags(), $converted->getCacheTags());
$this->assertSame($calculated_permissions->getCacheContexts(), $converted->getCacheContexts());
$this->assertSame($calculated_permissions->getCacheMaxAge(), $converted->getCacheMaxAge());
$item_new = $converted->getItem('scope', 'foo');
$this->assertInstanceOf(CalculatedPermissionsItem::class, $item_new);
$this->assertSame($item_old->getScope(), $item_new->getScope());
$this->assertSame($item_old->getIdentifier(), $item_new->getIdentifier());
$this->assertSame($item_old->getPermissions(), $item_new->getPermissions());
$this->assertSame($item_old->isAdmin(), $item_new->isAdmin());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment