Skip to content

A free open source IT asset/license management system

Add validation

Marcus Moore00fff45c5f1f
diff --git a/app/Http/Controllers/Reports/CustomComponentReportController.php b/app/Http/Controllers/Reports/CustomComponentReportController.php
index 5b94f42627..14083353bc 100644
--- a/app/Http/Controllers/Reports/CustomComponentReportController.php
+++ b/app/Http/Controllers/Reports/CustomComponentReportController.php
@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
+use App\Http\Requests\CustomComponentReportRequest;
use App\Models\Actionlog;
use App\Models\Component;
use App\Models\ReportTemplate;
@@ -37,7 +38,7 @@ class CustomComponentReportController extends Controller
]);
}
- public function run(Request $request)
+ public function run(CustomComponentReportRequest $request)
{
$this->authorize('reports.view');
diff --git a/app/Http/Requests/CustomComponentReportRequest.php b/app/Http/Requests/CustomComponentReportRequest.php
index b1c70eadb3..a380f43ba6 100644
--- a/app/Http/Requests/CustomComponentReportRequest.php
+++ b/app/Http/Requests/CustomComponentReportRequest.php
@@ -2,19 +2,51 @@
namespace App\Http\Requests;
-use Illuminate\Foundation\Http\FormRequest;
-
-class CustomComponentReportRequest extends FormRequest
+class CustomComponentReportRequest extends Request
{
public function authorize(): bool
{
return true;
}
+ public function prepareForValidation(): void
+ {
+ if ($this->filled('quantity_end') && ! $this->filled('quantity_start')) {
+ $this->merge(['quantity_start' => 0]);
+ }
+
+ if ($this->filled('min_quantity_end') && ! $this->filled('min_quantity_start')) {
+ $this->merge(['min_quantity_start' => 0]);
+ }
+
+ if ($this->filled('unit_cost_end') && ! $this->filled('unit_cost_start')) {
+ $this->merge(['unit_cost_start' => 0]);
+ }
+ }
+
public function rules(): array
{
return [
- //
+ 'purchase_start' => 'date|date_format:Y-m-d|nullable',
+ 'purchase_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:purchase_start',
+ 'quantity_start' => 'numeric|nullable',
+ 'quantity_end' => 'numeric|nullable|gte:quantity_start',
+ 'min_quantity_start' => 'numeric|nullable',
+ 'min_quantity_end' => 'numeric|nullable|gte:min_quantity_start',
+ 'unit_cost_start' => 'numeric|nullable',
+ 'unit_cost_end' => 'numeric|nullable|gte:unit_cost_start',
+ 'checkout_date_start' => 'date|date_format:Y-m-d|nullable',
+ 'checkout_date_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:checkout_date_start',
+ 'created_start' => 'date|date_format:Y-m-d|nullable',
+ 'created_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:created_start',
+ 'last_updated_start' => 'date|date_format:Y-m-d|nullable',
+ 'last_updated_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:last_updated_start',
+ 'last_updated_before' => 'integer|nullable',
];
}
+
+ public function response(array $errors)
+ {
+ return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
+ }
}
diff --git a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
index f375e3a90e..e8ff91072e 100644
--- a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
+++ b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
@@ -52,15 +52,59 @@ class CustomComponentReportTest extends TestCase
public function test_custom_component_report_validation()
{
- $this->markTestIncomplete();
-
- // todo: purchase_start and purchase_end
- // todo: quantity
- // todo: min quantity
- // todo: unit cost
- // todo: checkout
- // todo: created_at
- // todo: updated_at
+ // Invalid date formats are rejected
+ $this->sendRequest([
+ 'purchase_start' => 'not-a-date',
+ 'purchase_end' => 'not-a-date',
+ 'checkout_date_start' => 'not-a-date',
+ 'checkout_date_end' => 'not-a-date',
+ 'created_start' => 'not-a-date',
+ 'created_end' => 'not-a-date',
+ 'last_updated_start' => 'not-a-date',
+ 'last_updated_end' => 'not-a-date',
+ ])->assertSessionHasErrors([
+ 'purchase_start', 'purchase_end',
+ 'checkout_date_start', 'checkout_date_end',
+ 'created_start', 'created_end',
+ 'last_updated_start', 'last_updated_end',
+ ]);
+
+ // End date must be on or after start date
+ $this->sendRequest([
+ 'purchase_start' => '2024-12-31',
+ 'purchase_end' => '2024-01-01',
+ 'checkout_date_start' => '2024-12-31',
+ 'checkout_date_end' => '2024-01-01',
+ 'created_start' => '2024-12-31',
+ 'created_end' => '2024-01-01',
+ 'last_updated_start' => '2024-12-31',
+ 'last_updated_end' => '2024-01-01',
+ ])->assertSessionHasErrors([
+ 'purchase_end', 'checkout_date_end', 'created_end', 'last_updated_end',
+ ]);
+
+ // Non-numeric values are rejected, and last_updated_before must be an integer
+ $this->sendRequest([
+ 'quantity_start' => 'abc',
+ 'quantity_end' => 'abc',
+ 'min_quantity_start' => 'abc',
+ 'min_quantity_end' => 'abc',
+ 'unit_cost_start' => 'abc',
+ 'unit_cost_end' => 'abc',
+ 'last_updated_before' => 'not-an-integer',
+ ])->assertSessionHasErrors([
+ 'quantity_start', 'quantity_end',
+ 'min_quantity_start', 'min_quantity_end',
+ 'unit_cost_start', 'unit_cost_end',
+ 'last_updated_before',
+ ]);
+
+ // End must be >= start for numeric ranges
+ $this->sendRequest([
+ 'quantity_start' => 10, 'quantity_end' => 1,
+ 'min_quantity_start' => 10, 'min_quantity_end' => 1,
+ 'unit_cost_start' => 100, 'unit_cost_end' => 1,
+ ])->assertSessionHasErrors(['quantity_end', 'min_quantity_end', 'unit_cost_end']);
}
public function test_custom_component_report_headers()