Skip to content

Commit 70f1d8c

Browse files
authored
Merge pull request #355 from FriendsOfCake/search-form
Use Form::execute() instead of Form::validate().
2 parents 59dfa3f + 050707c commit 70f1d8c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Controller/Component/SearchComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function startup(EventInterface $event): void
104104

105105
$params = (array)$this->getController()->getRequest()->getData();
106106

107-
if ($form !== null && !$form->validate($params)) {
107+
if ($form !== null && !$form->execute($params)) {
108108
$this->getController()->set('searchForm', $form);
109109

110110
return;

tests/TestCase/Controller/Component/SearchComponentTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public function testGetWithForm(): void
379379
$this->assertInstanceOf(SearchForm::class, $this->Controller->viewBuilder()->getVar('searchForm'));
380380
}
381381

382-
public function testPostWithFormValidation(): void
382+
public function testPostFormValidationFailure(): void
383383
{
384384
$request = $this->Controller->getRequest()
385385
->withAttribute('params', [
@@ -406,7 +406,45 @@ public function testPostWithFormValidation(): void
406406
'minLength' => 'Search query must be at least 3 characters long',
407407
],
408408
];
409-
$this->assertEquals($errors, $form->getErrors());
409+
$this->assertSame($errors, $form->getErrors());
410+
}
411+
412+
public function testPostFormValidationSuccess(): void
413+
{
414+
$request = $this->Controller->getRequest()
415+
->withAttribute('params', [
416+
'controller' => 'Posts',
417+
'action' => 'index',
418+
])
419+
->withRequestTarget('/Posts')
420+
->withData('q', 'abcd')
421+
->withEnv('REQUEST_METHOD', 'POST');
422+
423+
$mock = new class extends SearchForm
424+
{
425+
public static $called = false;
426+
427+
protected function _execute(array $data = []): bool
428+
{
429+
static::$called = true;
430+
431+
return true;
432+
}
433+
};
434+
435+
$this->Search->setConfig('formClass', $mock::class);
436+
437+
$this->Controller->setRequest($request);
438+
$event = new Event('Controller.startup', $this->Controller);
439+
$this->Search->startup($event);
440+
441+
$result = $event->getResult();
442+
$this->assertInstanceOf(Response::class, $result);
443+
444+
$this->assertTrue($mock::$called, 'Form execute method was not called');
445+
446+
// View var is not set as there will be a redirect
447+
$this->assertNull($this->Controller->viewBuilder()->getVar('searchForm'));
410448
}
411449

412450
/**

0 commit comments

Comments
 (0)