AccessManagerTest.php

Contains \Drupal\Tests\Core\Access\AccessManagerTest.

Namespace

Drupal\Tests\Core\Access

File

drupal/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Access\AccessManagerTest.
  5. */
  6. namespace Drupal\Tests\Core\Access;
  7. use Drupal\Core\Access\AccessCheckInterface;
  8. use Drupal\Core\Access\AccessManager;
  9. use Drupal\Core\Access\DefaultAccessCheck;
  10. use Drupal\Tests\UnitTestCase;
  11. use Drupal\router_test\Access\DefinedTestAccessCheck;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Route;
  15. use Symfony\Component\Routing\RouteCollection;
  16. /**
  17. * Tests the access manager.
  18. *
  19. * @see \Drupal\Core\Access\AccessManager
  20. */
  21. class AccessManagerTest extends UnitTestCase {
  22. /**
  23. * The dependency injection container.
  24. *
  25. * @var \Symfony\Component\DependencyInjection\ContainerBuilder
  26. */
  27. protected $container;
  28. /**
  29. * The collection of routes, which are tested.
  30. *
  31. * @var \Symfony\Component\Routing\RouteCollection
  32. */
  33. protected $routeCollection;
  34. /**
  35. * The access manager to test.
  36. *
  37. * @var \Drupal\Core\Access\AccessManager
  38. */
  39. protected $accessManager;
  40. public static function getInfo() {
  41. return array(
  42. 'name' => 'Access manager tests',
  43. 'description' => 'Test for the AccessManager object.',
  44. 'group' => 'Routing',
  45. );
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function setUp() {
  51. parent::setUp();
  52. $this->container = new ContainerBuilder();
  53. $this->accessManager = new AccessManager();
  54. $this->accessManager->setContainer($this->container);
  55. $this->routeCollection = new RouteCollection();
  56. $this->routeCollection->add('test_route_1', new Route('/test-route-1'));
  57. $this->routeCollection->add('test_route_2', new Route('/test-route-2', array(), array('_access' => 'TRUE')));
  58. $this->routeCollection->add('test_route_3', new Route('/test-route-3', array(), array('_access' => 'FALSE')));
  59. }
  60. /**
  61. * Tests \Drupal\Core\Access\AccessManager::setChecks().
  62. */
  63. public function testSetChecks() {
  64. // Check setChecks without any access checker defined yet.
  65. $this->accessManager->setChecks($this->routeCollection);
  66. foreach ($this->routeCollection->all() as $route) {
  67. $this->assertNull($route->getOption('_access_checks'));
  68. }
  69. $this->setupAccessChecker();
  70. $this->accessManager->setChecks($this->routeCollection);
  71. $this->assertEquals($this->routeCollection->get('test_route_1')->getOption('_access_checks'), NULL);
  72. $this->assertEquals($this->routeCollection->get('test_route_2')->getOption('_access_checks'), array('test_access_default'));
  73. $this->assertEquals($this->routeCollection->get('test_route_3')->getOption('_access_checks'), array('test_access_default'));
  74. }
  75. /**
  76. * Tests \Drupal\Core\Access\AccessManager::check().
  77. */
  78. public function testCheck() {
  79. $request = new Request();
  80. // Check check without any access checker defined yet.
  81. foreach ($this->routeCollection->all() as $route) {
  82. $this->assertFalse($this->accessManager->check($route, $request));
  83. }
  84. $this->setupAccessChecker();
  85. // An access checker got setup, but the routes haven't been setup using
  86. // setChecks.
  87. foreach ($this->routeCollection->all() as $route) {
  88. $this->assertFalse($this->accessManager->check($route, $request));
  89. }
  90. $this->accessManager->setChecks($this->routeCollection);
  91. $this->assertFalse($this->accessManager->check($this->routeCollection->get('test_route_1'), $request));
  92. $this->assertTrue($this->accessManager->check($this->routeCollection->get('test_route_2'), $request));
  93. $this->assertFalse($this->accessManager->check($this->routeCollection->get('test_route_3'), $request));
  94. }
  95. /**
  96. * Provides data for the conjunction test.
  97. *
  98. * @return array
  99. * An array of data for check conjunctions.
  100. *
  101. * @see \Drupal\Tests\Core\Access\AccessManagerTest::testCheckConjunctions()
  102. */
  103. public function providerTestCheckConjunctions() {
  104. $access_configurations = array();
  105. $access_configurations[] = array(
  106. 'conjunction' => 'ALL',
  107. 'name' => 'test_route_4',
  108. 'condition_one' => AccessCheckInterface::ALLOW,
  109. 'condition_two' => AccessCheckInterface::KILL,
  110. 'expected' => FALSE,
  111. );
  112. $access_configurations[] = array(
  113. 'conjunction' => 'ALL',
  114. 'name' => 'test_route_5',
  115. 'condition_one' => AccessCheckInterface::ALLOW,
  116. 'condition_two' => AccessCheckInterface::DENY,
  117. 'expected' => FALSE,
  118. );
  119. $access_configurations[] = array(
  120. 'conjunction' => 'ALL',
  121. 'name' => 'test_route_6',
  122. 'condition_one' => AccessCheckInterface::KILL,
  123. 'condition_two' => AccessCheckInterface::DENY,
  124. 'expected' => FALSE,
  125. );
  126. $access_configurations[] = array(
  127. 'conjunction' => 'ALL',
  128. 'name' => 'test_route_7',
  129. 'condition_one' => AccessCheckInterface::ALLOW,
  130. 'condition_two' => AccessCheckInterface::ALLOW,
  131. 'expected' => TRUE,
  132. );
  133. $access_configurations[] = array(
  134. 'conjunction' => 'ALL',
  135. 'name' => 'test_route_8',
  136. 'condition_one' => AccessCheckInterface::KILL,
  137. 'condition_two' => AccessCheckInterface::KILL,
  138. 'expected' => FALSE,
  139. );
  140. $access_configurations[] = array(
  141. 'conjunction' => 'ALL',
  142. 'name' => 'test_route_9',
  143. 'condition_one' => AccessCheckInterface::DENY,
  144. 'condition_two' => AccessCheckInterface::DENY,
  145. 'expected' => FALSE,
  146. );
  147. $access_configurations[] = array(
  148. 'conjunction' => 'ANY',
  149. 'name' => 'test_route_10',
  150. 'condition_one' => AccessCheckInterface::ALLOW,
  151. 'condition_two' => AccessCheckInterface::KILL,
  152. 'expected' => FALSE,
  153. );
  154. $access_configurations[] = array(
  155. 'conjunction' => 'ANY',
  156. 'name' => 'test_route_11',
  157. 'condition_one' => AccessCheckInterface::ALLOW,
  158. 'condition_two' => AccessCheckInterface::DENY,
  159. 'expected' => TRUE,
  160. );
  161. $access_configurations[] = array(
  162. 'conjunction' => 'ANY',
  163. 'name' => 'test_route_12',
  164. 'condition_one' => AccessCheckInterface::KILL,
  165. 'condition_two' => AccessCheckInterface::DENY,
  166. 'expected' => FALSE,
  167. );
  168. $access_configurations[] = array(
  169. 'conjunction' => 'ANY',
  170. 'name' => 'test_route_13',
  171. 'condition_one' => AccessCheckInterface::ALLOW,
  172. 'condition_two' => AccessCheckInterface::ALLOW,
  173. 'expected' => TRUE,
  174. );
  175. $access_configurations[] = array(
  176. 'conjunction' => 'ANY',
  177. 'name' => 'test_route_14',
  178. 'condition_one' => AccessCheckInterface::KILL,
  179. 'condition_two' => AccessCheckInterface::KILL,
  180. 'expected' => FALSE,
  181. );
  182. $access_configurations[] = array(
  183. 'conjunction' => 'ANY',
  184. 'name' => 'test_route_15',
  185. 'condition_one' => AccessCheckInterface::DENY,
  186. 'condition_two' => AccessCheckInterface::DENY,
  187. 'expected' => FALSE,
  188. );
  189. return $access_configurations;
  190. }
  191. /**
  192. * Test \Drupal\Core\Access\AccessManager::check() with conjunctions.
  193. *
  194. * @dataProvider providerTestCheckConjunctions
  195. */
  196. public function testCheckConjunctions($conjunction, $name, $condition_one, $condition_two, $expected_access) {
  197. $this->setupAccessChecker();
  198. $access_check = new DefinedTestAccessCheck();
  199. $this->container->register('test_access_defined', $access_check);
  200. $this->accessManager->addCheckService('test_access_defined');
  201. $request = new Request();
  202. $route_collection = new RouteCollection();
  203. // Setup a test route for each access configuration.
  204. $requirements = array(
  205. '_access' => static::convertAccessCheckInterfaceToString($condition_one),
  206. '_test_access' => static::convertAccessCheckInterfaceToString($condition_two),
  207. );
  208. $options = array('_access_mode' => $conjunction);
  209. $route = new Route($name, array(), $requirements, $options);
  210. $route_collection->add($name, $route);
  211. $this->accessManager->setChecks($route_collection);
  212. $this->assertSame($this->accessManager->check($route, $request), $expected_access);
  213. }
  214. /**
  215. * Converts AccessCheckInterface constants to a string.
  216. *
  217. * @param mixed $constant
  218. * The access constant which is tested, so either
  219. * AccessCheckInterface::ALLOW, AccessCheckInterface::DENY OR
  220. * AccessCheckInterface::KILL.
  221. *
  222. * @return string
  223. * The corresponding string used in route requirements, so 'TRUE', 'FALSE'
  224. * or 'NULL'.
  225. */
  226. protected static function convertAccessCheckInterfaceToString($constant) {
  227. if ($constant === AccessCheckInterface::ALLOW) {
  228. return 'TRUE';
  229. }
  230. if ($constant === AccessCheckInterface::DENY) {
  231. return 'NULL';
  232. }
  233. if ($constant === AccessCheckInterface::KILL) {
  234. return 'FALSE';
  235. }
  236. }
  237. /**
  238. * Adds a default access check service to the container and the access manager.
  239. */
  240. protected function setupAccessChecker() {
  241. $access_check = new DefaultAccessCheck();
  242. $this->container->register('test_access_default', $access_check);
  243. $this->accessManager->addCheckService('test_access_default');
  244. }
  245. }

Classes

Namesort descending Description
AccessManagerTest Tests the access manager.