@@ -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