From 81e3861f78bff40e4ecb0e7006b97cf2f79c7792 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 17 Apr 2024 09:52:59 +0200 Subject: [PATCH] Add regression test for filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filters are used for converting an input value (usually a string) into a domain specific PHP type and they are bound to the validation scope: https://doc.nette.org/en/forms/validation#toc-modifying-input-values 45ed76a7ed22c9b97048441eccd4a7fb2f8f2d6f started filtering components passed to `Container::validate()` to `Control`s, to appease PHPStan. But `Multiplier` does not actually have `Control`s as direct children (other than the ‘Add’ `Submitter`s), so it would stop validating and filtering multiplied controls. It has been since fixed in a5a7348fdb1046275e83fa47d73a444695cf21b4, and more properly in the parent commit but let’s add a test so this does not happen again. It can be reproduced by removing `|| $component instanceof Container` from the parent commit. --- tests/Unit/MultiplierTest.php | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Unit/MultiplierTest.php b/tests/Unit/MultiplierTest.php index bf04ca4..0a6f55e 100644 --- a/tests/Unit/MultiplierTest.php +++ b/tests/Unit/MultiplierTest.php @@ -330,6 +330,50 @@ public function testSendNestedInnerWithDefault() ); } + /** + * Ensure filters work on submit, since they are dependent on properly set valdation scope. + * Regression test for https://github.com/contributte/forms-multiplier/issues/68 + */ + public function testSubmitFilter() + { + $response = $this->services->form->createRequest( + MultiplierBuilder::create() + ->fields([]) + ->beforeFormModifier(function (Form $form) { + $form->addInteger('num'); + }) + ->multiplierModifier(function (Multiplier $multiplier) { + $multiplier->onCreate[] = function (Container $container) { + $container->addInteger('mnum'); + }; + }) + ->formModifier(function (Form $form) { + $form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () { + }; + }) + ->createForm() + ) + ->setPost([ + 'num' => '11', + 'm' => [ + ['mnum' => '49'], + ], + ])->send(); + + $this->assertTrue($response->isSuccess()); + $this->assertSame([ + 'num' => 11, + 'm' => [ + ['mnum' => 49], + ], + ], $response->getValues()); + + $dom = $response->toDomQuery(); + + $this->assertDomHas($dom, 'input[name="m[0][mnum]"][value="49"]'); + $this->assertDomNotHas($dom, 'input[name="m[1][mnum]"]'); + } + public function testGroup() { $request = $this->services->form->createRequest(