/** * Tests the checkAccess() tree manipulator. * * @covers ::checkAccess */ public function testCheckAccess() { // Those menu links that are non-external will have their access checks // performed. 8 routes, but 1 is external, 2 already have their 'access' // property set, and 1 is a child if an inaccessible menu link, so only 4 // calls will be made. $this->accessManager->expects($this->exactly(4))->method('checkNamedRoute')->will($this->returnValueMap(array(array('example1', array(), $this->currentUser, NULL, FALSE), array('example2', array('foo' => 'bar'), $this->currentUser, NULL, TRUE), array('example3', array('baz' => 'qux'), $this->currentUser, NULL, FALSE), array('example5', array(), $this->currentUser, NULL, TRUE)))); $this->mockTree(); $this->originalTree[5]->subtree[7]->access = TRUE; $this->originalTree[8]->access = FALSE; $tree = $this->defaultMenuTreeManipulators->checkAccess($this->originalTree); // Menu link 1: route without parameters, access forbidden, hence removed. $this->assertFalse(array_key_exists(1, $tree)); // Menu link 2: route with parameters, access granted. $element = $tree[2]; $this->assertTrue($element->access); // Menu link 3: route with parameters, access forbidden, hence removed, // including its children. $this->assertFalse(array_key_exists(3, $tree[2]->subtree)); // Menu link 4: child of menu link 3, which already is removed. $this->assertSame(array(), $tree[2]->subtree); // Menu link 5: no route name, treated as external, hence access granted. $element = $tree[5]; $this->assertTrue($element->access); // Menu link 6: external URL, hence access granted. $element = $tree[6]; $this->assertTrue($element->access); // Menu link 7: 'access' already set. $element = $tree[5]->subtree[7]; $this->assertTrue($element->access); // Menu link 8: 'access' already set, to FALSE, hence removed. $this->assertFalse(array_key_exists(8, $tree)); }
/** * Tests that if access is granted, AccessSubscriber will not throw an exception. */ public function testAccessSubscriberDoesNotAlterRequestIfAccessManagerGrantsAccess() { $this->parameterBag->expects($this->once())->method('has')->with(RouteObjectInterface::ROUTE_OBJECT)->will($this->returnValue(TRUE)); $this->parameterBag->expects($this->once())->method('get')->with(RouteObjectInterface::ROUTE_OBJECT)->will($this->returnValue($this->route)); $this->accessManager->expects($this->once())->method('check')->with($this->equalTo($this->route))->will($this->returnValue(TRUE)); $subscriber = new AccessSubscriber($this->accessManager, $this->currentUser); // We're testing that no exception is thrown in this case. There is no // return. $subscriber->onKernelRequestAccessCheck($this->event); }
/** * Tests the access checking of the getContextualLinksArrayByGroup method. * * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup() */ public function testGetContextualLinksArrayByGroupAccessCheck() { $definitions = array('test_plugin1' => array('id' => 'test_plugin1', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'title' => 'Plugin 1', 'weight' => 0, 'group' => 'group1', 'route_name' => 'test_route', 'options' => array()), 'test_plugin2' => array('id' => 'test_plugin2', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'title' => 'Plugin 2', 'weight' => 2, 'group' => 'group1', 'route_name' => 'test_route2', 'options' => array('key' => 'value'))); $this->pluginDiscovery->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions)); $this->accessManager->expects($this->any())->method('checkNamedRoute')->will($this->returnValueMap(array(array('test_route', array('key' => 'value'), $this->account, NULL, TRUE), array('test_route2', array('key' => 'value'), $this->account, NULL, FALSE)))); // Set up mocking of the plugin factory. $map = array(); foreach ($definitions as $plugin_id => $definition) { $plugin = $this->getMock('Drupal\\Core\\Menu\\ContextualLinkInterface'); $plugin->expects($this->any())->method('getRouteName')->will($this->returnValue($definition['route_name'])); $plugin->expects($this->any())->method('getTitle')->will($this->returnValue($definition['title'])); $plugin->expects($this->any())->method('getWeight')->will($this->returnValue($definition['weight'])); $plugin->expects($this->any())->method('getOptions')->will($this->returnValue($definition['options'])); $map[] = array($plugin_id, array(), $plugin); } $this->factory->expects($this->any())->method('createInstance')->will($this->returnValueMap($map)); $result = $this->contextualLinkManager->getContextualLinksArrayByGroup('group1', array('key' => 'value')); // Ensure that access checking was respected. $this->assertTrue(isset($result['test_plugin1'])); $this->assertFalse(isset($result['test_plugin2'])); }
/** * Setup the access manager to always return TRUE. */ public function setupAccessManagerWithTrue() { $this->accessManager->expects($this->any())->method('checkNamedRoute')->will($this->returnValue(TRUE)); }