RouteMatch example

$this->pluginDefinition = [
      'route_name' => 'test_route',
    ];

    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('test_route')
      ->willReturn(new Route('/test-route'));

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', new Route('/'));
    $this->assertEquals([]$this->localTaskBase->getRouteParameters($route_match));
  }

  /** * @covers ::getRouteParameters */
  public function testGetRouteParametersInPluginDefinitions() {
    $this->pluginDefinition = [
      'route_name' => 'test_route',
      'route_parameters' => ['parameter' => 'example'],
    ];

    
$access = $entity->access('view all revisions', $account, TRUE);
    // Apply content_moderation's additional access logic.     // @see \Drupal\content_moderation\Access\LatestRevisionCheck::access()     if ($entity_type->getLinkTemplate('latest-version') && $entity->isLatestRevision() && isset($this->latestRevisionCheck)) {
      // The latest revision access checker only expects to be invoked by the       // routing system, which makes it necessary to fake a route match.       $routes = $this->router->getRouteCollection();
      $resource_type = $this->resourceTypeRepository->get($entity->getEntityTypeId()$entity->bundle());
      $route_name = sprintf('jsonapi.%s.individual', $resource_type->getTypeName());
      $route = $routes->get($route_name);
      $route->setOption('_content_moderation_entity_type', 'entity');
      $route_match = new RouteMatch($route_name$route['entity' => $entity]['entity' => $entity->uuid()]);
      $moderation_access_result = $this->latestRevisionCheck->access($route$route_match$account);
      $access = $access->andIf($moderation_access_result);
    }
    return $access;
  }

}
$serializer->willImplement(DecoderInterface::class);
    $serializer->decode(Json::encode(['this is an array']), 'json', Argument::type('array'))
      ->willReturn(['this is an array']);
    $this->requestHandler = new RequestHandler($serializer->reveal());
  }

  /** * @covers ::handle */
  public function testHandle() {
    $request = new Request([][][][][]['CONTENT_TYPE' => 'application/json'], Json::encode(['this is an array']));
    $route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin', 'example' => '']['_format' => 'json']))->setMethods(['GET']));

    $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class);
    $resource->get('', $request)
      ->shouldBeCalled();
    $resource->getPluginDefinition()
      ->willReturn([])
      ->shouldBeCalled();

    // Setup the configuration.     $config = $this->prophesize(RestResourceConfigInterface::class);
    $config->getResourcePlugin()->willReturn($resource->reveal());
    
