Skip to content

Commit

Permalink
Merge branch '7.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
glye committed Nov 21, 2018
2 parents 60dc511 + a556b5b commit 6751ff3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
Expand Up @@ -43,14 +43,20 @@ class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAwar
*/
private $siteAccess;

/**
* @var bool
*/
private $debug;

/**
* ConsoleCommandListener constructor.
*/
public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher)
public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher, $debug = false)
{
$this->defaultSiteAccessName = $defaultSiteAccessName;
$this->siteAccessList = $siteAccessList;
$this->eventDispatcher = $eventDispatcher;
$this->debug = $debug;
}

public static function getSubscribedEvents()
Expand All @@ -68,7 +74,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event)
$this->siteAccess->matchingType = 'cli';

if (!in_array($this->siteAccess->name, $this->siteAccessList)) {
throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType);
throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType, $this->debug);
}

$this->eventDispatcher->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, new ScopeChangeEvent($this->siteAccess));
Expand All @@ -78,4 +84,9 @@ public function setSiteAccess(SiteAccess $siteAccess = null)
{
$this->siteAccess = $siteAccess;
}

public function setDebug($debug = false)
{
$this->debug = $debug;
}
}
1 change: 1 addition & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml
Expand Up @@ -81,6 +81,7 @@ services:
- "%ezpublish.siteaccess.match_config%"
- "%ezpublish.siteaccess.list%"
- "%ezpublish.siteaccess.class%"
- "%kernel.debug%"

ezpublish.siteaccess_listener:
class: "%ezpublish.siteaccess_listener.class%"
Expand Down
6 changes: 5 additions & 1 deletion eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
Expand Up @@ -63,7 +63,11 @@ services:

ezpublish.console_event_listener:
class: "%ezpublish.console_event_listener.class%"
arguments: ["%ezpublish.siteaccess.default%", "%ezpublish.siteaccess.list%", "@event_dispatcher"]
arguments:
- "%ezpublish.siteaccess.default%"
- "%ezpublish.siteaccess.list%"
- "@event_dispatcher"
- "%kernel.debug%"
calls:
- [setSiteAccess, ["@ezpublish.siteaccess"]]
tags:
Expand Down
Expand Up @@ -84,13 +84,29 @@ public function testGetSubscribedEvents()

/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\. Valid siteaccesses are/
*/
public function testInvalidSiteAccess()
public function testInvalidSiteAccessDev()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(true);
$this->listener->onConsoleCommand($event);
}

/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\.$/
*/
public function testInvalidSiteAccessProd()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(false);
$this->listener->onConsoleCommand($event);
}

Expand Down
10 changes: 8 additions & 2 deletions eZ/Publish/Core/MVC/Exception/InvalidSiteAccessException.php
Expand Up @@ -19,9 +19,15 @@ class InvalidSiteAccessException extends RuntimeException
* @param string $siteAccess The invalid siteaccess
* @param array $siteAccessList All valid siteaccesses, as a regular array
* @param string $matchType How $siteAccess was matched
* @param bool $debug If true, Symfony environment is a debug one (like 'dev')
*/
public function __construct($siteAccess, array $siteAccessList, $matchType)
public function __construct($siteAccess, array $siteAccessList, $matchType, $debug = false)
{
parent::__construct("Invalid siteaccess '$siteAccess', matched by $matchType. Valid siteaccesses are " . implode(', ', $siteAccessList));
$message = "Invalid siteaccess '$siteAccess', matched by $matchType.";
if ($debug) {
$message .= ' Valid siteaccesses are ' . implode(', ', $siteAccessList);
}

parent::__construct($message);
}
}
13 changes: 10 additions & 3 deletions eZ/Publish/Core/MVC/Symfony/SiteAccess/Router.php
Expand Up @@ -84,6 +84,11 @@ class Router implements SiteAccessRouterInterface, SiteAccessAware
*/
protected $request;

