59f83af029dec25dae3a53884f3c2644c8a5bcc7
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / EntityCreateAccessCheckTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Entity;
4
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;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Entity\EntityCreateAccessCheck
14  *
15  * @group Access
16  * @group Entity
17  */
18 class EntityCreateAccessCheckTest extends UnitTestCase {
19
20   /**
21    * The mocked entity manager.
22    *
23    * @var \PHPUnit_Framework_MockObject_MockObject
24    */
25   public $entityManager;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
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);
39   }
40
41   /**
42    * Provides test data for testAccess.
43    *
44    * @return array
45    */
46   public function providerTestAccess() {
47     $no_access = FALSE;
48     $access = TRUE;
49
50     return [
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],
61     ];
62   }
63
64   /**
65    * Tests the method for checking access to routes.
66    *
67    * @dataProvider providerTestAccess
68    */
69   public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) {
70
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();
76     }
77     if (!$entity_bundle && !$expect_permission_context) {
78       $expected_access_result->setReason("Could not find '{bundle_argument}' request argument, therefore cannot check create access.");
79     }
80
81     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
82
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));
91
92       $entity_manager->expects($this->any())
93         ->method('getAccessControlHandler')
94         ->will($this->returnValue($access_control_handler));
95     }
96
97     $applies_check = new EntityCreateAccessCheck($entity_manager);
98
99     $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
100       ->disableOriginalConstructor()
101       ->getMock();
102     $route->expects($this->any())
103       ->method('getRequirement')
104       ->with('_entity_create_access')
105       ->will($this->returnValue($requirement));
106
107     $raw_variables = new ParameterBag();
108     if ($entity_bundle) {
109       $raw_variables->set('bundle_argument', $entity_bundle);
110     }
111
112     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
113     $route_match->expects($this->any())
114       ->method('getRawParameters')
115       ->will($this->returnValue($raw_variables));
116
117     $account = $this->getMock('Drupal\Core\Session\AccountInterface');
118     $this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account));
119   }
120
121 }