diff --git a/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php b/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php index 0ac626e8010..b9dab6d3708 100644 --- a/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php +++ b/eZ/Bundle/EzPublishCoreBundle/EventListener/ConsoleCommandListener.php @@ -10,6 +10,7 @@ */ namespace eZ\Bundle\EzPublishCoreBundle\EventListener; +use eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException; use eZ\Publish\Core\MVC\Symfony\Event\ScopeChangeEvent; use eZ\Publish\Core\MVC\Symfony\MVCEvents; use eZ\Publish\Core\MVC\Symfony\SiteAccess; @@ -29,6 +30,11 @@ class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAwar */ private $defaultSiteAccessName; + /** + * @var array + */ + private $siteAccessList; + /** * @var EventDispatcherInterface */ @@ -42,9 +48,10 @@ class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAwar /** * ConsoleCommandListener constructor. */ - public function __construct($defaultSiteAccessName, EventDispatcherInterface $eventDispatcher) + public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher) { $this->defaultSiteAccessName = $defaultSiteAccessName; + $this->siteAccessList = $siteAccessList; $this->eventDispatcher = $eventDispatcher; } @@ -59,11 +66,13 @@ public static function getSubscribedEvents() public function onConsoleCommand(ConsoleCommandEvent $event) { - $siteAccessName = $event->getInput()->getParameterOption('--siteaccess', null); - - $this->siteAccess->name = $siteAccessName ?: $this->defaultSiteAccessName; + $this->siteAccess->name = $event->getInput()->getParameterOption('--siteaccess', $this->defaultSiteAccessName); $this->siteAccess->matchingType = 'cli'; + if (!in_array($this->siteAccess->name, $this->siteAccessList)) { + throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType); + } + $this->eventDispatcher->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, new ScopeChangeEvent($this->siteAccess)); } diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml index 16174a9aaa6..3bd50dcff64 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml @@ -59,7 +59,7 @@ services: ezpublish.console_event_listener: class: %ezpublish.console_event_listener.class% - arguments: [%ezpublish.siteaccess.default.name%, @event_dispatcher] + arguments: [%ezpublish.siteaccess.default%, %ezpublish.siteaccess.list%, @event_dispatcher] calls: - [setSiteAccess, [@ezpublish.siteaccess]] tags: diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php new file mode 100644 index 00000000000..53a7f2b7c8b --- /dev/null +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php @@ -0,0 +1,117 @@ +siteAccess = new SiteAccess(); + $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->listener = new ConsoleCommandListener('default', $this->siteAccessList, $this->dispatcher); + $this->listener->setSiteAccess($this->siteAccess); + $this->dispatcher->addSubscriber($this->listener); + $this->command = new Command('test:siteaccess'); + $this->inputDefinition = new InputDefinition(array(new InputOption('siteaccess', null, InputOption::VALUE_OPTIONAL))); + $this->testOutput = new TestOutput(Output::VERBOSITY_QUIET, true); + } + + public function testGetSubscribedEvents() + { + $this->assertSame( + array( + ConsoleEvents::COMMAND => array(array('onConsoleCommand', -1)), + ), + $this->listener->getSubscribedEvents() + ); + } + + /** + * @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException + */ + public function testInvalidSiteAccess() + { + $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->onConsoleCommand($event); + } + + public function testValidSiteAccess() + { + $this->dispatcher->expects($this->once()) + ->method('dispatch'); + $input = new ArrayInput(array('--siteaccess' => 'site1'), $this->inputDefinition); + $event = new ConsoleCommandEvent($this->command, $input, $this->testOutput); + $this->listener->onConsoleCommand($event); + $this->assertEquals(new SiteAccess('site1', 'cli'), $this->siteAccess); + } + + public function testDefaultSiteAccess() + { + $this->dispatcher->expects($this->once()) + ->method('dispatch'); + $input = new ArrayInput(array(), $this->inputDefinition); + $event = new ConsoleCommandEvent($this->command, $input, $this->testOutput); + $this->listener->onConsoleCommand($event); + $this->assertEquals(new SiteAccess('default', 'cli'), $this->siteAccess); + } +} diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/Stubs/TestOutput.php b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/Stubs/TestOutput.php new file mode 100644 index 00000000000..01eb63d71ff --- /dev/null +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/Stubs/TestOutput.php @@ -0,0 +1,31 @@ +output = ''; + } + + protected function doWrite($message, $newline) + { + $this->output .= $message . ($newline ? "\n" : ''); + } +}