diff --git a/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php b/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php index 00b29aa48b0..1988d253241 100644 --- a/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php +++ b/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php @@ -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() @@ -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)); @@ -78,4 +84,9 @@ public function setSiteAccess(SiteAccess $siteAccess = null) { $this->siteAccess = $siteAccess; } + + public function setDebug($debug = false) + { + $this->debug = $debug; + } } diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml index b24e1054230..1cdcecc7694 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml @@ -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%" diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml index 6a7125d2a8d..b906f670864 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml @@ -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: diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php index c517e24ce71..067f67a710a 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php @@ -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); } diff --git a/eZ/Publish/Core/MVC/Exception/InvalidSiteAccessException.php b/eZ/Publish/Core/MVC/Exception/InvalidSiteAccessException.php index 2ee873c1fbc..6c6df424706 100644 --- a/eZ/Publish/Core/MVC/Exception/InvalidSiteAccessException.php +++ b/eZ/Publish/Core/MVC/Exception/InvalidSiteAccessException.php @@ -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); } } diff --git a/eZ/Publish/Core/MVC/Symfony/SiteAccess/Router.php b/eZ/Publish/Core/MVC/Symfony/SiteAccess/Router.php index a416bf43fcf..cae919296ef 100644 --- a/eZ/Publish/Core/MVC/Symfony/SiteAccess/Router.php +++ b/eZ/Publish/Core/MVC/Symfony/SiteAccess/Router.php @@ -84,6 +84,11 @@ class Router implements SiteAccessRouterInterface, SiteAccessAware */ protected $request; + /** + * @var bool + */ + protected $debug; + /** * Constructor. * @@ -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; @@ -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; } /** @@ -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; @@ -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; diff --git a/eZ/Publish/Core/MVC/Symfony/SiteAccess/Tests/RouterTest.php b/eZ/Publish/Core/MVC/Symfony/SiteAccess/Tests/RouterTest.php index 124fb064dfa..85d14eef5f1 100644 --- a/eZ/Publish/Core/MVC/Symfony/SiteAccess/Tests/RouterTest.php +++ b/eZ/Publish/Core/MVC/Symfony/SiteAccess/Tests/RouterTest.php @@ -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, @@ -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 ); } @@ -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");