Skip to content

Commit

Permalink
Support return redirect response for back step
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Sep 16, 2024
1 parent 146206c commit 6c4cb08
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
11 changes: 9 additions & 2 deletions src/Wizardable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ycs77\LaravelWizard;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

trait Wizardable
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down
54 changes: 29 additions & 25 deletions tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
36 changes: 36 additions & 0 deletions tests/Stubs/WizardControllerBeforeBackStepStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Ycs77\LaravelWizard\Test\Stubs;

use Illuminate\Http\Request;
use Ycs77\LaravelWizard\Step;

class WizardControllerBeforeBackStepStub extends WizardControllerStub
{
/**
* The wizard steps instance.
*
* @var array
*/
protected $steps = [
UserStepStub::class,
PostStepStub::class,
AvatarStepStub::class,
];

/**
* On before back wizard step event.
*
* @param \Illuminate\Http\Request $request
* @param \Ycs77\LaravelWizard\Step $step
* @return bool|\Illuminate\Http\RedirectResponse
*/
protected function beforeBackWizardStep(Request $request, Step $step)
{
if ($step instanceof AvatarStepStub) {
return $this->wizard()->redirectToStep('user-step-stub');
}

return true;
}
}
5 changes: 1 addition & 4 deletions tests/Stubs/WizardControllerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6c4cb08

Please sign in to comment.