setUrlGenerator example



    $final_html = $final_html ?? $link_html;

    $this->setUpUrlIntegrationServices();
    $this->setupDisplayWithEmptyArgumentsAndFields();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'value']);

    $expected_url->setOptions($expected_url->getOptions() + static::DEFAULT_URL_OPTIONS);
    $expected_link_url->setUrlGenerator($this->urlGenerator);

    $expected_url_options = $expected_url->getOptions();
    unset($expected_url_options['attributes']);

    $this->urlGenerator->expects($this->once())
      ->method('generateFromRoute')
      ->with($expected_url->getRouteName()$expected_url->getRouteParameters()$expected_url_options, TRUE)
      ->willReturn((new GeneratedUrl())->setGeneratedUrl($url_path));

    $result = $field->advancedRender($row);
    $this->assertEquals($final_html$result);
  }
// External URL     $data[] = [Url::fromUri('https://www.drupal.org'), TRUE];

    // Existing routed URL.     $url = Url::fromRoute('example.existing_route');

    $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
    $url_generator->expects($this->any())
      ->method('generateFromRoute')
      ->with('example.existing_route', [][])
      ->willReturn('/example/existing');
    $url->setUrlGenerator($url_generator);

    $data[] = [$url, TRUE];

    // Non-existent routed URL.     $url = Url::fromRoute('example.not_existing_route');

    $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
    $url_generator->expects($this->any())
      ->method('generateFromRoute')
      ->with('example.not_existing_route', [][])
      ->willThrowException(new RouteNotFoundException());
    

  public function testGenerateHrefs($route_name, array $parameters$absolute$expected_url) {
    $this->urlGenerator->expects($this->once())
      ->method('generateFromRoute')
      ->with($route_name$parameters['absolute' => $absolute] + $this->defaultOptions)
      ->willReturn((new GeneratedUrl())->setGeneratedUrl($expected_url));
    $this->moduleHandler->expects($this->once())
      ->method('alter');

    $url = new Url($route_name$parameters['absolute' => $absolute]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator->generate('Test', $url);
    $this->assertLink([
      'attributes' => ['href' => $expected_url],
    ]$result);
  }

  /** * Tests the generate() method with a route. * * @covers ::generate */
  

  public function generate($text, Url $url) {
    // The link generator should not modify the original URL object, this     // ensures consistent rendering.     // @see https://www.drupal.org/node/2842399     $url = clone $url;

    // Performance: avoid Url::toString() needing to retrieve the URL generator     // service from the container.     $url->setUrlGenerator($this->urlGenerator);

    if (is_array($text)) {
      $text = $this->renderer->render($text);
    }

    // Start building a structured representation of our link to be altered later.     $variables = [
      'text' => $text,
      'url' => $url,
      'options' => $url->getOptions(),
    ];

    

  public function testUrlBubbleableMetadataBubbling($collect_bubbleable_metadata$invocations, array $options) {
    $self = $this;

    $this->renderer->expects($this->exactly($invocations))
      ->method('render')
      ->willReturnCallback(function D$build) {
        $this->assertArrayHasKey('#cache', $build);
      });

    $url = new Url('test_1', []$options);
    $url->setUrlGenerator($this->generator);
    $url->toString($collect_bubbleable_metadata);
  }

  /** * Data provider for ::testUrlBubbleableMetadataBubbling(). */
  public function providerUrlBubbleableMetadataBubbling() {
    return [
      // No bubbling when bubbleable metadata is collected.       [TRUE, 0, []],
      // Bubbling when bubbleable metadata is not collected.
$map[] = ['node_view', ['node' => '1'], '/node/1'];
    $map[] = ['node_edit', ['node' => '2'], '/node/2/edit'];

    foreach ($urls as $url) {
      // Clone the URL so that there is no leak of internal state into the       // other ones.       $url = clone $url;
      $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
      $url_generator->expects($this->once())
        ->method('getPathFromRoute')
        ->willReturnMap($map);
      $url->setUrlGenerator($url_generator);

      $url->getInternalPath();
      $url->getInternalPath();
    }
  }

  /** * Tests the toString() method. * * @param \Drupal\Core\Url[] $urls * An array of Url objects. * * @depends testUrlFromRequest * * @covers ::toString */
Home | Imprint | This part of the site doesn't use cookies.