forked from ezsystems/ezpublish-kernel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix EZP-24760: console commands should validate siteaccess
- Loading branch information
Showing
4 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ConsoleCommandListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/Stubs/TestOutput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" : ''); | ||
} | ||
} |