urlGenerator example

$this->drupalGet('user/register');
    $this->assertSession()->titleEquals('Create new account' . $title_suffix);

    $this->drupalGet('user/password');
    $this->assertSession()->titleEquals('Reset your password' . $title_suffix);

    // Check the page title for registered users is "My Account" in menus.     $this->drupalLogin($this->drupalCreateUser());
    // After login, the client is redirected to /user.     $this->assertSession()->linkExists('My account', 0, "Page title of /user is 'My Account' in menus for registered users");
    $this->assertSession()->linkByHrefExists(\Drupal::urlGenerator()->generate('user.page'), 0);
  }

  /** * Ensures that logout URL redirects an anonymous user to the front page. */
  public function testAnonymousLogout() {
    $this->drupalGet('user/logout');
    $this->assertSession()->addressEquals('/');
    $this->assertSession()->statusCodeEquals(200);

    // The redirection shouldn't affect other pages.
'#type' => 'more_link',
          '#url' => Url::fromRoute('router_test.1'),
        ],
        'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('router_test.1')->toString() . '" and text()="More"]',
      ],
      [
        'name' => "#type 'more_link' anchor tag with a route",
        'value' => [
          '#type' => 'more_link',
          '#url' => Url::fromRoute('router_test.1'),
        ],
        'expected' => '//div[@class="more-link"]/a[@href="' . \Drupal::urlGenerator()->generate('router_test.1') . '" and text()="More"]',
      ],
      [
        'name' => "#type 'more_link' anchor tag with an absolute path",
        'value' => [
          '#type' => 'more_link',
          '#url' => Url::fromRoute('system.admin_content'),
          '#options' => ['absolute' => TRUE],
        ],
        'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('system.admin_content')->setAbsolute()->toString() . '" and text()="More"]',
      ],
      [
        

  protected $urlGenerator;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->urlGenerator = \Drupal::urlGenerator();
  }

  /** * Tests the output process. */
  public function testProcessOutbound() {
    $expected_cacheability = (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT);

    $request_stack = \Drupal::requestStack();
    /** @var \Symfony\Component\Routing\RequestContext $request_context */
    $request_context = \Drupal::service('router.request_context');

    

  public function toString($collect_bubbleable_metadata = FALSE) {
    if ($this->unrouted) {
      return $this->unroutedUrlAssembler()->assemble($this->getUri()$this->getOptions()$collect_bubbleable_metadata);
    }

    return $this->urlGenerator()->generateFromRoute($this->getRouteName()$this->getRouteParameters()$this->getOptions()$collect_bubbleable_metadata);
  }

  /** * Returns the route information for a render array. * * @return array * An associative array suitable for a render array. * * @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 */

  protected $urlGenerator;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->urlGenerator = \Drupal::urlGenerator();
  }

  /** * Tests the output process. */
  public function testProcessOutbound() {
    $expected_cacheability = (new BubbleableMetadata())
      ->addCacheContexts(['route'])
      ->setCacheMaxAge(Cache::PERMANENT);

    $request_stack = \Drupal::requestStack();
    
$this->setMockContainerService('token');
    $this->assertNotNull(\Drupal::token());
  }

  /** * Tests the urlGenerator() method. * * @covers ::urlGenerator */
  public function testUrlGenerator() {
    $this->setMockContainerService('url_generator');
    $this->assertNotNull(\Drupal::urlGenerator());
  }

  /** * Tests the linkGenerator() method. * * @covers ::linkGenerator */
  public function testLinkGenerator() {
    $this->setMockContainerService('link_generator');
    $this->assertNotNull(\Drupal::linkGenerator());
  }

  
$storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
    foreach ($this->langcodes as $langcode) {
      if ($entity->hasTranslation($langcode)) {
        $language = new Language(['id' => $langcode]);
        // Request the front page in this language and assert that the right         // translation shows up in the shortcut list with the right path.         $this->drupalGet('<front>', ['language' => $language]);
        $expected_path = \Drupal::urlGenerator()->generateFromRoute('user.page', []['language' => $language]);
        $label = $entity->getTranslation($langcode)->label();
        $this->assertSession()->elementExists('xpath', "//nav[contains(@class, 'toolbar-lining')]/ul[@class='toolbar-menu']/li/a[contains(@href, '{$expected_path}') and normalize-space(text())='{$label}']");
      }
    }
  }

  /** * {@inheritdoc} */
  protected function doTestTranslationEdit() {
    $storage = $this->container->get('entity_type.manager')
      
'key' => 'value',
        ],
      ],
    ];

    $expected_links = '';
    $expected_links .= '<ul id="somelinks">';
    $expected_links .= '<li><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
    $expected_links .= '<li>' . Html::escape('Plain "text"') . '</li>';
    $expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
    $expected_links .= '<li><a href="' . Url::fromRoute('<front>')->toString() . '">' . Html::escape('Front page') . '</a></li>';
    $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
    $query = ['key' => 'value'];
    $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
    $expected_links .= '</ul>';

    // Verify that passing a string as heading works.     $variables['heading'] = 'Links heading';
    $expected_heading = '<h2>Links heading</h2>';
    $expected = $expected_heading . $expected_links;
    $this->assertThemeOutput('links', $variables$expected);

    // Restore the original request's query.
Home | Imprint | This part of the site doesn't use cookies.