public function setUp() { parent::setUp(); $this->mockedRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); $this->mockedSiteMapControllerService = $this->getMockBuilder('RepoSitemapBundle\\Services\\SiteMapControllerService')->disableOriginalConstructor()->getMock(); $this->sitemapInfo = array('type' => SiteMapOptionService::TYPE_DYNAMIC, 'repository' => 'AppBundle:Test', 'lastmod' => '2000-01-01', 'changefreq' => 'daily', 'priority' => 0.67); $this->route = new Route('tests'); $this->route->setOption('sitemap', $this->sitemapInfo); }
public function setUp() { parent::setUp(); $this->mockedSiteMapOptionService = $this->getMockBuilder('RepoSitemapBundle\\Services\\SiteMapOptionService')->disableOriginalConstructor()->setMethods(array('getOptionValues', 'setOtherOptions'))->getMock(); $this->mockedRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); $this->sitemapOption = array('repository' => 'AppBundle:Test', 'lastmod' => '2000-01-01', 'changefreq' => 'daily', 'priority' => 0.67); $this->route = new Route('tests'); $this->route->setOption('sitemap', $this->sitemapOption); }
/** * @covers ::buildBasicRenderable */ public function testBuildBasicRenderable() { $route = new Route('/test-view'); $route->setDefault('view_id', 'test_view'); $route->setOption('_view_display_plugin_id', 'page'); $route->setOption('_view_display_show_admin_links', TRUE); $result = Page::buildBasicRenderable('test_view', 'page_1', [], $route); $this->assertEquals('test_view', $result['#view_id']); $this->assertEquals('page', $result['#view_display_plugin_id']); $this->assertEquals(TRUE, $result['#view_display_show_admin_links']); }
/** * Handle method annotations * * @param mixed $targetObj * @param \ReflectionMethod $reflection * @param array $annotations * * @return mixed */ public function handleMethodAnnotations(\ReflectionMethod $reflection, array $annotations, $targetObj = null) { if (isset($annotations['Route'])) { foreach ($annotations['Route'] as $route) { if ($route instanceof \stdClass && isset($route->path)) { $symfonyRoute = new Route($route->path, isset($route->defaults) ? $route->defaults : [], isset($route->requirements) ? $route->requirements : [], isset($route->options) ? $route->options : [], isset($route->host) ? $route->host : '', isset($route->schemes) ? $route->schemes : [], isset($route->methods) ? $route->methods : []); $symfonyRoute->setOption('karmaController', $reflection->getDeclaringClass()->getName()); $symfonyRoute->setOption('karmaAction', $reflection->getName()); $this->routeCollection->add(isset($route->name) ? $route->name : $reflection->getDeclaringClass()->getName() . ':' . $reflection->getName(), $symfonyRoute); } } } }
/** * {@inheritdoc} */ protected function processRoute(Route $route) { // Add entity upcasting information. $parameters = $route->getOption('parameters') ?: array(); $parameters += array('menu_link_plugin' => array('type' => 'menu_link_plugin')); $route->setOption('parameters', $parameters); }
protected function revisionDeleteRoute(EntityTypeInterface $entity_type) { $route = new Route($entity_type->getLinkTemplate('revision-delete')); $route->setDefault('_form', 'Drupal\\content_entity_base\\Entity\\Form\\EntityRevisionDeleteForm'); $route->setDefault('_title', 'Delete earlier revision'); $route->setRequirement('_entity_access_revision', $entity_type->id() . '.delete'); $route->setOption('parameters', [$entity_type->id() => ['type' => 'entity:' . $entity_type->id()], $entity_type->id() . '_revision' => ['type' => 'entity_revision:' . $entity_type->id()]]); return $route; }
/** * Confirms that a compiled route with default values has the correct outline. */ public function testCompilationDefaultValue() { // Because "here" has a default value, it should not factor into the outline // or the fitness. $route = new Route('/test/{something}/more/{here}', array('here' => 'there')); $route->setOption('compiler_class', 'Drupal\\Core\\Routing\\RouteCompiler'); $compiled = $route->compile(); $this->assertEquals($compiled->getFit(), 5, 'The fit was not correct.'); $this->assertEquals($compiled->getPatternOutline(), '/test/%/more', 'The pattern outline was not correct.'); }
protected function addPageRoute(EntityTypeInterface $entity_type) { $route = new Route($entity_type->getLinkTemplate('add-page')); $route->setDefault('_controller', '\\Drupal\\content_entity_base\\Entity\\Controller\\EntityBaseController::addPage'); $route->setDefault('_title_callback', '\\Drupal\\content_entity_base\\Entity\\Controller\\EntityBaseController::getAddPageTitle'); $route->setDefault('entity_definition', $entity_type->id()); $route->setOption('parameters', ['entity_definition' => ['type' => 'entity_definition']]); $route->setRequirement('_entity_create_access', $entity_type->id()); return $route; }
/** * Gets the entity revision view route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getRevisionViewRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('revision')) { $entity_type_id = $entity_type->id(); $route = new Route($entity_type->getLinkTemplate('revision')); $route->addDefaults(['_controller' => '\\Drupal\\entity\\Controller\\RevisionController::view', '_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::title']); $route->addRequirements(['_entity_access_revision' => "{$entity_type_id}.view"]); $route->setOption('parameters', [$entity_type->id() => ['type' => 'entity:' . $entity_type->id()], $entity_type->id() . '_revision' => ['type' => 'entity_revision:' . $entity_type->id()]]); return $route; } }
/** * Gets the add-form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getAddFormRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('add-form')) { $entity_type_id = $entity_type->id(); $parameters = [$entity_type_id => ['type' => 'entity:' . $entity_type_id]]; $route = new Route($entity_type->getLinkTemplate('add-form')); $bundle_entity_type_id = $entity_type->getBundleEntityType(); // Content entities with bundles are added via a dedicated controller. $route->setDefaults(['_controller' => 'Drupal\\drupalbristol_sponsors\\Controller\\SponsorEntityAddController::addForm', '_title_callback' => 'Drupal\\drupalbristol_sponsors\\Controller\\SponsorEntityAddController::getAddFormTitle'])->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}'); $parameters[$bundle_entity_type_id] = ['type' => 'entity:' . $bundle_entity_type_id]; $route->setOption('parameters', $parameters)->setOption('_admin_route', TRUE); return $route; } }
/** * @covers ::defaultFilter * @covers ::applyFilter * * @dataProvider providerTestDefaultFilter */ public function testDefaultFilter($applies, $has_route, $auth_option, $provider_id, $global) { $authentication_manager = new AuthenticationManager(); $auth_provider = $this->getMock('Drupal\\Core\\Authentication\\AuthenticationProviderInterface'); $authentication_manager->addProvider($auth_provider, $provider_id, 0, $global); $request = new Request(); if ($has_route) { $route = new Route('/example'); if ($auth_option) { $route->setOption('_auth', $auth_option); } $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route); } $this->assertSame($applies, $authentication_manager->appliesToRoutedRequest($request, FALSE)); }
/** * Gets the add-form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getAddFormRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('add-form')) { $entity_type_id = $entity_type->id(); $parameters = [$entity_type_id => ['type' => 'entity:' . $entity_type_id]]; $route = new Route($entity_type->getLinkTemplate('add-form')); // Content entities with bundles are added via a dedicated controller. if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) { $route->setDefaults(['_controller' => 'Drupal\\custom_page\\Controller\\CustomPageAddController::addForm', '_title_callback' => 'Drupal\\custom_page\\Controller\\CustomPageAddController::getAddFormTitle'])->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}'); $parameters[$bundle_entity_type_id] = ['type' => 'entity:' . $bundle_entity_type_id]; } else { // Use the add form handler, if available, otherwise default. $operation = 'default'; if ($entity_type->getFormClass('add')) { $operation = 'add'; } $route->setDefaults(['_entity_form' => "{$entity_type_id}.{$operation}", '_title' => "Add {$entity_type->getLabel()}"])->setRequirement('_entity_create_access', $entity_type_id); } $route->setOption('parameters', $parameters)->setOption('_admin_route', TRUE); return $route; } }
/** * {@inheritdoc} */ protected function processRoute(Route $route) { // Add entity upcasting information. $parameters = $route->getOption('parameters') ?: array(); $parameters += array($this->entityType => array('type' => 'entity:' . $this->entityType)); $route->setOption('parameters', $parameters); }
protected function getTestRoute() { $route = new Route('/test/{test_revision}'); $route->setOption('parameters', ['test_revision' => ['type' => 'entity_revision:test']]); return $route; }
/** * Confirm that we can dump a route collection to the database. */ public function testDump() { $connection = Database::getConnection(); $dumper = new MatcherDumper($connection, $this->state, 'test_routes'); $route = new Route('/test/{my}/path'); $route->setOption('compiler_class', 'Drupal\\Core\\Routing\\RouteCompiler'); $collection = new RouteCollection(); $collection->add('test_route', $route); $dumper->addRoutes($collection); $this->fixtures->createTables($connection); $dumper->dump(array('provider' => 'test')); $record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", array(':name' => 'test_route'))->fetchObject(); $loaded_route = unserialize($record->route); $this->assertEqual($record->name, 'test_route', 'Dumped route has correct name.'); $this->assertEqual($record->path, '/test/{my}/path', 'Dumped route has correct pattern.'); $this->assertEqual($record->pattern_outline, '/test/%/path', 'Dumped route has correct pattern outline.'); $this->assertEqual($record->fit, 5, 'Dumped route has correct fit.'); $this->assertTrue($loaded_route instanceof Route, 'Route object retrieved successfully.'); }
/** * Gets the entity revision version history route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getRevisionHistoryRoute($entity_type) { if ($entity_type->hasLinkTemplate('version-history')) { $entity_type_id = $entity_type->id(); $route = new Route($entity_type->getLinkTemplate('version-history')); $route->addDefaults(['_controller' => '\\Drupal\\entity\\Controller\\RevisionOverviewController::revisionOverviewController', '_title' => 'Revisions']); $route->setRequirement('_entity_access_revision', "{$entity_type_id}.list"); $route->setOption('entity_type_id', $entity_type->id()); $route->setOption('parameters', [$entity_type->id() => ['type' => 'entity:' . $entity_type->id()]]); return $route; } }
/** * Tests an _entity_form route where a non-entity parameter is first. * * The {argument} preceding {entity_test} in route path, is upcasting with a * custom param converter. * * @covers ::setRouteOptions * @covers ::getControllerClass * @covers ::getEntityTypes * @covers ::setParametersFromReflection * @covers ::setParametersFromEntityInformation */ public function testSetRouteOptionsWithEntityFormRouteAndArgument() { $this->setupEntityTypes(); $route = new Route('/example/{argument}/{entity_test}', ['_entity_form' => 'entity_test.edit']); // Add {argument} parameter configuration. In this case {argument} is // upcasted by a custom param converter 'argument_type'. $route->setOption('parameters', ['argument' => ['type' => 'argument_type']]); $defaults = $route->getDefaults(); $this->entityResolverManager->setRouteOptions($route); $this->assertEquals($defaults, $route->getDefaults()); $parameters = $route->getOption('parameters'); $expect = ['argument' => ['type' => 'argument_type'], 'entity_test' => ['type' => 'entity:entity_test']]; $this->assertEquals($expect, $parameters); }
/** * Sets the upcasting information using the _entity_* route defaults. * * Supports the '_entity_view' and '_entity_form' route defaults. * * @param \Symfony\Component\Routing\Route $route * The route object. */ protected function setParametersFromEntityInformation(Route $route) { if ($entity_view = $route->getDefault('_entity_view')) { list($entity_type) = explode('.', $entity_view, 2); } elseif ($entity_form = $route->getDefault('_entity_form')) { list($entity_type) = explode('.', $entity_form, 2); } if (isset($entity_type) && isset($this->getEntityTypes()[$entity_type])) { $parameter_definitions = $route->getOption('parameters') ?: array(); // First try to figure out whether there is already a parameter upcasting // the same entity type already. foreach ($parameter_definitions as $info) { if (isset($info['type'])) { // The parameter types are in the form 'entity:$entity_type'. list(, $parameter_entity_type) = explode(':', $info['type'], 2); if ($parameter_entity_type == $entity_type) { return; } } } if (!isset($parameter_definitions[$entity_type])) { $parameter_definitions[$entity_type] = array(); } $parameter_definitions[$entity_type] += array('type' => 'entity:' . $entity_type); if (!empty($parameter_definitions)) { $route->setOption('parameters', $parameter_definitions); } } }
/** * Gets the add-form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getAddFormRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('add-form')) { $entity_type_id = $entity_type->id(); $route = new Route($entity_type->getLinkTemplate('add-form')); // Use the add form handler, if available, otherwise default. $operation = 'default'; if ($entity_type->getFormClass('add')) { $operation = 'add'; } $route->setDefaults(['_entity_form' => "{$entity_type_id}.{$operation}", 'entity_type_id' => $entity_type_id]); // If the entity has bundles, we can provide a bundle-specific title // and access requirements. $expected_parameter = $entity_type->getBundleEntityType() ?: $entity_type->getKey('bundle'); // @todo: We have to check if a route contains a bundle in its path as // test entities have inconsistent usage of "add-form" link templates. // Fix it in https://www.drupal.org/node/2699959. if (($bundle_key = $entity_type->getKey('bundle')) && strpos($route->getPath(), '{' . $expected_parameter . '}') !== FALSE) { $route->setDefault('_title_callback', EntityController::class . '::addBundleTitle'); // If the bundles are entities themselves, we can add parameter // information to the route options. if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) { $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id); $route->setDefault('bundle_parameter', $bundle_entity_type_id)->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}'); // Entity types with serial IDs can specify this in their route // requirements, improving the matching process. if ($this->getEntityTypeIdKeyType($bundle_entity_type) === 'integer') { $route->setRequirement($entity_type_id, '\\d+'); } $bundle_entity_parameter = ['type' => 'entity:' . $bundle_entity_type_id]; if ($bundle_entity_type instanceof ConfigEntityTypeInterface) { // The add page might be displayed on an admin path. Even then, we // need to load configuration overrides so that, for example, the // bundle label gets translated correctly. // @see \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter $bundle_entity_parameter['with_config_overrides'] = TRUE; } $route->setOption('parameters', [$bundle_entity_type_id => $bundle_entity_parameter]); } else { // If the bundles are not entities, the bundle key is used as the // route parameter name directly. $route->setDefault('bundle_parameter', $bundle_key)->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_key . '}'); } } else { $route->setDefault('_title_callback', EntityController::class . '::addTitle')->setRequirement('_entity_create_access', $entity_type_id); } return $route; } }
private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null) { $code = array(); //substitute the default compiler class with the compredux compiler classes $route->setOption('compiler_class', 'NineThousand\\Bundle\\NineThousandCompreduxBundle\\Component\\Routing\\CompreduxRouteCompiler'); $compiledRoute = $route->compile(); $conditions = array(); $hasTrailingSlash = false; $matches = false; if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\1#', str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), $m)) { if ($supportsRedirections && substr($m['url'], -1) === '/') { $conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); $hasTrailingSlash = true; } else { $conditions[] = sprintf("\$pathinfo === %s", var_export(str_replace('\\', '', $m['url']), true)); } } else { if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) { $conditions[] = sprintf("0 === strpos(\$pathinfo, %s)", var_export($compiledRoute->getStaticPrefix(), true)); } $regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex()); if ($supportsRedirections && $pos = strpos($regex, '/$')) { $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2); $hasTrailingSlash = true; } $conditions[] = sprintf("preg_match(%s, \$pathinfo, \$matches)", var_export($regex, true)); $matches = true; } $conditions = implode(' && ', $conditions); $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); $code[] = <<<EOF // $name if ($conditions) { EOF; if ($req = $route->getRequirement('_method')) { $methods = explode('|', strtoupper($req)); // GET and HEAD are equivalent if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { $methods[] = 'HEAD'; } if (1 === count($methods)) { $code[] = <<<EOF if (\$this->context->getMethod() != '$methods[0]') { \$allow[] = '$methods[0]'; goto $gotoname; } EOF; } else { $methods = implode('\', \'', $methods); $code[] = <<<EOF if (!in_array(\$this->context->getMethod(), array('$methods'))) { \$allow = array_merge(\$allow, array('$methods')); goto $gotoname; } EOF; } } if ($hasTrailingSlash) { $code[] = sprintf(<<<EOF if (substr(\$pathinfo, -1) !== '/') { return \$this->redirect(\$pathinfo.'/', '%s'); } EOF , $name); } if ($scheme = $route->getRequirement('_scheme')) { if (!$supportsRedirections) { throw new \LogicException('The "_scheme" requirement is only supported for route dumper that implements RedirectableUrlMatcherInterface.'); } $code[] = sprintf(<<<EOF if (\$this->context->getScheme() !== '$scheme') { return \$this->redirect(\$pathinfo, '%s', '$scheme'); } EOF , $name); } // optimize parameters array if (true === $matches && $route->getDefaults()) { $code[] = sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));" , str_replace("\n", '', var_export($route->getDefaults(), true)), $name); } elseif (true === $matches) { $code[] = sprintf(" \$matches['_route'] = '%s';", $name); $code[] = sprintf(" return \$matches;", $name); } elseif ($route->getDefaults()) { $code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($route->getDefaults(), array('_route' => $name)), true))); } else { $code[] = sprintf(" return array('_route' => '%s');", $name); } $code[] = " }"; if ($req) { $code[] = " $gotoname:"; } $code[] = ''; return $code; }
/** * @covers ::convert() * * @expectedException \Drupal\Core\ParamConverter\ParamNotConvertedException * @expectedExceptionMessage The "id" parameter was not converted for the path "/test/{id}" (route name: "test_route") */ public function testConvertMissingParam() { $route = new Route('/test/{id}'); $parameters = array('id' => array('converter' => 'test_convert')); $route->setOption('parameters', $parameters); $defaults = array(RouteObjectInterface::ROUTE_OBJECT => $route, RouteObjectInterface::ROUTE_NAME => 'test_route', 'id' => 1); $converter = $this->getMock('Drupal\\Core\\ParamConverter\\ParamConverterInterface'); $converter->expects($this->any())->method('convert')->with(1, $this->isType('array'), 'id', $this->isType('array'))->will($this->returnValue(NULL)); $this->manager->addConverter($converter, 'test_convert'); $this->manager->convert($defaults); }
/** * Sets the upcasting information using the _entity_* route defaults. * * Supports the '_entity_view' and '_entity_form' route defaults. * * @param \Symfony\Component\Routing\Route $route * The route object. */ protected function setParametersFromEntityInformation(Route $route) { if ($entity_view = $route->getDefault('_entity_view')) { list($entity_type) = explode('.', $entity_view, 2); } elseif ($entity_form = $route->getDefault('_entity_form')) { list($entity_type) = explode('.', $entity_form, 2); } // Do not add parameter information if the route does not declare a // parameter in the first place. This is the case for add forms, for // example. if (isset($entity_type) && isset($this->getEntityTypes()[$entity_type]) && strpos($route->getPath(), '{' . $entity_type . '}') !== FALSE) { $parameter_definitions = $route->getOption('parameters') ?: array(); // First try to figure out whether there is already a parameter upcasting // the same entity type already. foreach ($parameter_definitions as $info) { if (isset($info['type']) && strpos($info['type'], 'entity:') === 0) { // The parameter types are in the form 'entity:$entity_type'. list(, $parameter_entity_type) = explode(':', $info['type'], 2); if ($parameter_entity_type == $entity_type) { return; } } } if (!isset($parameter_definitions[$entity_type])) { $parameter_definitions[$entity_type] = array(); } $parameter_definitions[$entity_type] += array('type' => 'entity:' . $entity_type); if (!empty($parameter_definitions)) { $route->setOption('parameters', $parameter_definitions); } } }