From 7ded07b5c229593b3544db7241593a582b7e718a Mon Sep 17 00:00:00 2001
From: Patrick van Loon
Date: Mon, 5 Jul 2021 16:07:06 +0200
Subject: [PATCH 1/2] Ensure only the form that is inside the request is being
handled.
---
src/Transitioner.php | 5 +++--
tests/TransitionerTest.php | 12 ++++++++----
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/Transitioner.php b/src/Transitioner.php
index 360aa3c..96c0f24 100644
--- a/src/Transitioner.php
+++ b/src/Transitioner.php
@@ -264,8 +264,9 @@ private function createGroupListenerId(string $eventName, string $group): string
private function submitForm(FormInterface $form): void
{
- if (!$form->isSubmitted()) {
- $form->handleRequest($this->getHttpRequest());
+ $httpRequest = $this->getHttpRequest();
+ if ($httpRequest->request->has($form->getName()) && !$form->isSubmitted()) {
+ $form->handleRequest($httpRequest);
}
}
diff --git a/tests/TransitionerTest.php b/tests/TransitionerTest.php
index 931cef4..f087b83 100644
--- a/tests/TransitionerTest.php
+++ b/tests/TransitionerTest.php
@@ -23,6 +23,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;
+use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
@@ -211,7 +212,7 @@ public function testTransitionForwardWithInvalidForm(): void
$this->transitioner->forwards($this->flow);
}
- public function testTransitionFormwardWithBlockedTransition(): void
+ public function testTransitionForwardWithBlockedTransition(): void
{
$this->setCurrentRequest($this->createHttpRequest('forwards'));
$this->setCurrentStepNumber(1);
@@ -483,10 +484,13 @@ private function setCurrentStepNumber(int $currentStep): void
private function createHttpRequest(string $action): HttpRequest
{
- $request = $this->createMock(HttpRequest::class);
- $request->method('get')->with('some-trans-key')->willReturn($action);
+ $httpRequest = $this->createMock(HttpRequest::class);
+ $httpRequest->method('get')->with('some-trans-key')->willReturn($action);
+ $request = $this->createMock(ParameterBag::class);
+ $request->method('has')->willReturn(true);
+ $httpRequest->request = $request;
- return $request;
+ return $httpRequest;
}
private function assertFlowEvent(string $expectedInstance): Constraint
From 0d07c7dc59abc79939453c578272a5c6d9aa4bee Mon Sep 17 00:00:00 2001
From: Patrick van Loon
Date: Mon, 5 Jul 2021 16:20:33 +0200
Subject: [PATCH 2/2] Fix unit test
---
tests/TransitionerTest.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/tests/TransitionerTest.php b/tests/TransitionerTest.php
index f087b83..32d73c2 100644
--- a/tests/TransitionerTest.php
+++ b/tests/TransitionerTest.php
@@ -473,6 +473,7 @@ private function setCurrentStepForm(bool $valid, bool $submitted): void
$form = $this->createStub(FormInterface::class);
$form->method('isValid')->willReturn($valid);
$form->method('isSubmitted')->willReturn($submitted);
+ $form->method('getName')->willReturn('form_name');
$this->flow->method('getCurrentStepForm')->willReturn($form);
}