You are here

public function EntityCreateAccessCheckTest::testAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 74
Contains \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest.

Class

EntityCreateAccessCheckTest
@coversDefaultClass \Drupal\Core\Entity\EntityCreateAccessCheck

Namespace

Drupal\Tests\Core\Entity

Code

public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) {

  // 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();
  }
  $entity_manager = $this
    ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');

  // 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 || strpos($requirement, '{') === FALSE) {
    $access_control_handler = $this
      ->getMock('Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
    $access_control_handler
      ->expects($this
      ->once())
      ->method('createAccess')
      ->with($entity_bundle)
      ->will($this
      ->returnValue($access_result));
    $entity_manager
      ->expects($this
      ->any())
      ->method('getAccessControlHandler')
      ->will($this
      ->returnValue($access_control_handler));
  }
  $applies_check = new EntityCreateAccessCheck($entity_manager);
  $route = $this
    ->getMockBuilder('Symfony\\Component\\Routing\\Route')
    ->disableOriginalConstructor()
    ->getMock();
  $route
    ->expects($this
    ->any())
    ->method('getRequirement')
    ->with('_entity_create_access')
    ->will($this
    ->returnValue($requirement));
  $raw_variables = new ParameterBag();
  if ($entity_bundle) {
    $raw_variables
      ->set('bundle_argument', $entity_bundle);
  }
  $route_match = $this
    ->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
  $route_match
    ->expects($this
    ->any())
    ->method('getRawParameters')
    ->will($this
    ->returnValue($raw_variables));
  $account = $this
    ->getMock('Drupal\\Core\\Session\\AccountInterface');
  $this
    ->assertEquals($expected_access_result, $applies_check
    ->access($route, $route_match, $account));
}