/**
* @var bool
*/
protected $debug;

/**
* Constructor.
*
Expand All @@ -93,8 +98,9 @@ class Router implements SiteAccessRouterInterface, SiteAccessAware
* @param array $siteAccessesConfiguration
* @param array $siteAccessList
* @param string|null $siteAccessClass
* @param bool $debug
*/
public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInterface $logger, $defaultSiteAccess, array $siteAccessesConfiguration, array $siteAccessList, $siteAccessClass = null)
public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInterface $logger, $defaultSiteAccess, array $siteAccessesConfiguration, array $siteAccessList, $siteAccessClass = null, $debug = false)
{
$this->matcherBuilder = $matcherBuilder;
$this->logger = $logger;
Expand All @@ -103,6 +109,7 @@ public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInter
$this->siteAccessList = array_fill_keys($siteAccessList, true);
$this->siteAccessClass = $siteAccessClass ?: 'eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess';
$this->request = new SimplifiedRequest();
$this->debug = $debug;
}

/**
Expand Down Expand Up @@ -139,7 +146,7 @@ public function match(SimplifiedRequest $request)
$siteaccessName = $request->headers['x-siteaccess'][0];
if (!isset($this->siteAccessList[$siteaccessName])) {
unset($this->siteAccess);
throw new InvalidSiteAccessException($siteaccessName, array_keys($this->siteAccessList), 'X-Siteaccess request header');
throw new InvalidSiteAccessException($siteaccessName, array_keys($this->siteAccessList), 'X-Siteaccess request header', $this->debug);
}

$this->siteAccess->name = $siteaccessName;
Expand All @@ -153,7 +160,7 @@ public function match(SimplifiedRequest $request)
if ($siteaccessEnvName !== false) {
if (!isset($this->siteAccessList[$siteaccessEnvName])) {
unset($this->siteAccess);
throw new InvalidSiteAccessException($siteaccessEnvName, array_keys($this->siteAccessList), 'EZPUBLISH_SITEACCESS Environment variable');
throw new InvalidSiteAccessException($siteaccessEnvName, array_keys($this->siteAccessList), 'EZPUBLISH_SITEACCESS Environment variable', $this->debug);
}

$this->siteAccess->name = $siteaccessEnvName;
Expand Down
26 changes: 23 additions & 3 deletions eZ/Publish/Core/MVC/Symfony/SiteAccess/Tests/RouterTest.php
Expand Up @@ -38,7 +38,12 @@ protected function tearDown()
parent::tearDown();
}

public function testConstruct()
public function testConstructDebug()
{
return $this->testConstruct(true);
}

public function testConstruct($debug = false)
{
return new Router(
$this->matcherBuilder,
Expand Down Expand Up @@ -77,7 +82,9 @@ public function testConstruct()
),
),
),
array('first_sa', 'second_sa', 'third_sa', 'fourth_sa', 'headerbased_sa', 'fr_eng', 'fr_us')
array('first_sa', 'second_sa', 'third_sa', 'fourth_sa', 'headerbased_sa', 'fr_eng', 'fr_us'),
null,
$debug
);
}

Expand All @@ -96,11 +103,24 @@ public function testMatch(SimplifiedRequest $request, $siteAccess, Router $route
$router->setSiteAccess();
}

/**
* @depends testConstructDebug
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\. Valid siteaccesses are/
*/
public function testMatchWithDevEnvFail(Router $router)
{
$saName = 'foobar_sa';
putenv("EZPUBLISH_SITEACCESS=$saName");
$router->match(new SimplifiedRequest());
}

/**
* @depends testConstruct
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\.$/
*/
public function testMatchWithEnvFail(Router $router)
public function testMatchWithProdEnvFail(Router $router)
{
$saName = 'foobar_sa';
putenv("EZPUBLISH_SITEACCESS=$saName");
Expand Down

0 comments on commit 6751ff3

Please sign in to comment.