init
This commit is contained in:
21 files changed
+12224
No files matched your search
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaRowActions\Tests;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Nova\Actions\Action;
|
||||
use Laravel\Nova\Fields\ActionFields;
|
||||
use Laravel\Nova\Fields\Text;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Laravel\Nova\Nova;
|
||||
use Laravel\Nova\Resource;
|
||||
use MeghdadFadaee\NovaRowActions\FieldServiceProvider;
|
||||
use MeghdadFadaee\NovaRowActions\RowActions;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
|
||||
class RowActionsTest extends TestCase
|
||||
{
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
return [FieldServiceProvider::class];
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Nova::resources([RowActionResource::class]);
|
||||
RowActionResource::$allowsActions = true;
|
||||
$this->app->instance(NovaRequest::class, $this->novaRequest());
|
||||
}
|
||||
|
||||
public function test_it_serializes_native_action_metadata_and_arbitrary_configuration(): void
|
||||
{
|
||||
$field = RowActions::make('Operations')
|
||||
->add(
|
||||
ConfigurableAction::make()->withoutConfirmation(),
|
||||
icon: 'bolt',
|
||||
color: 'warning',
|
||||
variant: 'solid',
|
||||
label: fn (Model $model) => 'Run '.$model->getKey(),
|
||||
tooltip: 'Custom tooltip',
|
||||
order: 42,
|
||||
confirm: true,
|
||||
showLabel: true,
|
||||
);
|
||||
|
||||
$action = $this->serialize($field, $this->model(id: 17))['actions'][0];
|
||||
|
||||
$this->assertSame('configurable-action', $action['action']['uriKey']);
|
||||
$this->assertSame('Configurable Action', $action['action']['name']);
|
||||
$this->assertSame('confirm-action-modal', $action['action']['component']);
|
||||
$this->assertSame('reason', $action['action']['fields'][0]['attribute']);
|
||||
$this->assertFalse($action['action']['withoutConfirmation']);
|
||||
$this->assertSame('bolt', $action['icon']);
|
||||
$this->assertSame('warning', $action['color']);
|
||||
$this->assertSame('solid', $action['variant']);
|
||||
$this->assertSame('Run 17', $action['label']);
|
||||
$this->assertSame('Custom tooltip', $action['tooltip']);
|
||||
$this->assertSame(42, $action['order']);
|
||||
$this->assertTrue($action['showLabel']);
|
||||
}
|
||||
|
||||
public function test_it_hides_actions_that_fail_nova_authorization(): void
|
||||
{
|
||||
$field = RowActions::make()
|
||||
->add(ConfigurableAction::make()->canSee(fn () => false))
|
||||
->add(ConfigurableAction::make()->canRun(fn () => false));
|
||||
|
||||
$this->assertSame([], $this->serialize($field, $this->model())['actions']);
|
||||
|
||||
RowActionResource::$allowsActions = false;
|
||||
|
||||
$policyField = RowActions::make()->add(ConfigurableAction::make());
|
||||
|
||||
$this->assertSame([], $this->serialize($policyField, $this->model())['actions']);
|
||||
}
|
||||
|
||||
public function test_explicit_can_run_authorization_matches_novas_policy_precedence(): void
|
||||
{
|
||||
RowActionResource::$allowsActions = false;
|
||||
|
||||
$field = RowActions::make()->add(
|
||||
ConfigurableAction::make()->canRun(fn () => true)
|
||||
);
|
||||
|
||||
$this->assertCount(1, $this->serialize($field, $this->model())['actions']);
|
||||
}
|
||||
|
||||
public function test_it_applies_visibility_active_and_disabled_callbacks_per_model(): void
|
||||
{
|
||||
$field = RowActions::make()
|
||||
->add(
|
||||
ConfigurableAction::make(),
|
||||
activeWhen: fn (Model $model) => $model->state === 'active',
|
||||
disabledWhen: fn (Model $model) => $model->locked,
|
||||
visibleWhen: fn (Model $model) => $model->visible,
|
||||
);
|
||||
|
||||
$visible = $this->serialize($field, $this->model(
|
||||
state: 'active',
|
||||
locked: true,
|
||||
visible: true,
|
||||
))['actions'][0];
|
||||
|
||||
$this->assertTrue($visible['active']);
|
||||
$this->assertTrue($visible['disabled']);
|
||||
|
||||
$hidden = $this->serialize($field, $this->model(visible: false));
|
||||
|
||||
$this->assertSame([], $hidden['actions']);
|
||||
}
|
||||
|
||||
public function test_it_orders_buttons_stably_and_preserves_action_confirmation_by_default(): void
|
||||
{
|
||||
$field = RowActions::make()
|
||||
->add(ConfigurableAction::make()->withName('Last'), order: 20)
|
||||
->add(ConfigurableAction::make()->withName('First A'), order: 10)
|
||||
->add(ConfigurableAction::make()->withName('First B'), order: 10);
|
||||
|
||||
$actions = $this->serialize($field, $this->model())['actions'];
|
||||
|
||||
$this->assertSame(
|
||||
['First A', 'First B', 'Last'],
|
||||
array_column(array_column($actions, 'action'), 'name'),
|
||||
);
|
||||
$this->assertFalse($actions[0]['action']['withoutConfirmation']);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function serialize(RowActions $field, RowActionModel $model): array
|
||||
{
|
||||
$field->resolveForDisplay($model);
|
||||
|
||||
return json_decode(json_encode($field, JSON_THROW_ON_ERROR), true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
private function model(
|
||||
int $id = 1,
|
||||
string $state = 'idle',
|
||||
bool $locked = false,
|
||||
bool $visible = true,
|
||||
): RowActionModel {
|
||||
return (new RowActionModel)->forceFill(compact('id', 'state', 'locked', 'visible'));
|
||||
}
|
||||
|
||||
private function novaRequest(): NovaRequest
|
||||
{
|
||||
$baseRequest = Request::create('/nova-api/row-action-models', 'GET');
|
||||
$route = new Route('GET', '/nova-api/{resource}', fn () => null);
|
||||
$route->bind($baseRequest);
|
||||
|
||||
return tap(NovaRequest::createFrom($baseRequest), function (NovaRequest $request) use ($route): void {
|
||||
$request->setRouteResolver(fn () => $route);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class RowActionModel extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
||||
class RowActionResource extends Resource
|
||||
{
|
||||
public static $model = RowActionModel::class;
|
||||
|
||||
public static bool $allowsActions = true;
|
||||
|
||||
public static function uriKey(): string
|
||||
{
|
||||
return 'row-action-models';
|
||||
}
|
||||
|
||||
public function fields(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function authorizedToRunAction(NovaRequest $request, Action $action): bool
|
||||
{
|
||||
return static::$allowsActions;
|
||||
}
|
||||
}
|
||||
|
||||
class ConfigurableAction extends Action
|
||||
{
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name ?? 'Configurable Action';
|
||||
}
|
||||
|
||||
public function uriKey(): string
|
||||
{
|
||||
return 'configurable-action';
|
||||
}
|
||||
|
||||
public function fields(NovaRequest $request): array
|
||||
{
|
||||
return [Text::make('Reason', 'reason')];
|
||||
}
|
||||
|
||||
public function handle(ActionFields $fields, Collection $models): void
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user