Verified Commit 0769b54d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2881077 by smustgrave, sardara, amcgowanca, alexpott, dawehner:...

Issue #2881077 by smustgrave, sardara, amcgowanca, alexpott, dawehner: Outbound path processors cannot override the specified URL fragment

(cherry picked from commit 66500ab0)
parent da1de2ff
Loading
Loading
Loading
Loading
+13 −7
Original line number Diff line number Diff line
@@ -270,16 +270,15 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle
    $route = $this->getRoute($name);
    $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;

    // Generate a relative URL having no path, just query string and fragment.
    if ($route->getOption('_no_path')) {
      $query = $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';
      $fragment = '';
      if (isset($options['fragment'])) {
        if (($fragment = trim($options['fragment'])) != '') {
          $fragment = '#' . $fragment;
        }
      }

    // Generate a relative URL having no path, just query string and fragment.
    if ($route->getOption('_no_path')) {
      $query = $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';
      $url = $query . $fragment;
      return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
    }
@@ -329,6 +328,13 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle

    $query = $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';

    $fragment = '';
    if (isset($options['fragment'])) {
      if (($fragment = trim($options['fragment'])) != '') {
        $fragment = '#' . $fragment;
      }
    }

    // The base_url might be rewritten from the language rewrite in domain mode.
    if (isset($options['base_url'])) {
      $base_url = $options['base_url'];
+3 −2
Original line number Diff line number Diff line
@@ -512,18 +512,19 @@ public function providerTestNoPath() {
   * Note: We use absolute covers to let
   * \Drupal\Tests\Core\Render\MetadataBubblingUrlGeneratorTest work.
   */
  public function testGenerateWithPathProcessorChangingQueryParameter() {
  public function testGenerateWithPathProcessorChangingOptions() {
    $path_processor = $this->createMock(OutboundPathProcessorInterface::CLASS);
    $path_processor->expects($this->atLeastOnce())
      ->method('processOutbound')
      ->willReturnCallback(function ($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
        $options['query'] = ['zoo' => 5];
        $options['fragment'] = 'foo';
        return $path;
      });
    $this->processorManager->addOutbound($path_processor);

    $options = [];
    $this->assertGenerateFromRoute('test_2', ['narf' => 5], $options, '/goodbye/cruel/world?zoo=5', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT));
    $this->assertGenerateFromRoute('test_2', ['narf' => 5], $options, '/goodbye/cruel/world?zoo=5#foo', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT));
  }

  /**