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

merge-requests/3547/merge
Alex Pott 2023-03-09 09:03:29 +00:00
parent 905966164b
commit 66500ab088
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 16 additions and 9 deletions

View File

@ -273,16 +273,15 @@ class UrlGenerator implements UrlGeneratorInterface {
$route = $this->getRoute($name);
$generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
$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']) : '';
$fragment = '';
if (isset($options['fragment'])) {
if (($fragment = trim($options['fragment'])) != '') {
$fragment = '#' . $fragment;
}
}
$url = $query . $fragment;
return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
}
@ -335,6 +334,13 @@ class UrlGenerator implements UrlGeneratorInterface {
$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'];

View File

@ -538,18 +538,19 @@ class UrlGeneratorTest extends UnitTestCase {
* 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));
}
/**