class EntityBundleAccessCheckTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityBundleAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityBundleAccessCheckTest

Unit test of entity access checking system.

@coversDefaultClass \Drupal\Core\Entity\EntityBundleAccessCheck

@group Access @group Entity

Hierarchy

Expanded class hierarchy of EntityBundleAccessCheckTest

File

core/tests/Drupal/Tests/Core/Entity/EntityBundleAccessCheckTest.php, line 24

Namespace

Drupal\Tests\Core\Entity
View source
class EntityBundleAccessCheckTest extends UnitTestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)
            ->reveal();
        $container = new Container();
        $container->set('cache_contexts_manager', $cache_contexts_manager);
        \Drupal::setContainer($container);
    }
    
    /**
     * Data provider.
     */
    public function getBundleAndAccessResult() {
        return [
            [
                'article',
                'node:article',
                AccessResult::allowed(),
            ],
            [
                'page',
                'node:article',
                AccessResult::neutral('The entity bundle does not match the route _entity_bundles requirement.'),
            ],
            [
                'page',
                'node:article|page',
                AccessResult::allowed(),
            ],
            [
                'article',
                'node:article|page',
                AccessResult::allowed(),
            ],
            [
                'book_page',
                'node:article|page',
                AccessResult::neutral('The entity bundle does not match the route _entity_bundles requirement.'),
            ],
        ];
    }
    
    /**
     * @covers ::access
     *
     * @dataProvider getBundleAndAccessResult
     */
    public function testRouteAccess($bundle, $access_requirement, $access_result) {
        $route = new Route('/foo/{node}', [], [
            '_entity_bundles' => $access_requirement,
        ], [
            'parameters' => [
                'node' => [
                    'type' => 'entity:node',
                ],
            ],
        ]);
        
        /** @var \Drupal\Core\Session\AccountInterface $account */
        $account = $this->prophesize(AccountInterface::class)
            ->reveal();
        
        /** @var \Drupal\node\NodeInterface|\Prophecy\Prophecy\ObjectProphecy $node */
        $node = $this->prophesize(NodeInterface::class);
        $node->bundle()
            ->willReturn($bundle);
        $node->getCacheContexts()
            ->willReturn([]);
        $node->getCacheTags()
            ->willReturn([]);
        $node->getCacheMaxAge()
            ->willReturn(-1);
        $node = $node->reveal();
        
        /** @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
        $route_match = $this->prophesize(RouteMatchInterface::class);
        $route_match->getRawParameters()
            ->willReturn(new ParameterBag([
            'node' => 1,
        ]));
        $route_match->getParameters()
            ->willReturn(new ParameterBag([
            'node' => $node,
        ]));
        $route_match = $route_match->reveal();
        $access_check = new EntityBundleAccessCheck();
        $this->assertEquals($access_result, $access_check->access($route, $route_match, $account));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityBundleAccessCheckTest::getBundleAndAccessResult public function Data provider.
EntityBundleAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
EntityBundleAccessCheckTest::testRouteAccess public function @covers ::access
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.

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