$this->assertEquals(['test_access']$route2->getOption('_access_checks'));
  }

  /** * Tests \Drupal\Core\Access\AccessManager::check(). */
  public function testCheck() {
    $route_matches = [];

    // Construct route match objects.     foreach ($this->routeCollection->all() as $route_name => $route) {
      $route_matches[$route_name] = new RouteMatch($route_name$route[][]);
    }

    // Check route access without any access checker defined yet.     foreach ($route_matches as $route_match) {
      $this->assertEquals(FALSE, $this->accessManager->check($route_match$this->account));
      $this->assertEquals(AccessResult::neutral()$this->accessManager->check($route_match$this->account, NULL, TRUE));
    }

    $this->setupAccessChecker();

    // An access checker got setup, but the routes haven't been setup using

  public function testGetAddRouteName() {
    $result = $this->configNamesMapper->getAddRouteName();
    $expected = 'config_translation.item.add.' . $this->pluginDefinition['base_route_name'];
    $this->assertSame($expected$result);
  }

  /** * Tests ConfigNamesMapper::getAddRouteParameters(). */
  public function testGetAddRouteParameters() {
    $route_match = new RouteMatch('example', new Route('/test/{langcode}')['langcode' => 'xx']);
    $this->configNamesMapper->populateFromRouteMatch($route_match);

    $expected = ['langcode' => 'xx'];
    $result = $this->configNamesMapper->getAddRouteParameters();
    $this->assertSame($expected$result);
  }

  /** * Tests ConfigNamesMapper::getAddRoute(). */
  public function testGetAddRoute() {
    

  public function checkNamedRoute($route_name, array $parameters = [], AccountInterface $account = NULL, $return_as_object = FALSE) {
    try {
      $route = $this->routeProvider->getRouteByName($route_name);

      // ParamConverterManager relies on the route name and object being       // available from the parameters array.       $parameters[RouteObjectInterface::ROUTE_NAME] = $route_name;
      $parameters[RouteObjectInterface::ROUTE_OBJECT] = $route;
      $upcasted_parameters = $this->paramConverterManager->convert($parameters + $route->getDefaults());

      $route_match = new RouteMatch($route_name$route$upcasted_parameters$parameters);
      return $this->check($route_match$account, NULL, $return_as_object);
    }
    catch (RouteNotFoundException $e) {
      // Cacheable until extensions change.       $result = AccessResult::forbidden()->addCacheTags(['config:core.extension']);
      return $return_as_object ? $result : $result->isAllowed();
    }
    catch (ParamNotConvertedException $e) {
      // Uncacheable because conversion of the parameter may not have been       // possible due to dynamic circumstances.       $result = AccessResult::forbidden()->setCacheMaxAge(0);
      

  public function testGetRuntimeContexts() {
    // Create term.     $vocabulary = $this->createVocabulary();
    $term = $this->createTerm($vocabulary);

    // Create RouteMatch from term entity.     $url = $term->toUrl();
    $route_provider = \Drupal::service('router.route_provider');
    $route = $route_provider->getRouteByName($url->getRouteName());
    $route_match = new RouteMatch($url->getRouteName()$route[
      'taxonomy_term' => $term,
    ]);

    // Initiate TermRouteContext with RouteMatch.     $provider = new TermRouteContext($route_match);

    $runtime_contexts = $provider->getRuntimeContexts([]);
    $this->assertArrayHasKey('taxonomy_term', $runtime_contexts);
    $this->assertTrue($runtime_contexts['taxonomy_term']->hasContextValue());
  }

}
public function testAddingConfigNames() {
    // Get a config names mapper.     $mappers = \Drupal::service('plugin.manager.config_translation.mapper')->getMappers();
    $mapper = $mappers['system.site_information_settings'];

    // Test that it doesn't contain a config name from config_translation_test.     $config_names = $mapper->getConfigNames();
    $this->assertNotContains('config_translation_test.content', $config_names);

    // Call populateFromRouteMatch() to dispatch the "config mapper populate"     // event.     $mapper->populateFromRouteMatch(new RouteMatch('test', new Route('/')));

    // Test that it contains the new config name from config_translation_test.     $config_names = $mapper->getConfigNames();
    $this->assertContains('config_translation_test.content', $config_names);
  }

}
->shouldBeCalled()
      ->willReturn($workflow_entity_access_result);

    $route = new Route('', [
      'workflow' => NULL,
      'workflow_transition' => NULL,
      'workflow_state' => NULL,
    ][
      '_workflow_access' => $route_requirement,
    ]);
    $route_match_params = ['workflow' => $workflow->reveal()] + $route_parameters;
    $route_match = new RouteMatch(NULL, $route$route_match_params);

    $access_check = new WorkflowStateTransitionOperationsAccessCheck();
    $account = $this->prophesize(AccountInterface::class);
    $this->assertEquals($workflow_entity_access_result$access_check->access($route_match$account->reveal()));
  }

  /** * Test cases for ::testAccess. */
  public function accessTestCases() {
    return [
      
/** * @coversDefaultClass \Drupal\Core\Routing\RouteMatch * @group Routing */
class RouteMatchTest extends RouteMatchTestBase {

  /** * {@inheritdoc} */
  protected function getRouteMatch($name, Route $route, array $parameters, array $raw_parameters) {
    return new RouteMatch($name$route$parameters$raw_parameters);
  }

  /** * @covers ::createFromRequest * @covers ::__construct */
  public function testRouteMatchFromRequest() {
    $request = new Request();

    // A request that hasn't been routed yet.     $route_match = RouteMatch::createFromRequest($request);
    
public function testToRenderArray() {
    $this->expectDeprecation('Drupal\Core\Url::toRenderArray() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3342977');
    $url = Url::fromRoute('entity.node.canonical', ['node' => 3]);
    $this->assertIsArray($url->toRenderArray());
  }

  /** * Tests the fromRouteMatch() method. */
  public function testFromRouteMatch() {
    $route = new Route('/test-route/{foo}');
    $route_match = new RouteMatch('test_route', $route['foo' => (object) [1]]['foo' => 1]);
    $url = Url::fromRouteMatch($route_match);
    $this->assertSame('test_route', $url->getRouteName());
    $this->assertEquals(['foo' => '1']$url->getRouteParameters());
  }

  /** * Data provider for testing entity URIs. */
  public function providerTestEntityUris() {
    return [
      [
        

  public function testPathChangedHelper() {
    $route = \Drupal::service('router.route_provider')->getRouteByName('path.changed.bc');
    $raw_parameters = [
      'block_type' => 'test_block_type',
    ];
    $query = [
      'destination' => 'admin/structure/block',
      'plugin_id' => 'some_block_config',
    ];
    $helper = new PathChangedHelper(
      new RouteMatch('path.changed.bc', $route[]$raw_parameters),
      new Request($query)
    );

    // Assert that oldPath() returns the internal path for path.changed.bc.     $this->assertEquals('old/path/test_block_type', $helper->oldPath());
    // Assert that newPath() returns the internal path for path.changed.     $this->assertEquals('new/path/test_block_type', $helper->newPath());
    // Assert that redirect() returns a RedirectResponse for the absolute URL of     // path.changed, and the query string comes from the Request object with the     // destination parameter removed.     $redirect = $helper->redirect();
    

  public function testMultilingualBookBreadcrumbBuilder(string $langcode) {
    $this->setCurrentLanguage($langcode);
    // Test a level 3 node.     $nid = 7;
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::load($nid);
    $route = new Route('/node/{node}');
    $route_match = new RouteMatch('entity.node.canonical', $route['node' => $node]['node' => $nid]);
    /** @var \Drupal\book\BookBreadcrumbBuilder $bbb */
    $bbb = $this->container->get('book.breadcrumb');
    $links = $bbb->build($route_match)->getLinks();
    $link = array_shift($links);
    $rendered_link = Link::fromTextAndUrl($link->getText()$link->getUrl())->toString();
    $this->assertStringContainsString("http://$langcode.book.test.domain/", $rendered_link);
    $link = array_shift($links);
    $this->assertNodeLinkIsCorrectlyTranslated(1, $link->getText()$link->getUrl()$langcode);
    $link = array_shift($links);
    $this->assertNodeLinkIsCorrectlyTranslated(3, $link->getText()$link->getUrl()$langcode);
    $this->assertEmpty($links);
  }
$this->assertNull($result->get('key_controlled_by_plugin_collection'));
  }

  /** * Tests EntityForm::getEntityFromRouteMatch() for edit and delete forms. * * @covers ::getEntityFromRouteMatch */
  public function testGetEntityFromRouteMatchEditDelete() {
    $entity = $this->prophesize(EntityInterface::class)->reveal();
    $id = $this->entityType->id();
    $route_match = new RouteMatch(
      'test_route',
      new Route('/entity-test/manage/{' . $id . '}/edit'),
      [$id => $entity],
      [$id => 1]
    );
    $actual = $this->entityForm->getEntityFromRouteMatch($route_match$id);
    $this->assertEquals($entity$actual);
  }

  /** * Tests EntityForm::getEntityFromRouteMatch() for add forms without a bundle. * * @covers ::getEntityFromRouteMatch */
->method('applies')
      ->willReturn(TRUE);

    $this->container->set('test_negotiator', $negotiator);

    $negotiators = ['test_negotiator'];

    $this->themeAccessCheck->expects($this->any())
      ->method('checkAccess')
      ->willReturn(TRUE);

    $route_match = new RouteMatch('test_route', new Route('/test-route')[][]);
    $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match);

    $this->assertEquals('example_test', $theme);
  }

  /** * Tests determining with two negotiators checking the priority. * * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */
  public function testDetermineActiveThemeWithPriority() {
    
Home | Imprint | This part of the site doesn't use cookies.