validate enabled bool on extension install through admin

This commit is contained in:
Sky Mulley
2026-10-06 00:24:39 +01:00
parent 6c0413e39b
commit 5fdc4ec5f6
2 changed files with 28 additions and 0 deletions
@@ -47,4 +47,19 @@ class InstallExtensionRequest extends AdminApiRequest
'enable' => $this->boolean('enable', false),
];
}
/**
* @inheritdoc
*/
protected function prepareForValidation(): void
{
if (! $this->has('enable')) {
return;
}
$enable = filter_var($this->input('enable'), FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
if ($enable !== null) {
$this->merge(['enable' => $enable]);
}
}
}
@@ -158,6 +158,19 @@ test('install rejects a package with an invalid manifest', function (): void {
$this->assertDatabaseMissing('extensions', ['identifier' => 'Bad_Probe']);
});
test('install accepts multipart boolean strings for enable', function (string $enable): void {
$this->post(route('api.admin.extensions.install'), ['package' => extensionPackage(['id' => 'Bad_Probe', 'name' => 'Bad', 'version' => '1.0.0']), 'enable' => $enable], ['Accept' => 'application/json'])
->assertUnprocessable()
->assertJsonPath('errors.0.code', 'InvalidExtensionException');
})->with(['true', 'false', '1', '0']);
test('install rejects a non boolean enable value', function (): void {
$response = $this->post(route('api.admin.extensions.install'), ['package' => extensionPackage(['id' => 'admin-fixture', 'name' => 'Fixture', 'version' => '1.0.0']), 'enable' => 'maybe'], ['Accept' => 'application/json']);
$response->assertUnprocessable();
$error = collect($response->json('errors'))->firstWhere('meta.source_field', 'enable');
expect($error)->not->toBeNull();
expect($error['meta']['rule'])->toBe('boolean');
});
test('non admin forbidden', function (string $method, string $routeName, array $parameters = []): void {
writeExtension('admin-fixture');
$this->actingAsNonAdmin();