diff --git a/src/Wizardable.php b/src/Wizardable.php index b587704..d494284 100644 --- a/src/Wizardable.php +++ b/src/Wizardable.php @@ -3,6 +3,7 @@ namespace Ycs77\LaravelWizard; use Closure; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; trait Wizardable @@ -102,7 +103,13 @@ public function store(Request $request, string $step) $step = $this->getWizardStep($request, $step); // If trigger from 'back', set this step index and redirect to prev step. - if ($request->query('_trigger') === 'back' && $this->beforeBackWizardStep($request, $step)) { + if ($request->query('_trigger') === 'back' && + ($redirectTo = $this->beforeBackWizardStep($request, $step)) !== false + ) { + if ($redirectTo instanceof RedirectResponse) { + return $redirectTo; + } + $this->wizard()->cacheProgress($request, $step); $prevStep = $this->wizard()->stepRepo()->prev(); @@ -545,7 +552,7 @@ protected function wizardStepSaved(Request $request, Step $step) * * @param \Illuminate\Http\Request $request * @param \Ycs77\LaravelWizard\Step $step - * @return bool + * @return bool|\Illuminate\Http\RedirectResponse */ protected function beforeBackWizardStep(Request $request, Step $step) { diff --git a/tests/Feature/HttpTest.php b/tests/Feature/HttpTest.php index 46aed4b..fa3b5cf 100644 --- a/tests/Feature/HttpTest.php +++ b/tests/Feature/HttpTest.php @@ -7,6 +7,11 @@ use Illuminate\Support\Facades\Storage; use Ycs77\LaravelWizard\Cache\CachedFile; use Ycs77\LaravelWizard\Facades\Wizard; +use Ycs77\LaravelWizard\Test\Stubs\WizardControllerBeforeBackStepStub; +use Ycs77\LaravelWizard\Test\Stubs\WizardControllerOptionsStub; +use Ycs77\LaravelWizard\Test\Stubs\WizardControllerSkipStub; +use Ycs77\LaravelWizard\Test\Stubs\WizardControllerStub; +use Ycs77\LaravelWizard\Test\Stubs\WizardControllerUploadFileStub; use Ycs77\LaravelWizard\Test\TestCase; class HttpTest extends TestCase @@ -17,11 +22,7 @@ protected function setUp(): void { parent::setUp(); - $this->setWizardRoutes( - '/wizard/test', - '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerStub', - 'wizard.test' - ); + $this->setWizardRoutes('/wizard/test', WizardControllerStub::class, 'wizard.test'); $this->authenticate(); } @@ -140,11 +141,7 @@ public function testRunAllWizardStepsSaveOnLastStepAndCanBeCachedUploadedFile() Storage::fake('local'); - $this->setWizardRoutes( - '/wizard/upload-file', - '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerUploadFileStub', - 'wizard.upload-file' - ); + $this->setWizardRoutes('/wizard/upload-file', WizardControllerUploadFileStub::class, 'wizard.upload-file'); // Get avatar step $response = $this->get('/wizard/upload-file/avatar-step-stub'); @@ -226,6 +223,25 @@ public function testWizardStepTriggerToBack() ], $this->app['session']->get('laravel_wizard.test')); } + public function testWizardStepRedirectToResponseFromBack() + { + $this->session([ + 'laravel_wizard.test' => [ + '_last_index' => 2, + ], + ]); + + $this->setWizardRoutes('/wizard/back-step', WizardControllerBeforeBackStepStub::class, 'wizard.back-step'); + + // Get avatar step + $response = $this->get('/wizard/back-step/avatar-step-stub'); + $response->assertStatus(200); + + // Post avatar step + $response = $this->post('/wizard/back-step/avatar-step-stub?_trigger=back'); + $response->assertRedirect('/wizard/back-step/user-step-stub'); + } + public function testWizardStepTriggerToBackNoValidate() { $this->session([ @@ -260,11 +276,7 @@ public function testWizardStepTriggerToSkipAndCached() { $this->app['config']->set('wizard.cache', true); - $this->setWizardRoutes( - '/wizard/can-skip', - '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerSkipStub', - 'wizard.can-skip' - ); + $this->setWizardRoutes('/wizard/can-skip', WizardControllerSkipStub::class, 'wizard.can-skip'); $response = $this->post('/wizard/can-skip/user-step-stub?_trigger=skip', [ 'name' => null, @@ -284,11 +296,7 @@ public function testWizardStepTriggerToSkipAndNoCache() { $this->app['config']->set('wizard.cache', false); - $this->setWizardRoutes( - '/wizard/can-skip', - '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerSkipStub', - 'wizard.can-skip' - ); + $this->setWizardRoutes('/wizard/can-skip', WizardControllerSkipStub::class, 'wizard.can-skip'); $response = $this->post('/wizard/can-skip/user-step-stub?_trigger=skip', [ 'name' => null, @@ -329,11 +337,7 @@ public function testWizardNoCacheNowRunStepSaveData() public function testWizardSetNoCacheFromControllerNowRunStepSaveData() { - $this->setWizardRoutes( - '/wizard/no-cache', - '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerOptionsStub', - 'wizard.no-cache' - ); + $this->setWizardRoutes('/wizard/no-cache', WizardControllerOptionsStub::class, 'wizard.no-cache'); $response = $this->post('/wizard/no-cache/user-step-stub', [ 'name' => 'John', diff --git a/tests/Stubs/WizardControllerBeforeBackStepStub.php b/tests/Stubs/WizardControllerBeforeBackStepStub.php new file mode 100644 index 0000000..57ef420 --- /dev/null +++ b/tests/Stubs/WizardControllerBeforeBackStepStub.php @@ -0,0 +1,36 @@ +wizard()->redirectToStep('user-step-stub'); + } + + return true; + } +} diff --git a/tests/Stubs/WizardControllerStub.php b/tests/Stubs/WizardControllerStub.php index 0c64f8c..87fe8ce 100644 --- a/tests/Stubs/WizardControllerStub.php +++ b/tests/Stubs/WizardControllerStub.php @@ -2,15 +2,12 @@ namespace Ycs77\LaravelWizard\Test\Stubs; -use Illuminate\Foundation\Auth\Access\AuthorizesRequests; -use Illuminate\Foundation\Bus\DispatchesJobs; -use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller; use Ycs77\LaravelWizard\Wizardable; class WizardControllerStub extends Controller { - use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Wizardable; + use Wizardable; /** * The wizard name.