Skip to content

Commit

Permalink
Fix EZP-24760: console commands should validate siteaccess
Browse files Browse the repository at this point in the history
  • Loading branch information
wizhippo committed Sep 3, 2015
1 parent f6793dc commit 2121f23
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 5 deletions.
Expand Up @@ -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;
Expand All @@ -29,6 +30,11 @@ class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAwar
*/
private $defaultSiteAccessName;

/**
* @var array
*/
private $siteAccessList;

/**
* @var EventDispatcherInterface
*/
Expand All @@ -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;
}

Expand All @@ -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));
}

Expand Down
Expand Up @@ -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:
Expand Down
@@ -0,0 +1,117 @@
<?php

/**
* File containing the ConsoleCommandListenerTest class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace eZ\Bundle\EzPublishCoreBundle\Tests\EventListener;

use eZ\Bundle\EzPublishCoreBundle\EventListener\ConsoleCommandListener;
use eZ\Bundle\EzPublishCoreBundle\Tests\EventListener\Stubs\TestOutput;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use PHPUnit_Framework_TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ConsoleCommandListenerTest extends PHPUnit_Framework_TestCase
{
/**
* @var array
*/
private $siteAccessList = ['default', 'site1'];

/**
* @var SiteAccess
*/
private $siteAccess;

/**
* @var EventDispatcherInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $dispatcher;

/**
* @var ConsoleCommandListener
*/
private $listener;

/**
* @var InputDefinition;
*/
private $inputDefinition;

/**
* @var Command
*/
private $command;

/**
* @var TestOutput
*/
private $testOutput;

public function setUp()
{
parent::setUp();
$this->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);
}
}
@@ -0,0 +1,31 @@
<?php

/**
* File containing the TestOutput class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace eZ\Bundle\EzPublishCoreBundle\Tests\EventListener\Stubs;

use Symfony\Component\Console\Output\Output;

/**
* Stub class for TestOutput Output.
*/
class TestOutput extends Output
{
public $output = '';

public function clear()
{
$this->output = '';
}

protected function doWrite($message, $newline)
{
$this->output .= $message . ($newline ? "\n" : '');
}
}

0 comments on commit 2121f23

Please sign in to comment.