Skip to content

Commit

Permalink
Correctly support absolute URLs with empty prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 21, 2020
1 parent ded7d5c commit 64b388e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core-bundle/src/Routing/Candidates/PageCandidates.php
Expand Up @@ -90,8 +90,12 @@ private function addRegexQuery(QueryBuilder $queryBuilder, string $pathInfo): bo
$paths[] = '(?P<'.$type.'>'.substr($pathRegex, 2, strrpos($pathRegex, '$') - 2).')';
}

$prefixes = array_map(static function ($prefix) {
return $prefix ? preg_quote('/'.$prefix, '#') : '';
}, $this->urlPrefixes);

preg_match_all(
'#^(/'.implode('|/', array_map('preg_quote', $this->urlPrefixes)).')('.implode('|', $paths).')('.implode('|', array_map('preg_quote', $this->urlSuffixes)).')'.'$#sD',
'#^('.implode('|', $prefixes).')('.implode('|', $paths).')('.implode('|', array_map('preg_quote', $this->urlSuffixes)).')'.'$#sD',
$pathInfo,
$matches
);
Expand Down
34 changes: 34 additions & 0 deletions core-bundle/tests/Routing/Candidates/CandidatesTest.php
Expand Up @@ -428,6 +428,40 @@ public function testIncluesPageWithAbsolutePathAndSuffix(): void
$this->assertSame(['bar/baz', 'bar', 'bar/baz.html', 15], $candidates->getCandidates($request));
}

public function testIncluesPageWithAbsolutePathWithoutPrefix(): void
{
$request = $this->mockRequest('/bar/baz.html');

$queryBuilder = $this->createMock(QueryBuilder::class);
$queryBuilder
->expects($this->once())
->method('orWhere')
->with('type IN (:types)')
->willReturnSelf()
;

$queryBuilder
->expects($this->once())
->method('setParameter')
->with('types', ['foo', 'bar'], Connection::PARAM_STR_ARRAY)
->willReturnSelf()
;

$pageRegistry = $this->mockPageRegistry(['foo', ''], ['.html']);
$pageRegistry
->expects($this->once())
->method('getPathRegex')
->willReturn([
'foo' => '#^/bar/[a-z]+$#sD',
'bar' => '#^/bar$#sD',
])
;

$candidates = new PageCandidates($this->mockConnection($queryBuilder), $pageRegistry);

$this->assertSame(['bar/baz', 'bar', 15], $candidates->getCandidates($request));
}

/**
* @return Request&MockObject
*/
Expand Down

0 comments on commit 64b388e

Please sign in to comment.