function RefinableCalculatedPermissionsTest::testMerge

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Session/RefinableCalculatedPermissionsTest.php \Drupal\Tests\Core\Session\RefinableCalculatedPermissionsTest::testMerge()

Tests merging in another CalculatedPermissions object.

@depends testAddItem

File

core/tests/Drupal/Tests/Core/Session/RefinableCalculatedPermissionsTest.php, line 118

Class

RefinableCalculatedPermissionsTest
Tests the RefinableCalculatedPermissions class.

Namespace

Drupal\Tests\Core\Session

Code

public function testMerge() : void {
    $scope = 'some_scope';
    $cache_context_manager = $this->prophesize(CacheContextsManager::class);
    $cache_context_manager->assertValidTokens(Argument::any())
        ->willReturn(TRUE);
    $container = $this->prophesize(ContainerInterface::class);
    $container->get('cache_contexts_manager')
        ->willReturn($cache_context_manager->reveal());
    \Drupal::setContainer($container->reveal());
    $item_a = new CalculatedPermissionsItem([
        'baz',
    ], FALSE, $scope, 'foo');
    $item_b = new CalculatedPermissionsItem([
        'bob',
        'charlie',
    ], FALSE, $scope, 'foo');
    $item_c = new CalculatedPermissionsItem([], FALSE, $scope, 'bar');
    $item_d = new CalculatedPermissionsItem([], FALSE, $scope, 'baz');
    $calculated_permissions = new RefinableCalculatedPermissions();
    $calculated_permissions->addItem($item_a)
        ->addItem($item_c)
        ->addCacheContexts([
        'foo',
    ])
        ->addCacheTags([
        'foo',
    ]);
    $other = new RefinableCalculatedPermissions();
    $other->addItem($item_b)
        ->addItem($item_d)
        ->addCacheContexts([
        'bar',
    ])
        ->addCacheTags([
        'bar',
    ]);
    $calculated_permissions->merge($other);
    $this->assertNotFalse($calculated_permissions->getItem($scope, 'bar'), 'Original item that did not conflict was kept.');
    $this->assertNotFalse($calculated_permissions->getItem($scope, 'baz'), 'Incoming item that did not conflict was added.');
    $this->assertSame([
        'baz',
        'bob',
        'charlie',
    ], $calculated_permissions->getItem($scope, 'foo')
        ->getPermissions(), 'Permissions were merged properly.');
    $this->assertEqualsCanonicalizing([
        'bar',
        'foo',
    ], $calculated_permissions->getCacheContexts(), 'Cache contexts were merged properly');
    $this->assertEqualsCanonicalizing([
        'bar',
        'foo',
    ], $calculated_permissions->getCacheTags(), 'Cache tags were merged properly');
}

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