function EntityCreateAccessCheckTest::testAccess

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest::testAccess()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest::testAccess()
  3. 10 core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest::testAccess()

Tests the method for checking access to routes.

@dataProvider providerTestAccess

File

core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php, line 73

Class

EntityCreateAccessCheckTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityCreateAccessCheck.php/class/EntityCreateAccessCheck/11.x" title="Defines an access checker for entity creation." class="local">\Drupal\Core\Entity\EntityCreateAccessCheck</a>

Namespace

Drupal\Tests\Core\Entity

Code

public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) : void {
    // Set up the access result objects for allowing or denying access.
    $access_result = $access ? AccessResult::allowed()->cachePerPermissions() : AccessResult::neutral()->cachePerPermissions();
    $expected_access_result = $expected ? AccessResult::allowed() : AccessResult::neutral();
    if ($expect_permission_context) {
        $expected_access_result->cachePerPermissions();
    }
    if (!$entity_bundle && !$expect_permission_context) {
        $expected_access_result->setReason("Could not find '{bundle_argument}' request argument, therefore cannot check create access.");
    }
    // Don't expect a call to the access control handler when we have a bundle
    // argument requirement but no bundle is provided.
    if ($entity_bundle || !str_contains($requirement, '{')) {
        $access_control_handler = $this->createMock('Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
        $access_control_handler->expects($this->once())
            ->method('createAccess')
            ->with($entity_bundle)
            ->willReturn($access_result);
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getAccessControlHandler')
            ->willReturn($access_control_handler);
    }
    $applies_check = new EntityCreateAccessCheck($this->entityTypeManager);
    $route = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')
        ->disableOriginalConstructor()
        ->getMock();
    $route->expects($this->any())
        ->method('getRequirement')
        ->with('_entity_create_access')
        ->willReturn($requirement);
    $raw_variables = new InputBag();
    if ($entity_bundle) {
        $raw_variables->set('bundle_argument', $entity_bundle);
    }
    $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $route_match->expects($this->any())
        ->method('getRawParameters')
        ->willReturn($raw_variables);
    $account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
    $this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account));
}

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