Skip to content

Commit

Permalink
feat: Allow uploads on a new conversation (dvsa/olcs-selfserve#95)
Browse files Browse the repository at this point in the history
* feat: Allow uploads on a new conversation

* fix: PHPCS white space
  • Loading branch information
wadedvsa authored Mar 13, 2024
1 parent 950ed5f commit 40d63cc
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,32 @@ public function indexAction(): ViewModel
public function addAction(): ViewModel
{
$form = $this->formHelperService->createForm(CreateForm::class, true, false);
if ($this->getRequest()->isPost()) {
$form->get('correlationId')->setValue(sha1(microtime()));

$isPost = $this->getRequest()->isPost();
$canUploadFiles = $this->getCurrentOrganisation()['isMessagingFileUploadEnabled'];
if (!$canUploadFiles) {
$form->get('form-actions')->remove('file');
}

if ($isPost) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
return $this->submitConversation($form);
}
}

$hasProcessedFiles = false;
if ($canUploadFiles && $isPost) {
$hasProcessedFiles = $this->processFiles(
$form,
'form-actions->file',
[$this, 'processFileUpload'],
[$this, 'deleteFile'],
[$this, 'getUploadedFiles'],
'form-actions->file->fileCount',
);
}

if (!$hasProcessedFiles && $isPost && $form->isValid()) {
return $this->submitConversation($form);
}

$view = new ViewModel();
Expand Down Expand Up @@ -144,6 +165,7 @@ private function mapFormDataToCommand(\Laminas\Form\Form $form): Create
$processedData = [
'messageSubject' => $data['form-actions']['messageSubject'],
'messageContent' => $data['form-actions']['messageContent'],
'correlationId' => $data['correlationId'],
];

$appOrLicNoPrefix = substr($data['form-actions']['appOrLicNo'], 0, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Common\Form\Element\DynamicSelect;
use Common\Form\Elements\InputFilters\ActionButton;
use Common\Form\Elements\Types\GuidanceTranslated;
use Common\Form\Model\Fieldset\MultipleFileUpload;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\Textarea;

Expand Down Expand Up @@ -59,6 +60,13 @@ class Create
*/
public ?TextArea $messageContent = null;

/**
* @Form\Name("file")
* @Form\Attributes({"id": "file"})
* @Form\ComposedObject(MultipleFileUpload::class)
*/
public ?MultipleFileUpload $file = null;

/**
* @Form\Attributes({"value": "markup-messaging-new-conversation-timeframe"})
* @Form\Type(\Common\Form\Elements\Types\GuidanceTranslated::class)
Expand Down
13 changes: 10 additions & 3 deletions app/selfserve/module/Olcs/src/Form/Model/Form/Message/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
namespace Olcs\Form\Model\Form\Message;

use Laminas\Form\Annotation as Form;
use Olcs\Form\Model\Fieldset\Message\Reply as ReplyFieldset;
use Laminas\Form\Element\Hidden;
use Olcs\Form\Model\Fieldset\Message\Create as CreateFieldset;

/**
* @Form\Type("Common\Form\Form")
*/
class Create
{
/**
* @Form\Attributes({"value": ""})
* @Form\Type(Hidden::class)
*/
public ?Hidden $correlationId = null;

/**
* @Form\Name("form-actions")
* @Form\ComposedObject(\Olcs\Form\Model\Fieldset\Message\Create::class)
* @Form\ComposedObject(CreateFieldset::class)
*/
public ?ReplyFieldset $formActions = null;
public ?CreateFieldset $formActions = null;
}
110 changes: 93 additions & 17 deletions app/selfserve/test/Olcs/src/Controller/ConversationsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Laminas\Form\Element\Text;
use Laminas\Form\Fieldset;
use Laminas\Http\Request;
use Laminas\Http\Response as HttpResponse;
use Laminas\Mvc\Controller\Plugin\Params;
use Dvsa\Olcs\Transfer\Command\Messaging\Message\Create as CreateMessageCommand;
use Laminas\Mvc\Controller\Plugin\Url;
Expand All @@ -26,6 +25,7 @@
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase as TestCase;
use Olcs\Controller\ConversationsController as Sut;
use Olcs\Form\Model\Form\Message\Create;
use Olcs\Form\Model\Form\Message\Reply;
use ReflectionClass;
use LmcRbacMvc\Service\AuthorizationService;
Expand Down Expand Up @@ -59,7 +59,17 @@ public function setUp(): void
$this->setMockedProperties($reflectionClass, 'formHelperService', $this->mockFormHelperService);
$this->setMockedProperties($reflectionClass, 'navigationService', $this->mockNavigation);
$this->setMockedProperties($reflectionClass, 'uploadHelper', $this->mockUploadHelper);
}

public function setMockedProperties(ReflectionClass $reflectionClass, string $property, $value): void
{
$reflectionProperty = $reflectionClass->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->sut, $value);
}

