@@ -302,6 +302,106 @@ protected function executeStepNextStep($instanceId, array $data): array
302302 ]);
303303});
304304
305+ it ('authorize() is only called for firstStep ' , function () {
306+ $ authorizeForStep = true ;
307+
308+ fakeListFor ('person ' , new class ($ authorizeForStep ) extends PersonList
309+ {
310+ public function __construct (public bool &$ authorizeForStep ) {}
311+
312+ protected function getInstanceCommands (): ?array
313+ {
314+ return [
315+ 'wizard ' => new class ($ this ->authorizeForStep ) extends InstanceWizardCommand
316+ {
317+ public function __construct (public bool &$ authorizeForStep ) {}
318+
319+ protected function getKey (): string
320+ {
321+ return 'test-key ' ;
322+ }
323+
324+ public function label (): ?string
325+ {
326+ return 'my command ' ;
327+ }
328+
329+ public function authorizeFor (mixed $ instanceId ): bool
330+ {
331+ return false ;
332+ }
333+
334+ public function authorizeForStep (string $ step , mixed $ instanceId ): bool
335+ {
336+ return $ this ->authorizeForStep ;
337+ }
338+
339+ public function buildFormFieldsForFirstStep (FieldsContainer $ formFields ): void
340+ {
341+ $ formFields ->addField (SharpFormTextField::make ('name ' ));
342+ }
343+
344+ protected function executeFirstStep ($ instanceId , array $ data ): array
345+ {
346+ $ this ->validate ($ data , ['name ' => 'required ' ]);
347+
348+ return $ this ->toStep ('next-step ' );
349+ }
350+
351+ public function buildFormFieldsForStepNextStep (FieldsContainer $ formFields ): void
352+ {
353+ $ formFields ->addField (SharpFormTextField::make ('age ' ));
354+ }
355+
356+ protected function executeStepNextStep ($ instanceId , array $ data ): array
357+ {
358+ return $ this ->reload ();
359+ }
360+ },
361+ ];
362+ }
363+ });
364+
365+ // First post step 1...
366+ $ this
367+ ->postJson (
368+ route ('code16.sharp.api.list.command.instance ' , [
369+ 'globalFilter ' => 'root ' ,
370+ 'entityKey ' => 'person ' ,
371+ 'commandKey ' => 'wizard ' ,
372+ 'instanceId ' => 1 ]),
373+ ['data ' => ['name ' => 'test ' ]],
374+ )
375+ ->assertForbidden ();
376+
377+ // Then post step 2 should be authorized (default)...
378+ $ this
379+ ->postJson (
380+ route ('code16.sharp.api.list.command.instance ' , [
381+ 'entityKey ' => 'person ' ,
382+ 'commandKey ' => 'wizard ' ,
383+ 'instanceId ' => 1 ,
384+ 'command_step ' => 'next-step:test-key ' ,
385+ ]),
386+ ['data ' => ['age ' => '22 ' ]],
387+ )
388+ ->assertOk ();
389+
390+ // Post 2 but disallowed
391+ $ authorizeForStep = false ;
392+ $ this
393+ ->postJson (
394+ route ('code16.sharp.api.list.command.instance ' , [
395+ 'entityKey ' => 'person ' ,
396+ 'commandKey ' => 'wizard ' ,
397+ 'instanceId ' => 1 ,
398+ 'command_step ' => 'next-step:test-key ' ,
399+ ]),
400+ ['data ' => ['age ' => '22 ' ]],
401+ )
402+ ->assertForbidden ();
403+ });
404+
305405it ('allows to define a global method for step execution ' , function () {
306406 fakeListFor ('person ' , new class () extends PersonList
307407 {
0 commit comments