Best Atoum code snippet using manager.check
AccessManagerTest.php
Source:AccessManagerTest.php
...72 protected $currentUser;73 /**74 * @var \Drupal\Core\Access\CheckProvider75 */76 protected $checkProvider;77 /**78 * {@inheritdoc}79 */80 protected function setUp() {81 parent::setUp();82 $this->container = new ContainerBuilder();83 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();84 $this->container->set('cache_contexts_manager', $cache_contexts_manager);85 \Drupal::setContainer($this->container);86 $this->routeCollection = new RouteCollection();87 $this->routeCollection->add('test_route_1', new Route('/test-route-1'));88 $this->routeCollection->add('test_route_2', new Route('/test-route-2', [], ['_access' => 'TRUE']));89 $this->routeCollection->add('test_route_3', new Route('/test-route-3', [], ['_access' => 'FALSE']));90 $this->routeCollection->add('test_route_4', new Route('/test-route-4/{value}', [], ['_access' => 'TRUE']));91 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');92 $map = [];93 foreach ($this->routeCollection->all() as $name => $route) {94 $map[] = [$name, [], $route];95 }96 $map[] = ['test_route_4', ['value' => 'example'], $this->routeCollection->get('test_route_4')];97 $this->routeProvider->expects($this->any())98 ->method('getRouteByName')99 ->will($this->returnValueMap($map));100 $map = [];101 $map[] = ['test_route_1', [], '/test-route-1'];102 $map[] = ['test_route_2', [], '/test-route-2'];103 $map[] = ['test_route_3', [], '/test-route-3'];104 $map[] = ['test_route_4', ['value' => 'example'], '/test-route-4/example'];105 $this->paramConverter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');106 $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');107 $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface');108 $this->argumentsResolverFactory = $this->getMock('Drupal\Core\Access\AccessArgumentsResolverFactoryInterface');109 $this->checkProvider = new CheckProvider();110 $this->checkProvider->setContainer($this->container);111 $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);112 }113 /**114 * Tests \Drupal\Core\Access\AccessManager::setChecks().115 */116 public function testSetChecks() {117 // Check setChecks without any access checker defined yet.118 $this->checkProvider->setChecks($this->routeCollection);119 foreach ($this->routeCollection->all() as $route) {120 $this->assertNull($route->getOption('_access_checks'));121 }122 $this->setupAccessChecker();123 $this->checkProvider->setChecks($this->routeCollection);124 $this->assertEquals($this->routeCollection->get('test_route_1')->getOption('_access_checks'), NULL);125 $this->assertEquals($this->routeCollection->get('test_route_2')->getOption('_access_checks'), ['test_access_default']);126 $this->assertEquals($this->routeCollection->get('test_route_3')->getOption('_access_checks'), ['test_access_default']);127 }128 /**129 * Tests setChecks with a dynamic access checker.130 */131 public function testSetChecksWithDynamicAccessChecker() {132 // Setup the access manager.133 $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);134 // Setup the dynamic access checker.135 $access_check = $this->getMock('Drupal\Tests\Core\Access\TestAccessCheckInterface');136 $this->container->set('test_access', $access_check);137 $this->checkProvider->addCheckService('test_access', 'access');138 $route = new Route('/test-path', [], ['_foo' => '1', '_bar' => '1']);139 $route2 = new Route('/test-path', [], ['_foo' => '1', '_bar' => '2']);140 $collection = new RouteCollection();141 $collection->add('test_route', $route);142 $collection->add('test_route2', $route2);143 $access_check->expects($this->exactly(2))144 ->method('applies')145 ->with($this->isInstanceOf('Symfony\Component\Routing\Route'))146 ->will($this->returnCallback(function (Route $route) {147 return $route->getRequirement('_bar') == 2;148 }));149 $this->checkProvider->setChecks($collection);150 $this->assertEmpty($route->getOption('_access_checks'));151 $this->assertEquals(['test_access'], $route2->getOption('_access_checks'));152 }153 /**154 * Tests \Drupal\Core\Access\AccessManager::check().155 */156 public function testCheck() {157 $route_matches = [];158 // Construct route match objects.159 foreach ($this->routeCollection->all() as $route_name => $route) {160 $route_matches[$route_name] = new RouteMatch($route_name, $route, [], []);161 }162 // Check route access without any access checker defined yet.163 foreach ($route_matches as $route_match) {164 $this->assertEquals(FALSE, $this->accessManager->check($route_match, $this->account));165 $this->assertEquals(AccessResult::neutral(), $this->accessManager->check($route_match, $this->account, NULL, TRUE));166 }167 $this->setupAccessChecker();168 // An access checker got setup, but the routes haven't been setup using169 // setChecks.170 foreach ($route_matches as $route_match) {171 $this->assertEquals(FALSE, $this->accessManager->check($route_match, $this->account));172 $this->assertEquals(AccessResult::neutral(), $this->accessManager->check($route_match, $this->account, NULL, TRUE));173 }174 // Now applicable access checks have been saved on each route object.175 $this->checkProvider->setChecks($this->routeCollection);176 $this->setupAccessArgumentsResolverFactory();177 $this->assertEquals(FALSE, $this->accessManager->check($route_matches['test_route_1'], $this->account));178 $this->assertEquals(TRUE, $this->accessManager->check($route_matches['test_route_2'], $this->account));179 $this->assertEquals(FALSE, $this->accessManager->check($route_matches['test_route_3'], $this->account));180 $this->assertEquals(TRUE, $this->accessManager->check($route_matches['test_route_4'], $this->account));181 $this->assertEquals(AccessResult::neutral(), $this->accessManager->check($route_matches['test_route_1'], $this->account, NULL, TRUE));182 $this->assertEquals(AccessResult::allowed(), $this->accessManager->check($route_matches['test_route_2'], $this->account, NULL, TRUE));183 $this->assertEquals(AccessResult::forbidden(), $this->accessManager->check($route_matches['test_route_3'], $this->account, NULL, TRUE));184 $this->assertEquals(AccessResult::allowed(), $this->accessManager->check($route_matches['test_route_4'], $this->account, NULL, TRUE));185 }186 /**187 * Tests \Drupal\Core\Access\AccessManager::check() with no account specified.188 *189 * @covers ::check190 */191 public function testCheckWithNullAccount() {192 $this->setupAccessChecker();193 $this->checkProvider->setChecks($this->routeCollection);194 $route = $this->routeCollection->get('test_route_2');195 $route_match = new RouteMatch('test_route_2', $route, [], []);196 // Asserts that the current user is passed to the access arguments resolver197 // factory.198 $this->setupAccessArgumentsResolverFactory()199 ->with($route_match, $this->currentUser, NULL);200 $this->assertTrue($this->accessManager->check($route_match));201 }202 /**203 * Provides data for the conjunction test.204 *205 * @return array206 * An array of data for check conjunctions.207 *208 * @see \Drupal\Tests\Core\Access\AccessManagerTest::testCheckConjunctions()209 */210 public function providerTestCheckConjunctions() {211 $access_allow = AccessResult::allowed();212 $access_deny = AccessResult::neutral();213 $access_kill = AccessResult::forbidden();214 $access_configurations = [];215 $access_configurations[] = [216 'name' => 'test_route_4',217 'condition_one' => 'TRUE',218 'condition_two' => 'FALSE',219 'expected' => $access_kill,220 ];221 $access_configurations[] = [222 'name' => 'test_route_5',223 'condition_one' => 'TRUE',224 'condition_two' => 'NULL',225 'expected' => $access_deny,226 ];227 $access_configurations[] = [228 'name' => 'test_route_6',229 'condition_one' => 'FALSE',230 'condition_two' => 'NULL',231 'expected' => $access_kill,232 ];233 $access_configurations[] = [234 'name' => 'test_route_7',235 'condition_one' => 'TRUE',236 'condition_two' => 'TRUE',237 'expected' => $access_allow,238 ];239 $access_configurations[] = [240 'name' => 'test_route_8',241 'condition_one' => 'FALSE',242 'condition_two' => 'FALSE',243 'expected' => $access_kill,244 ];245 $access_configurations[] = [246 'name' => 'test_route_9',247 'condition_one' => 'NULL',248 'condition_two' => 'NULL',249 'expected' => $access_deny,250 ];251 return $access_configurations;252 }253 /**254 * Test \Drupal\Core\Access\AccessManager::check() with conjunctions.255 *256 * @dataProvider providerTestCheckConjunctions257 */258 public function testCheckConjunctions($name, $condition_one, $condition_two, $expected_access) {259 $this->setupAccessChecker();260 $access_check = new DefinedTestAccessCheck();261 $this->container->register('test_access_defined', $access_check);262 $this->checkProvider->addCheckService('test_access_defined', 'access', ['_test_access']);263 $route_collection = new RouteCollection();264 // Setup a test route for each access configuration.265 $requirements = [266 '_access' => $condition_one,267 '_test_access' => $condition_two,268 ];269 $route = new Route($name, [], $requirements);270 $route_collection->add($name, $route);271 $this->checkProvider->setChecks($route_collection);272 $this->setupAccessArgumentsResolverFactory();273 $route_match = new RouteMatch($name, $route, [], []);274 $this->assertEquals($expected_access->isAllowed(), $this->accessManager->check($route_match, $this->account));275 $this->assertEquals($expected_access, $this->accessManager->check($route_match, $this->account, NULL, TRUE));276 }277 /**278 * Tests the checkNamedRoute method.279 *280 * @see \Drupal\Core\Access\AccessManager::checkNamedRoute()281 */282 public function testCheckNamedRoute() {283 $this->setupAccessChecker();284 $this->checkProvider->setChecks($this->routeCollection);285 $this->setupAccessArgumentsResolverFactory();286 $this->paramConverter->expects($this->at(0))287 ->method('convert')288 ->with([RouteObjectInterface::ROUTE_NAME => 'test_route_2', RouteObjectInterface::ROUTE_OBJECT => $this->routeCollection->get('test_route_2')])289 ->will($this->returnValue([]));290 $this->paramConverter->expects($this->at(1))291 ->method('convert')292 ->with([RouteObjectInterface::ROUTE_NAME => 'test_route_2', RouteObjectInterface::ROUTE_OBJECT => $this->routeCollection->get('test_route_2')])293 ->will($this->returnValue([]));294 $this->paramConverter->expects($this->at(2))295 ->method('convert')296 ->with(['value' => 'example', RouteObjectInterface::ROUTE_NAME => 'test_route_4', RouteObjectInterface::ROUTE_OBJECT => $this->routeCollection->get('test_route_4')])297 ->will($this->returnValue(['value' => 'example']));298 $this->paramConverter->expects($this->at(3))299 ->method('convert')300 ->with(['value' => 'example', RouteObjectInterface::ROUTE_NAME => 'test_route_4', RouteObjectInterface::ROUTE_OBJECT => $this->routeCollection->get('test_route_4')])301 ->will($this->returnValue(['value' => 'example']));302 // Tests the access with routes with parameters without given request.303 $this->assertEquals(TRUE, $this->accessManager->checkNamedRoute('test_route_2', [], $this->account));304 $this->assertEquals(AccessResult::allowed(), $this->accessManager->checkNamedRoute('test_route_2', [], $this->account, TRUE));305 $this->assertEquals(TRUE, $this->accessManager->checkNamedRoute('test_route_4', ['value' => 'example'], $this->account));306 $this->assertEquals(AccessResult::allowed(), $this->accessManager->checkNamedRoute('test_route_4', ['value' => 'example'], $this->account, TRUE));307 }308 /**309 * Tests the checkNamedRoute with upcasted values.310 *311 * @see \Drupal\Core\Access\AccessManager::checkNamedRoute()312 */313 public function testCheckNamedRouteWithUpcastedValues() {314 $this->routeCollection = new RouteCollection();315 $route = new Route('/test-route-1/{value}', [], ['_test_access' => 'TRUE']);316 $this->routeCollection->add('test_route_1', $route);317 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');318 $this->routeProvider->expects($this->any())319 ->method('getRouteByName')320 ->with('test_route_1', ['value' => 'example'])321 ->will($this->returnValue($route));322 $map = [];323 $map[] = ['test_route_1', ['value' => 'example'], '/test-route-1/example'];324 $this->paramConverter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');325 $this->paramConverter->expects($this->atLeastOnce())326 ->method('convert')327 ->with(['value' => 'example', RouteObjectInterface::ROUTE_NAME => 'test_route_1', RouteObjectInterface::ROUTE_OBJECT => $route])328 ->will($this->returnValue(['value' => 'upcasted_value']));329 $this->setupAccessArgumentsResolverFactory($this->exactly(2))330 ->with($this->callback(function ($route_match) {331 return $route_match->getParameters()->get('value') == 'upcasted_value';332 }));333 $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);334 $access_check = $this->getMock('Drupal\Tests\Core\Access\TestAccessCheckInterface');335 $access_check->expects($this->atLeastOnce())336 ->method('applies')337 ->will($this->returnValue(TRUE));338 $access_check->expects($this->atLeastOnce())339 ->method('access')340 ->will($this->returnValue(AccessResult::forbidden()));341 $this->container->set('test_access', $access_check);342 $this->checkProvider->addCheckService('test_access', 'access');343 $this->checkProvider->setChecks($this->routeCollection);344 $this->assertEquals(FALSE, $this->accessManager->checkNamedRoute('test_route_1', ['value' => 'example'], $this->account));345 $this->assertEquals(AccessResult::forbidden(), $this->accessManager->checkNamedRoute('test_route_1', ['value' => 'example'], $this->account, TRUE));346 }347 /**348 * Tests the checkNamedRoute with default values.349 *350 * @covers ::checkNamedRoute351 */352 public function testCheckNamedRouteWithDefaultValue() {353 $this->routeCollection = new RouteCollection();354 $route = new Route('/test-route-1/{value}', ['value' => 'example'], ['_test_access' => 'TRUE']);355 $this->routeCollection->add('test_route_1', $route);356 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');357 $this->routeProvider->expects($this->any())358 ->method('getRouteByName')359 ->with('test_route_1', [])360 ->will($this->returnValue($route));361 $map = [];362 $map[] = ['test_route_1', ['value' => 'example'], '/test-route-1/example'];363 $this->paramConverter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');364 $this->paramConverter->expects($this->atLeastOnce())365 ->method('convert')366 ->with(['value' => 'example', RouteObjectInterface::ROUTE_NAME => 'test_route_1', RouteObjectInterface::ROUTE_OBJECT => $route])367 ->will($this->returnValue(['value' => 'upcasted_value']));368 $this->setupAccessArgumentsResolverFactory($this->exactly(2))369 ->with($this->callback(function ($route_match) {370 return $route_match->getParameters()->get('value') == 'upcasted_value';371 }));372 $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);373 $access_check = $this->getMock('Drupal\Tests\Core\Access\TestAccessCheckInterface');374 $access_check->expects($this->atLeastOnce())375 ->method('applies')376 ->will($this->returnValue(TRUE));377 $access_check->expects($this->atLeastOnce())378 ->method('access')379 ->will($this->returnValue(AccessResult::forbidden()));380 $this->container->set('test_access', $access_check);381 $this->checkProvider->addCheckService('test_access', 'access');382 $this->checkProvider->setChecks($this->routeCollection);383 $this->assertEquals(FALSE, $this->accessManager->checkNamedRoute('test_route_1', [], $this->account));384 $this->assertEquals(AccessResult::forbidden(), $this->accessManager->checkNamedRoute('test_route_1', [], $this->account, TRUE));385 }386 /**387 * Tests checkNamedRoute given an invalid/non existing route name.388 */389 public function testCheckNamedRouteWithNonExistingRoute() {390 $this->routeProvider->expects($this->any())391 ->method('getRouteByName')392 ->will($this->throwException(new RouteNotFoundException()));393 $this->setupAccessChecker();394 $this->assertEquals(FALSE, $this->accessManager->checkNamedRoute('test_route_1', [], $this->account), 'A non existing route lead to access.');395 $this->assertEquals(AccessResult::forbidden()->addCacheTags(['config:core.extension']), $this->accessManager->checkNamedRoute('test_route_1', [], $this->account, TRUE), 'A non existing route lead to access.');396 }397 /**398 * Tests that an access checker throws an exception for not allowed values.399 *400 * @dataProvider providerCheckException401 */402 public function testCheckException($return_value) {403 $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');404 // Setup a test route for each access configuration.405 $requirements = [406 '_test_incorrect_value' => 'TRUE',407 ];408 $options = [409 '_access_checks' => [410 'test_incorrect_value',411 ],412 ];413 $route = new Route('', [], $requirements, $options);414 $route_provider->expects($this->any())415 ->method('getRouteByName')416 ->will($this->returnValue($route));417 $this->paramConverter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');418 $this->paramConverter->expects($this->any())419 ->method('convert')420 ->will($this->returnValue([]));421 $this->setupAccessArgumentsResolverFactory();422 $container = new ContainerBuilder();423 // Register a service that will return an incorrect value.424 $access_check = $this->getMock('Drupal\Tests\Core\Access\TestAccessCheckInterface');425 $access_check->expects($this->any())426 ->method('access')427 ->will($this->returnValue($return_value));428 $container->set('test_incorrect_value', $access_check);429 $access_manager = new AccessManager($route_provider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);430 $this->checkProvider->setContainer($container);431 $this->checkProvider->addCheckService('test_incorrect_value', 'access');432 $this->setExpectedException(AccessException::class);433 $access_manager->checkNamedRoute('test_incorrect_value', [], $this->account);434 }435 /**436 * Data provider for testCheckException.437 *438 * @return array439 */440 public function providerCheckException() {441 return [442 [[1]],443 ['string'],444 [0],445 [1],446 ];447 }448 /**449 * Adds a default access check service to the container and the access manager.450 */451 protected function setupAccessChecker() {452 $access_check = new DefaultAccessCheck();453 $this->container->register('test_access_default', $access_check);454 $this->checkProvider->addCheckService('test_access_default', 'access', ['_access']);455 }456 /**457 * Add default expectations to the access arguments resolver factory.458 */459 protected function setupAccessArgumentsResolverFactory($constraint = NULL) {460 if (!isset($constraint)) {461 $constraint = $this->any();462 }463 return $this->argumentsResolverFactory->expects($constraint)464 ->method('getArgumentsResolver')465 ->will($this->returnCallback(function ($route_match, $account) {466 $resolver = $this->getMock('Drupal\Component\Utility\ArgumentsResolverInterface');467 $resolver->expects($this->any())468 ->method('getArguments')...
check
Using AI Code Generation
1$manager = new Manager();2$manager->check();3$manager = new Manager();4$manager->check();5$manager = new Manager();6$manager->check();7$manager = new Manager();8$manager->check();9$manager = new Manager();10$manager->check();11$manager = new Manager();12$manager->check();13$manager = new Manager();14$manager->check();15$manager = new Manager();16$manager->check();17$manager = new Manager();18$manager->check();19$manager = new Manager();20$manager->check();21$manager = new Manager();22$manager->check();23$manager = new Manager();24$manager->check();25$manager = new Manager();26$manager->check();27$manager = new Manager();28$manager->check();29$manager = new Manager();30$manager->check();31$manager = new Manager();32$manager->check();33$manager = new Manager();34$manager->check();35$manager = new Manager();36$manager->check();
check
Using AI Code Generation
1require_once 'manager.php';2$manager = new Manager();3$manager->check();4require_once 'manager.php';5$manager = new Manager();6$manager->check();7require_once 'manager.php';8$manager = new Manager();9$manager->check();10require_once 'manager.php';11$manager = new Manager();12$manager->check();13require_once 'manager.php';14$manager = new Manager();15$manager->check();16require_once 'manager.php';17$manager = new Manager();18$manager->check();19require_once 'manager.php';20$manager = new Manager();21$manager->check();22require_once 'manager.php';23$manager = new Manager();24$manager->check();25require_once 'manager.php';26$manager = new Manager();27$manager->check();28require_once 'manager.php';29$manager = new Manager();30$manager->check();31require_once 'manager.php';32$manager = new Manager();33$manager->check();34require_once 'manager.php';35$manager = new Manager();36$manager->check();37require_once 'manager.php';38$manager = new Manager();39$manager->check();40require_once 'manager.php';41$manager = new Manager();42$manager->check();43require_once 'manager.php';44$manager = new Manager();45$manager->check();
check
Using AI Code Generation
1$manager = new Manager();2$manager->check("1.php");3$manager = new Manager();4$manager->check("2.php");5$manager = new Manager();6$manager->check("3.php");7$manager = new Manager();8$manager->check("4.php");9$manager = new Manager();10$manager->check("5.php");11$manager = new Manager();12$manager->check("6.php");13$manager = new Manager();14$manager->check("7.php");15$manager = new Manager();16$manager->check("8.php");17$manager = new Manager();18$manager->check("9.php");19$manager = new Manager();20$manager->check("10.php");21$manager = new Manager();22$manager->check("11.php");23$manager = new Manager();24$manager->check("12.php");25$manager = new Manager();26$manager->check("13.php");27$manager = new Manager();28$manager->check("14.php");29$manager = new Manager();30$manager->check("15.php");31$manager = new Manager();32$manager->check("16.php");
check
Using AI Code Generation
1require_once('manager.php');2$manager = new Manager();3if($manager->check('1.php')) {4 echo "File exists";5} else {6 echo "File doesn't exist";7}8require_once('manager.php');9$manager = new Manager();10if($manager->check('2.php')) {11 echo "File exists";12} else {13 echo "File doesn't exist";14}15require_once('manager.php');16$manager = new Manager();17if($manager->check('3.php')) {18 echo "File exists";19} else {20 echo "File doesn't exist";21}22require_once('manager.php');23$manager = new Manager();24if($manager->check('4.php')) {25 echo "File exists";26} else {27 echo "File doesn't exist";28}29require_once('manager.php');30$manager = new Manager();31if($manager->check('5.php')) {32 echo "File exists";33} else {34 echo "File doesn't exist";35}36require_once('manager.php');37$manager = new Manager();38if($manager->check('6.php')) {39 echo "File exists";40} else {41 echo "File doesn't exist";42}43require_once('manager.php');44$manager = new Manager();45if($manager->check('7.php')) {46 echo "File exists";47} else {48 echo "File doesn't exist";49}50require_once('manager.php');51$manager = new Manager();52if($manager->check('8.php')) {53 echo "File exists";54} else {55 echo "File doesn't exist";56}57require_once('manager.php');58$manager = new Manager();59if($manager->check('9.php')) {60 echo "File exists";61} else {62 echo "File doesn't exist";63}
check
Using AI Code Generation
1include_once("manager.php");2$manager = new Manager();3$manager->check();4include_once("manager.php");5$manager = new Manager();6$manager->check();7class Manager {8 private function __construct() {}9 public static function getInstance()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with check on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!