Skip to content

Commit

Permalink
Merged branch '1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jun 2, 2021
2 parents ee2cef3 + 8768a71 commit 7a37c2d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/**
* Decorator of FOSJsRouting routes extractor.
* Ensures that base URL contains the SiteAccess part when applicable.
*
* @internal
*/
class ExposedRoutesExtractor implements ExposedRoutesExtractorInterface
{
Expand Down Expand Up @@ -44,9 +46,13 @@ public function getRoutes(): RouteCollection
*/
public function getBaseUrl(): string
{
$masterRequest = $this->requestStack->getMasterRequest();
$baseUrl = $this->innerExtractor->getBaseUrl();
$siteAccess = $masterRequest->attributes->get('siteaccess');
$request = $this->requestStack->getMasterRequest();
if ($request === null) {
return $baseUrl;
}

$siteAccess = $request->attributes->get('siteaccess');
if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\URILexer) {
$baseUrl .= $siteAccess->matcher->analyseLink('');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\Tests\Routing\JsRouting;

use eZ\Bundle\EzPublishCoreBundle\Routing\JsRouting\ExposedRoutesExtractor;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @covers \eZ\Bundle\EzPublishCoreBundle\Routing\JsRouting\ExposedRoutesExtractor
*
* @internal
*/
final class ExposedRoutesExtractorTest extends TestCase
{
private const BASE_URL = '/foo';

public function getDataForTestGetBaseUrl(): iterable
{
yield 'CLI' => [
// no master request in a stack
null,
self::BASE_URL,
];

yield 'No SiteAccess' => [
new Request(),
self::BASE_URL,
];

$siteAccess = new SiteAccess(
'test',
SiteAccess\Matcher\HostText::class,
new SiteAccess\Matcher\HostText([])
);
yield 'SiteAccess w/o URI Lexer matcher' => [
new Request([], [], ['siteaccess' => $siteAccess]),
self::BASE_URL,
];

$siteAccess = new SiteAccess(
'test',
SiteAccess\Matcher\URIText::class,
new SiteAccess\Matcher\URIText(['prefix' => 'bar'])
);
yield 'SiteAccess with URI Lexer matcher' => [
new Request([], [], ['siteaccess' => $siteAccess]),
self::BASE_URL . '/bar/',
];
}

/**
* @dataProvider getDataForTestGetBaseUrl
*/
public function testGetBaseUrl(?Request $masterRequest, string $expectedBaseUrl): void
{
$innerExtractor = $this->createMock(ExposedRoutesExtractorInterface::class);
$requestStack = $this->createMock(RequestStack::class);

$innerExtractor->method('getBaseUrl')->willReturn(self::BASE_URL);
$requestStack->method('getMasterRequest')->willReturn($masterRequest);

$extractor = new ExposedRoutesExtractor($innerExtractor, $requestStack);

self::assertSame($expectedBaseUrl, $extractor->getBaseUrl());
}
}

0 comments on commit 7a37c2d

Please sign in to comment.