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();
78 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
80 // Don't expect a call to the access control handler when we have a bundle
81 // argument requirement but no bundle is provided.
82 if ($entity_bundle || strpos($requirement, '{') === FALSE) {
83 $access_control_handler = $this->getMock('Drupal\Core\Entity\EntityAccessControlHandlerInterface');
84 $access_control_handler->expects($this->once())
85 ->method('createAccess')
86 ->with($entity_bundle)
87 ->will($this->returnValue($access_result));
89 $entity_manager->expects($this->any())
90 ->method('getAccessControlHandler')
91 ->will($this->returnValue($access_control_handler));
94 $applies_check = new EntityCreateAccessCheck($entity_manager);
96 $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
97 ->disableOriginalConstructor()
99 $route->expects($this->any())
100 ->method('getRequirement')
101 ->with('_entity_create_access')
102 ->will($this->returnValue($requirement));
104 $raw_variables = new ParameterBag();
105 if ($entity_bundle) {
106 $raw_variables->set('bundle_argument', $entity_bundle);
109 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
110 $route_match->expects($this->any())
111 ->method('getRawParameters')
112 ->will($this->returnValue($raw_variables));
114 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
115 $this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account));