public function testViewAction(): void
{
$this->mockFormHelperService->shouldReceive('createForm')
->once()
->with(Reply::class, true, false)
Expand All @@ -73,17 +83,7 @@ function ($form, $request) {
return true;
},
);
}

public function setMockedProperties(ReflectionClass $reflectionClass, string $property, $value): void
{
$reflectionProperty = $reflectionClass->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->sut, $value);
}

public function testViewAction(): void
{
$mockResponse = m::mock(Response::class);
$mockResponse->shouldReceive('isOk')
->andReturn(true);
Expand Down Expand Up @@ -184,6 +184,84 @@ public function testViewAction(): void
$this->assertEquals($table, $view->getVariable('table'));
}

public function testAdd(): void
{
$mockUrl = m::mock(Url::class);
$mockUrl->shouldReceive('fromRoute')
->once()
->with('conversations')
->andReturn('/back/route');

$this->mockUser->shouldReceive('getUserData')
->once()
->andReturn(
[
'organisationUsers' => [
[
'organisation' => [
'isMessagingFileUploadEnabled' => true,
],
],
],
],
);
$this->sut->shouldReceive('plugin')
->with('currentUser')
->andReturn($this->mockUser);
$this->sut->shouldReceive('plugin')
->with('url')
->once()
->andReturn($mockUrl);

$this->mockFormHelperService->shouldReceive('createForm')
->once()
->with(Create::class, true, false)
->andReturn($this->mockForm);

$mockFormElement = m::mock(Hidden::class);
$mockFormElement->shouldReceive('setValue')
->once();

$this->mockForm->shouldReceive('get')
->once()
->with('correlationId')
->andReturn($mockFormElement);

$this->mockForm->shouldReceive('setData')
->once()
->with([]);

$mockRequest = m::mock(Request::class);
$mockRequest->shouldReceive('isPost')
->once()
->andReturn(true);
$mockRequest->shouldReceive('getPost')
->once()
->andReturn([]);

$this->sut->shouldReceive('getRequest')
->twice()
->andReturn($mockRequest);

$this->mockForm->shouldReceive('isValid')
->once()
->andReturn(false);

$this->sut->shouldReceive('processFiles')
->once()
->with(
$this->mockForm,
'form-actions->file',
[$this->sut, 'processFileUpload'],
[$this->sut, 'deleteFile'],
[$this->sut, 'getUploadedFiles'],
'form-actions->file->fileCount',
);

$view = $this->sut->addAction();
$this->assertInstanceOf(ViewModel::class, $view);
}

public function testReply(): void
{
$mockRequest = m::mock(Request::class);
Expand Down Expand Up @@ -228,13 +306,11 @@ public function testReply(): void
$mockCommandHandler = m::mock(HandleCommand::class);
$mockCommandHandler->shouldReceive('__invoke')
->once()
->withArgs(
function ($command) {
$this->assertInstanceOf(CreateMessageCommand::class, $command);
->withArgs(function ($command) {
$this->assertInstanceOf(CreateMessageCommand::class, $command);

return true;
},
)
return true;
})
->andReturn($mockCommandReturn);

$this->sut->shouldReceive('plugin')
Expand Down

0 comments on commit 40d63cc

Please sign in to comment.