Skip to content

Commit

Permalink
Validate user action earlier than before execute (#2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Nov 22, 2024
1 parent c6f86ce commit 75603bf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions demos/_unit-test/useraction-no-id-arg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Demos;

use Atk4\Ui\App;
use Atk4\Ui\Button;
use Atk4\Ui\View;

/** @var App $app */
require_once __DIR__ . '/../init-app.php';

$country = new Country($app->db);
DemoActionsUtil::setupDemoActions($country);

$buttons = View::addTo($app, ['ui' => 'vertical basic buttons']);

foreach ($country->getUserActions() as $action) {
$b = Button::addTo($buttons, [$action->getCaption()]);
$b->on('click', $action); // action is intentionally not bound to entity nor ID arg is passed to executor
}
2 changes: 2 additions & 0 deletions src/UserAction/ConfirmationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public function executeModelAction(): void
$this->action = $this->executeModelActionLoad($this->action);

$this->loader->set(function (Loader $p) {
$this->action->validateBeforeExecute();

if ($this->step === 'execute') {
$this->doFinal($p);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/UserAction/StepExecutorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected function setFormField(Form $form, array $fields, string $step): Form
protected function runSteps(): void
{
$this->loader->set(function (Loader $p) {
$this->action->validateBeforeExecute();

switch ($this->step) {
case 'args':
$this->doArgs($p);
Expand Down
31 changes: 31 additions & 0 deletions tests-behat/useraction.feature
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,34 @@ Feature: UserAction executor and UserConfirmation modal
Then No toast should be displayed
When I click using selector "//tr[td[text()='Argentina']]//div.ui.dropdown[div[text()='Actions...']]//div.menu/div[text()='Callback']"
Then Toast display should contain text "Success: callback execute using country Argentina"

Scenario: validate user action earlier than before execute, exception is displayed
Given I am on "_unit-test/useraction-no-id-arg.php"
When I press button "Disabled"
Then Modal is open with text "Atk4\Data\Exception: User action is disabled"
Then I hide js modal
When I press button "Add"
Then Modal is open with text "Add Country"
Then I hide js modal
When I press button "Edit"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "Delete"
Then I press Modal button "Ok"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "Callback"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "Preview"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "Argument"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "User Confirmation"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal
When I press button "Multi Step"
Then Modal is open with text "Atk4\Data\Exception: User action can be executed on loaded entity only"
Then I hide js modal

0 comments on commit 75603bf

Please sign in to comment.