From faee5671e1f7dfbc26315890c192318488da2df8 Mon Sep 17 00:00:00 2001 From: rogamoore Date: Wed, 15 Feb 2023 11:15:53 +0000 Subject: [PATCH] Remove reference to LiipThemeBundle and dispatch event instead (#7004) --- UPGRADE.md | 6 + .../Command/ValidateWebspacesCommand.php | 22 +-- .../PageBundle/Resources/config/command.xml | 2 +- .../Command/ValidateWebspacesCommandTest.php | 3 +- .../Command/ValidateWebspacesCommandTest.php | 125 ++++++++++++++++++ 5 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 src/Sulu/Bundle/PageBundle/Tests/Unit/Command/ValidateWebspacesCommandTest.php diff --git a/UPGRADE.md b/UPGRADE.md index 10644ef0b8b..a9a16a66b72 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,11 @@ # Upgrade +## 2.5.7 + +### Constructor of ValidateWebspacesCommand changed + +The constructor of `ValidateWebspacesCommand` requires now EventDispatcherInterface instead of activeTheme. + ## 2.5.2 ### Add indexes to route table diff --git a/src/Sulu/Bundle/PageBundle/Command/ValidateWebspacesCommand.php b/src/Sulu/Bundle/PageBundle/Command/ValidateWebspacesCommand.php index 49929c16763..48e1016d127 100644 --- a/src/Sulu/Bundle/PageBundle/Command/ValidateWebspacesCommand.php +++ b/src/Sulu/Bundle/PageBundle/Command/ValidateWebspacesCommand.php @@ -11,8 +11,11 @@ namespace Sulu\Bundle\PageBundle\Command; +use Sulu\Bundle\PreviewBundle\Preview\Events; +use Sulu\Bundle\PreviewBundle\Preview\Events\PreRenderEvent; use Sulu\Component\Content\Compat\StructureManagerInterface; use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; +use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes; use Sulu\Component\Webspace\Manager\WebspaceManagerInterface; use Sulu\Component\Webspace\StructureProvider\WebspaceStructureProvider; use Sulu\Component\Webspace\Webspace; @@ -20,6 +23,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Twig\Environment; class ValidateWebspacesCommand extends Command @@ -62,14 +66,14 @@ class ValidateWebspacesCommand extends Command private $errors = []; /** - * @var string + * @var WebspaceManagerInterface */ - private $activeTheme; + private $webspaceManager; /** - * @var WebspaceManagerInterface + * @var EventDispatcherInterface */ - private $webspaceManager; + private $eventDispatcher; public function __construct( Environment $twig, @@ -78,7 +82,7 @@ public function __construct( StructureManagerInterface $structureManager, WebspaceStructureProvider $structureProvider, WebspaceManagerInterface $webspaceManager, - $activeTheme = null + EventDispatcherInterface $eventDispatcher ) { parent::__construct(); @@ -87,8 +91,8 @@ public function __construct( $this->controllerNameConverter = $controllerNameConverter; $this->structureManager = $structureManager; $this->structureProvider = $structureProvider; - $this->activeTheme = $activeTheme; $this->webspaceManager = $webspaceManager; + $this->eventDispatcher = $eventDispatcher; } protected function configure() @@ -106,9 +110,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $messages = ''; foreach ($webspaces as $webspace) { - if (null !== $this->activeTheme) { - $this->activeTheme->setName($webspace->getTheme()); - } + $this->eventDispatcher->dispatch(new PreRenderEvent(new RequestAttributes([ + 'webspace' => $webspace, + ])), Events::PRE_RENDER); $this->outputWebspace($webspace); } diff --git a/src/Sulu/Bundle/PageBundle/Resources/config/command.xml b/src/Sulu/Bundle/PageBundle/Resources/config/command.xml index 8bed753f2c9..708038becf5 100644 --- a/src/Sulu/Bundle/PageBundle/Resources/config/command.xml +++ b/src/Sulu/Bundle/PageBundle/Resources/config/command.xml @@ -54,7 +54,7 @@ - + diff --git a/src/Sulu/Bundle/PageBundle/Tests/Functional/Command/ValidateWebspacesCommandTest.php b/src/Sulu/Bundle/PageBundle/Tests/Functional/Command/ValidateWebspacesCommandTest.php index a9aac73c9a7..3768cafefd9 100644 --- a/src/Sulu/Bundle/PageBundle/Tests/Functional/Command/ValidateWebspacesCommandTest.php +++ b/src/Sulu/Bundle/PageBundle/Tests/Functional/Command/ValidateWebspacesCommandTest.php @@ -45,7 +45,8 @@ public function setUp(): void $controllerNameParser, $this->getContainer()->get('sulu.content.structure_manager'), $this->getContainer()->get('sulu.content.webspace_structure_provider'), - $this->getContainer()->get('sulu_core.webspace.webspace_manager') + $this->getContainer()->get('sulu_core.webspace.webspace_manager'), + $this->getContainer()->get('event_dispatcher') ); $command->setApplication($application); $this->tester = new CommandTester($command); diff --git a/src/Sulu/Bundle/PageBundle/Tests/Unit/Command/ValidateWebspacesCommandTest.php b/src/Sulu/Bundle/PageBundle/Tests/Unit/Command/ValidateWebspacesCommandTest.php new file mode 100644 index 00000000000..e2b4080d28d --- /dev/null +++ b/src/Sulu/Bundle/PageBundle/Tests/Unit/Command/ValidateWebspacesCommandTest.php @@ -0,0 +1,125 @@ + + */ + private $twig; + + /** + * @var ObjectProphecy + */ + private $structureMetadataFactory; + + /** + * @var ObjectProphecy + */ + private $structureManager; + + /** + * @var ObjectProphecy + */ + private $structureProvider; + + /** + * @var ObjectProphecy + */ + private $webspaceManager; + + /** + * @var ObjectProphecy + */ + private $eventDispatcher; + + /** + * @var ValidateWebspacesCommand + */ + private $validateWebspacesCommand; + + /** + * @var ObjectProphecy + */ + private $input; + + /** + * @var ObjectProphecy + */ + private $output; + + public function setUp(): void + { + $this->twig = $this->prophesize(Environment::class); + $this->structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class); + $this->structureManager = $this->prophesize(StructureManagerInterface::class); + $this->structureProvider = $this->prophesize(WebspaceStructureProvider::class); + $this->webspaceManager = $this->prophesize(WebspaceManagerInterface::class); + $this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + + $this->validateWebspacesCommand = new ValidateWebspacesCommand( + $this->twig->reveal(), + $this->structureMetadataFactory->reveal(), + null, + $this->structureManager->reveal(), + $this->structureProvider->reveal(), + $this->webspaceManager->reveal(), + $this->eventDispatcher->reveal() + ); + + $this->input = $this->prophesize(InputInterface::class); + $this->output = $this->prophesize(OutputInterface::class); + } + + public function testExecute(): void + { + $webspace = new Webspace(); + $webspace->setKey('sulu_io'); + $webspace->addLocalization(new Localization('de')); + + $this->structureManager->getStructures()->willReturn([]); + $this->webspaceManager->getWebspaceCollection()->willReturn(new WebspaceCollection([$webspace])); + + $executeMethod = new \ReflectionMethod(ValidateWebspacesCommand::class, 'execute'); + $executeMethod->setAccessible(true); + + $this->eventDispatcher->dispatch(new PreRenderEvent(new RequestAttributes([ + 'webspace' => $webspace, + ])), Events::PRE_RENDER) + ->shouldBeCalled(); + + $executeMethod->invoke($this->validateWebspacesCommand, $this->input->reveal(), $this->output->reveal()); + } +}