class RefinableCalculatedPermissionsTest

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Session/RefinableCalculatedPermissionsTest.php \Drupal\Tests\Core\Session\RefinableCalculatedPermissionsTest

Tests the RefinableCalculatedPermissions class.

@covers \Drupal\Core\Session\CalculatedPermissionsTrait @covers \Drupal\Core\Session\RefinableCalculatedPermissions @group Session

Hierarchy

Expanded class hierarchy of RefinableCalculatedPermissionsTest

File

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

Namespace

Drupal\Tests\Core\Session
View source
class RefinableCalculatedPermissionsTest extends UnitTestCase {
    
    /**
     * Tests the addition of a calculated permissions item.
     */
    public function testAddItem() : void {
        $calculated_permissions = new RefinableCalculatedPermissions();
        $scope = 'some_scope';
        $item = new CalculatedPermissionsItem([
            'bar',
        ], FALSE, $scope, 'foo');
        $calculated_permissions->addItem($item);
        $this->assertSame($item, $calculated_permissions->getItem($scope, 'foo'), 'Managed to retrieve the calculated permissions item.');
        $item = new CalculatedPermissionsItem([
            'baz',
        ], FALSE, $scope, 'foo');
        $calculated_permissions->addItem($item);
        $this->assertEquals([
            'bar',
            'baz',
        ], $calculated_permissions->getItem($scope, 'foo')
            ->getPermissions(), 'Adding a calculated permissions item that was already in the list merges them.');
        $item = new CalculatedPermissionsItem([
            'cat',
        ], TRUE, $scope, 'foo');
        $calculated_permissions->addItem($item);
        $this->assertEquals([], $calculated_permissions->getItem($scope, 'foo')
            ->getPermissions(), 'Merging in a calculated permissions item with admin rights empties the permissions.');
        $this->assertTrue($calculated_permissions->getItem($scope, 'foo')
            ->isAdmin(), 'Merging in a calculated permissions item with admin rights flags the result as having admin rights.');
    }
    
    /**
     * Tests the overwriting of a calculated permissions item.
     *
     * @depends testAddItem
     */
    public function testAddItemOverwrite() : void {
        $calculated_permissions = new RefinableCalculatedPermissions();
        $scope = 'some_scope';
        $item = new CalculatedPermissionsItem([
            'bar',
        ], FALSE, $scope, 'foo');
        $calculated_permissions->addItem($item);
        $item = new CalculatedPermissionsItem([
            'baz',
        ], FALSE, $scope, 'foo');
        $calculated_permissions->addItem($item, TRUE);
        $this->assertEquals([
            'baz',
        ], $calculated_permissions->getItem($scope, 'foo')
            ->getPermissions(), 'Successfully overwrote an item that was already in the list.');
    }
    
    /**
     * Tests the removal of a calculated permissions item.
     *
     * @depends testAddItem
     */
    public function testRemoveItem() : void {
        $scope = 'some_scope';
        $item = new CalculatedPermissionsItem([
            'bar',
        ], FALSE, $scope, 'foo');
        $calculated_permissions = new RefinableCalculatedPermissions();
        $calculated_permissions->addItem($item);
        $calculated_permissions->removeItem($scope, 'foo');
        $this->assertFalse($calculated_permissions->getItem($scope, 'foo'), 'Could not retrieve a removed item.');
    }
    
    /**
     * Tests the removal of all calculated permissions items.
     *
     * @depends testAddItem
     */
    public function testRemoveItems() : void {
        $scope = 'some_scope';
        $item = new CalculatedPermissionsItem([
            'bar',
        ], FALSE, $scope, 'foo');
        $calculated_permissions = new RefinableCalculatedPermissions();
        $calculated_permissions->addItem($item);
        $calculated_permissions->removeItems();
        $this->assertFalse($calculated_permissions->getItem($scope, 'foo'), 'Could not retrieve a removed item.');
    }
    
    /**
     * Tests the removal of calculated permissions items by scope.
     *
     * @depends testAddItem
     */
    public function testRemoveItemsByScope() : void {
        $scope_a = 'cat';
        $scope_b = 'dog';
        $item_a = new CalculatedPermissionsItem([
            'bar',
        ], FALSE, $scope_a, 'foo');
        $item_b = new CalculatedPermissionsItem([
            'baz',
        ], FALSE, $scope_b, 1);
        $calculated_permissions = (new RefinableCalculatedPermissions())->addItem($item_a)
            ->addItem($item_b);
        $calculated_permissions->removeItemsByScope($scope_a);
        $this->assertFalse($calculated_permissions->getItem($scope_a, 'foo'), 'Could not retrieve a removed item.');
        $this->assertNotFalse($calculated_permissions->getItem($scope_b, 1), 'Untouched scope item was found.');
    }
    
    /**
     * Tests merging in another CalculatedPermissions object.
     *
     * @depends testAddItem
     */
    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');
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
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.
RefinableCalculatedPermissionsTest::testAddItem public function Tests the addition of a calculated permissions item.
RefinableCalculatedPermissionsTest::testAddItemOverwrite public function Tests the overwriting of a calculated permissions item.
RefinableCalculatedPermissionsTest::testMerge public function Tests merging in another CalculatedPermissions object.
RefinableCalculatedPermissionsTest::testRemoveItem public function Tests the removal of a calculated permissions item.
RefinableCalculatedPermissionsTest::testRemoveItems public function Tests the removal of all calculated permissions items.
RefinableCalculatedPermissionsTest::testRemoveItemsByScope public function Tests the removal of calculated permissions items by scope.
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::setUp protected function 358
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.