From 4e78b1b8a6c40bddb2811342ef5cea728ed0b6b5 Mon Sep 17 00:00:00 2001 From: Clemente Raposo Date: Thu, 18 Mar 2021 18:01:25 +0000 Subject: [PATCH] Add convert lead action - Add record action handler - Add action configuration - Add unit test --- config/modules/leads/recordview/actions.php | 29 ++++++ .../RecordActions/ConvertLeadAction.php | 97 +++++++++++++++++++ .../RecordActions/ConvertLeadActionTest.php | 79 +++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 config/modules/leads/recordview/actions.php create mode 100644 core/src/Service/RecordActions/ConvertLeadAction.php create mode 100644 tests/unit/core/src/Service/RecordActions/ConvertLeadActionTest.php diff --git a/config/modules/leads/recordview/actions.php b/config/modules/leads/recordview/actions.php new file mode 100644 index 000000000..833005469 --- /dev/null +++ b/config/modules/leads/recordview/actions.php @@ -0,0 +1,29 @@ +getParameter('module.recordview.actions'); + +if (!isset($actions['modules']['leads'])) { + $actions['modules']['leads'] = []; +} + +if (!isset($actions['modules']['leads']['actions'])) { + $actions['modules']['leads']['actions'] = []; +} + +$actions['modules']['leads']['actions']['convert-lead'] = [ + 'key' => 'convert-lead', + 'labelKey' => 'LBL_CONVERTLEAD', + 'asyncProcess' => true, + 'params' => [], + 'modes' => ['detail'], + 'acl' => ['edit'], +]; + +$container->setParameter('module.recordview.actions', $actions); diff --git a/core/src/Service/RecordActions/ConvertLeadAction.php b/core/src/Service/RecordActions/ConvertLeadAction.php new file mode 100644 index 000000000..2f5435775 --- /dev/null +++ b/core/src/Service/RecordActions/ConvertLeadAction.php @@ -0,0 +1,97 @@ +moduleNameMapper = $moduleNameMapper; + } + + /** + * @inheritDoc + */ + public function getProcessType(): string + { + return self::PROCESS_TYPE; + } + + /** + * @inheritDoc + */ + public function requiredAuthRole(): string + { + return 'ROLE_USER'; + } + + /** + * @inheritDoc + */ + public function configure(Process $process): void + { + //This process is synchronous + //We aren't going to store a record on db + //thus we will use process type as the id + $process->setId(self::PROCESS_TYPE); + $process->setAsync(false); + } + + /** + * @inheritDoc + */ + public function validate(Process $process): void + { + if (empty($process->getOptions())) { + throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND); + } + + $options = $process->getOptions(); + + if (empty($options['module']) || empty($options['action'])) { + throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND); + } + + if (empty($options['id'])) { + throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND); + } + } + + /** + * @inheritDoc + */ + public function run(Process $process) + { + $options = $process->getOptions(); + + $responseData = [ + 'handler' => 'redirect', + 'params' => [ + 'route' => $options['module'] . '/convert-lead/' . $options['id'], + 'queryParams' => [ + ] + ] + ]; + + $process->setStatus('success'); + $process->setMessages([]); + $process->setData($responseData); + } +} diff --git a/tests/unit/core/src/Service/RecordActions/ConvertLeadActionTest.php b/tests/unit/core/src/Service/RecordActions/ConvertLeadActionTest.php new file mode 100644 index 000000000..09027322a --- /dev/null +++ b/tests/unit/core/src/Service/RecordActions/ConvertLeadActionTest.php @@ -0,0 +1,79 @@ +setType('record-convert-lead'); + $process->setOptions([ + 'action' => 'record-convert-lead', + 'id' => 'e24be4e0-4424-9728-9294-5ea315eef53e', + 'module' => 'leads' + ]); + + $this->service->run($process); + + static::assertSame([ + 'handler' => 'redirect', + 'params' => [ + 'route' => 'leads/convert-lead/e24be4e0-4424-9728-9294-5ea315eef53e', + 'queryParams' => [ + ] + ] + ], $process->getData()); + + static::assertEquals('success', $process->getStatus()); + static::assertSame([], $process->getMessages()); + } + + /** + * @throws Exception + */ + protected function _before(): void + { + $session = new Session(new MockArraySessionStorage('PHPSESSID')); + $session->start(); + + $moduleNameMapper = new ModuleNameMapperHandler( + $this->tester->getProjectDir(), + $this->tester->getLegacyDir(), + $this->tester->getLegacySessionName(), + $this->tester->getDefaultSessionName(), + $this->tester->getLegacyScope(), + $session + ); + + $this->service = new ConvertLeadAction( + $moduleNameMapper + ); + } +}