3 namespace Drupal\Tests\Core\Entity;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\Context\CacheContextsManager;
7 use Drupal\Core\DependencyInjection\Container;
8 use Drupal\Core\Entity\EntityCreateAccessCheck;
9 use Drupal\Tests\UnitTestCase;
10 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
13 * @coversDefaultClass \Drupal\Core\Entity\EntityCreateAccessCheck
18 class EntityCreateAccessCheckTest extends UnitTestCase {
21 * The mocked entity manager.
23 * @var \PHPUnit_Framework_MockObject_MockObject
25 public $entityManager;
30 protected function setUp() {
33 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
34 $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
35 $cache_contexts_manager->reveal();
36 $container = new Container();
37 $container->set('cache_contexts_manager', $cache_contexts_manager);
38 \Drupal::setContainer($container);
42 * Provides test data for testAccess.
46 public function providerTestAccess() {
51 ['', 'entity_test', $no_access, $no_access],
52 ['', 'entity_test', $access, $access],
53 ['test_entity', 'entity_test:test_entity', $access, $access],
54 ['test_entity', 'entity_test:test_entity', $no_access, $no_access],
55 ['test_entity', 'entity_test:{bundle_argument}', $access, $access],
56 ['test_entity', 'entity_test:{bundle_argument}', $no_access, $no_access],
57 ['', 'entity_test:{bundle_argument}', $no_access, $no_access, FALSE],
58 // When the bundle is not provided, access should be denied even if the
59 // access control handler would allow access.
60 ['', 'entity_test:{bundle_argument}', $access, $no_access, FALSE],
65 * Tests the method for checking access to routes.
67 * @dataProvider providerTestAccess
69 public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) {
71 // Set up the access result objects for allowing or denying access.
72 $access_result = $access ? AccessResult::allowed()->cachePerPermissions() : AccessResult::neutral()->cachePerPermissions();
73 $expected_access_result = $expected ? AccessResult::allowed() : AccessResult::neutral();
74 if ($expect_permission_context) {
75 $expected_access_result->cachePerPermissions();
77 if (!$entity_bundle && !$expect_permission_context) {
78 $expected_access_result->setReason("Could not find '{bundle_argument}' request argument, therefore cannot check create access.");
81 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
83 // Don't expect a call to the access control handler when we have a bundle
84 // argument requirement but no bundle is provided.
85 if ($entity_bundle || strpos($requirement, '{') === FALSE) {
86 $access_control_handler = $this->getMock('Drupal\Core\Entity\EntityAccessControlHandlerInterface');
87 $access_control_handler->expects($this->once())
88 ->method('createAccess')
89 ->with($entity_bundle)
90 ->will($this->returnValue($access_result));
92 $entity_manager->expects($this->any())
93 ->method('getAccessControlHandler')
94 ->will($this->returnValue($access_control_handler));
97 $applies_check = new EntityCreateAccessCheck($entity_manager);
99 $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
100 ->disableOriginalConstructor()
102 $route->expects($this->any())
103 ->method('getRequirement')
104 ->with('_entity_create_access')
105 ->will($this->returnValue($requirement));
107 $raw_variables = new ParameterBag();
108 if ($entity_bundle) {
109 $raw_variables->set('bundle_argument', $entity_bundle);
112 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
113 $route_match->expects($this->any())
114 ->method('getRawParameters')
115 ->will($this->returnValue($raw_variables));
117 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
118 $this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account));