/** * Tests the rebuild with routes provided by a callback. * * @see \Drupal\Core\Routing\RouteBuilder::rebuild() */ public function testRebuildWithProviderBasedRoutes() { $this->lock->expects($this->once())->method('acquire')->with('router_rebuild')->will($this->returnValue(TRUE)); $this->yamlDiscovery->expects($this->once())->method('findAll')->will($this->returnValue(array('test_module' => array('route_callbacks' => array('\\Drupal\\Tests\\Core\\Routing\\TestRouteSubscriber::routesFromArray', 'test_module.route_service:routesFromCollection'))))); $container = new ContainerBuilder(); $container->set('test_module.route_service', new TestRouteSubscriber()); $this->controllerResolver->expects($this->any())->method('getControllerFromDefinition')->will($this->returnCallback(function ($controller) use($container) { $count = substr_count($controller, ':'); if ($count == 1) { list($service, $method) = explode(':', $controller, 2); $object = $container->get($service); } else { list($class, $method) = explode('::', $controller, 2); $object = new $class(); } return array($object, $method); })); $route_collection_filled = new RouteCollection(); $route_collection_filled->add('test_route.1', new Route('/test-route/1')); $route_collection_filled->add('test_route.2', new Route('/test-route/2')); $route_build_event = new RouteBuildEvent($route_collection_filled); // Ensure that the alter routes events are fired. $this->dispatcher->expects($this->at(0))->method('dispatch')->with(RoutingEvents::DYNAMIC, $route_build_event); $this->dispatcher->expects($this->at(1))->method('dispatch')->with(RoutingEvents::ALTER, $route_build_event); // Ensure that the routes are set to the dumper and dumped. $this->dumper->expects($this->at(0))->method('addRoutes')->with($route_collection_filled); $this->dumper->expects($this->at(1))->method('dump'); $this->assertTrue($this->routeBuilder->rebuild()); }
/** * Tests a YAML file containing both static permissions and a callback. */ public function testPermissionsYamlStaticAndCallback() { vfsStreamWrapper::register(); $root = new vfsStreamDirectory('modules'); vfsStreamWrapper::setRoot($root); $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->once())->method('getModuleDirectories')->willReturn(array('module_a' => vfsStream::url('modules/module_a'))); $url = vfsStream::url('modules'); mkdir($url . '/module_a'); file_put_contents($url . '/module_a/module_a.permissions.yml', "'access module a':\n title: 'Access A'\n description: 'bla bla'\npermission_callbacks:\n - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'\n"); $modules = array('module_a'); $extensions = array('module_a' => $this->mockModuleExtension('module_a', 'Module a')); $this->moduleHandler->expects($this->any())->method('getImplementations')->with('permission')->willReturn(array()); $this->moduleHandler->expects($this->any())->method('getModuleList')->willReturn(array_flip($modules)); $this->controllerResolver->expects($this->once())->method('getControllerFromDefinition')->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')->willReturn(array(new TestPermissionCallbacks(), 'titleDescription')); $this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver); // Setup system_rebuild_module_data(). $this->permissionHandler->setSystemRebuildModuleData($extensions); $actual_permissions = $this->permissionHandler->getPermissions(); $this->assertCount(2, $actual_permissions); $this->assertEquals($actual_permissions['access module a']['title'], 'Access A'); $this->assertEquals($actual_permissions['access module a']['provider'], 'module_a'); $this->assertEquals($actual_permissions['access module a']['description'], 'bla bla'); $this->assertEquals($actual_permissions['access module b']['title'], 'Access B'); $this->assertEquals($actual_permissions['access module b']['provider'], 'module_a'); $this->assertEquals($actual_permissions['access module b']['description'], 'bla bla'); }
/** * Test the access method. */ public function testAccess() { $request = new Request(array()); $this->controllerResolver->expects($this->at(0))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')->will($this->returnValue(array(new TestController(), 'accessDeny'))); $this->argumentsResolver->expects($this->at(0))->method('getArguments')->will($this->returnValue(array())); $this->controllerResolver->expects($this->at(1))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')->will($this->returnValue(array(new TestController(), 'accessAllow'))); $this->argumentsResolver->expects($this->at(1))->method('getArguments')->will($this->returnValue(array())); $this->controllerResolver->expects($this->at(2))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')->will($this->returnValue(array(new TestController(), 'accessParameter'))); $this->argumentsResolver->expects($this->at(2))->method('getArguments')->will($this->returnValue(array('parameter' => 'TRUE'))); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')); $account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $this->assertSame(AccessInterface::DENY, $this->accessChecker->access($route, $request, $account)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')); $this->assertSame(AccessInterface::ALLOW, $this->accessChecker->access($route, $request, $account)); $route = new Route('/test-route', array('parameter' => 'TRUE'), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')); $this->assertSame(AccessInterface::ALLOW, $this->accessChecker->access($route, $request, $account)); }
/** * Tests a dynamic title. * * @see \Drupal\Core\Controller\TitleResolver::getTitle() */ public function testDynamicTitle() { $request = new Request(); $route = new Route('/test-route', array('_title' => 'static title', '_title_callback' => 'Drupal\\Tests\\Core\\Controller\\TitleCallback::example')); $callable = array(new TitleCallback(), 'example'); $this->controllerResolver->expects($this->once())->method('getControllerFromDefinition')->with('Drupal\\Tests\\Core\\Controller\\TitleCallback::example')->will($this->returnValue($callable)); $this->controllerResolver->expects($this->once())->method('getArguments')->with($request, $callable)->will($this->returnValue(array('example'))); $this->assertEquals('test example', $this->titleResolver->getTitle($request, $route)); }
/** * Test the access method. */ public function testAccess() { $route_match = $this->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'); $this->controllerResolver->expects($this->at(0))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')->will($this->returnValue(array(new TestController(), 'accessDeny'))); $resolver0 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver0->expects($this->once())->method('getArguments')->will($this->returnValue(array())); $this->argumentsResolverFactory->expects($this->at(0))->method('getArgumentsResolver')->will($this->returnValue($resolver0)); $this->controllerResolver->expects($this->at(1))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')->will($this->returnValue(array(new TestController(), 'accessAllow'))); $resolver1 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver1->expects($this->once())->method('getArguments')->will($this->returnValue(array())); $this->argumentsResolverFactory->expects($this->at(1))->method('getArgumentsResolver')->will($this->returnValue($resolver1)); $this->controllerResolver->expects($this->at(2))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')->will($this->returnValue(array(new TestController(), 'accessParameter'))); $resolver2 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver2->expects($this->once())->method('getArguments')->will($this->returnValue(array('parameter' => 'TRUE'))); $this->argumentsResolverFactory->expects($this->at(2))->method('getArgumentsResolver')->will($this->returnValue($resolver2)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')); $account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $this->assertEquals(AccessResult::neutral(), $this->accessChecker->access($route, $route_match, $account)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')); $this->assertEquals(AccessResult::allowed(), $this->accessChecker->access($route, $route_match, $account)); $route = new Route('/test-route', array('parameter' => 'TRUE'), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')); $this->assertEquals(AccessResult::allowed(), $this->accessChecker->access($route, $route_match, $account)); }
/** * @covers \Drupal\Core\Menu\LocalActionManager::getActionsForRoute() * * @dataProvider getActionsForRouteProvider */ public function testGetActionsForRoute($route_appears, array $plugin_definitions, array $expected_actions) { $this->discovery->expects($this->any())->method('getDefinitions')->will($this->returnValue($plugin_definitions)); $map = array(); foreach ($plugin_definitions as $plugin_id => $plugin_definition) { $plugin = $this->getMock('Drupal\\Core\\Menu\\LocalActionInterface'); $plugin->expects($this->any())->method('getRouteName')->will($this->returnValue($plugin_definition['route_name'])); $plugin->expects($this->any())->method('getRouteParameters')->will($this->returnValue(isset($plugin_definition['route_parameters']) ? $plugin_definition['route_parameters'] : array())); $plugin->expects($this->any())->method('getTitle')->will($this->returnValue($plugin_definition['title'])); $this->controllerResolver->expects($this->any())->method('getArguments')->with($this->request, array($plugin, 'getTitle'))->will($this->returnValue(array())); $plugin->expects($this->any())->method('getWeight')->will($this->returnValue($plugin_definition['weight'])); $this->controllerResolver->expects($this->any())->method('getArguments')->with($this->request, array($plugin, 'getTitle'))->will($this->returnValue(array())); $map[] = array($plugin_id, array(), $plugin); } $this->factory->expects($this->any())->method('createInstance')->will($this->returnValueMap($map)); $this->assertEquals($expected_actions, $this->localActionManager->getActionsForRoute($route_appears)); }
/** * Setups the controller resolver to return the given controller definition. * * @param string $controller_definition * The definition of a controller */ protected function setupControllerResolver($controller_definition) { $controller = $controller_definition; list($class, $method) = explode('::', $controller); $this->controllerResolver->expects($this->atLeastOnce())->method('getControllerFromDefinition')->with($controller_definition)->will($this->returnValue(array(new $class(), $method))); }