Skip to content

Commit 00fff45

Browse files
committed
Add validation
1 parent 9c042ef commit 00fff45

3 files changed

Lines changed: 91 additions & 14 deletions

File tree

app/Http/Controllers/Reports/CustomComponentReportController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers\Reports;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\CustomComponentReportRequest;
67
use App\Models\Actionlog;
78
use App\Models\Component;
89
use App\Models\ReportTemplate;
@@ -37,7 +38,7 @@ public function show(Request $request)
3738
]);
3839
}
3940

40-
public function run(Request $request)
41+
public function run(CustomComponentReportRequest $request)
4142
{
4243
$this->authorize('reports.view');
4344

app/Http/Requests/CustomComponentReportRequest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,51 @@
22

33
namespace App\Http\Requests;
44

5-
use Illuminate\Foundation\Http\FormRequest;
6-
7-
class CustomComponentReportRequest extends FormRequest
5+
class CustomComponentReportRequest extends Request
86
{
97
public function authorize(): bool
108
{
119
return true;
1210
}
1311

12+
public function prepareForValidation(): void
13+
{
14+
if ($this->filled('quantity_end') && ! $this->filled('quantity_start')) {
15+
$this->merge(['quantity_start' => 0]);
16+
}
17+
18+
if ($this->filled('min_quantity_end') && ! $this->filled('min_quantity_start')) {
19+
$this->merge(['min_quantity_start' => 0]);
20+
}
21+
22+
if ($this->filled('unit_cost_end') && ! $this->filled('unit_cost_start')) {
23+
$this->merge(['unit_cost_start' => 0]);
24+
}
25+
}
26+
1427
public function rules(): array
1528
{
1629
return [
17-
//
30+
'purchase_start' => 'date|date_format:Y-m-d|nullable',
31+
'purchase_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:purchase_start',
32+
'quantity_start' => 'numeric|nullable',
33+
'quantity_end' => 'numeric|nullable|gte:quantity_start',
34+
'min_quantity_start' => 'numeric|nullable',
35+
'min_quantity_end' => 'numeric|nullable|gte:min_quantity_start',
36+
'unit_cost_start' => 'numeric|nullable',
37+
'unit_cost_end' => 'numeric|nullable|gte:unit_cost_start',
38+
'checkout_date_start' => 'date|date_format:Y-m-d|nullable',
39+
'checkout_date_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:checkout_date_start',
40+
'created_start' => 'date|date_format:Y-m-d|nullable',
41+
'created_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:created_start',
42+
'last_updated_start' => 'date|date_format:Y-m-d|nullable',
43+
'last_updated_end' => 'date|date_format:Y-m-d|nullable|after_or_equal:last_updated_start',
44+
'last_updated_before' => 'integer|nullable',
1845
];
1946
}
47+
48+
public function response(array $errors)
49+
{
50+
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
51+
}
2052
}

tests/Feature/Reporting/Custom/CustomComponentReportTest.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,59 @@ public function test_can_load_custom_report_page()
5252

5353
public function test_custom_component_report_validation()
5454
{
55-
$this->markTestIncomplete();
56-
57-
// todo: purchase_start and purchase_end
58-
// todo: quantity
59-
// todo: min quantity
60-
// todo: unit cost
61-
// todo: checkout
62-
// todo: created_at
63-
// todo: updated_at
55+
// Invalid date formats are rejected
56+
$this->sendRequest([
57+
'purchase_start' => 'not-a-date',
58+
'purchase_end' => 'not-a-date',
59+
'checkout_date_start' => 'not-a-date',
60+
'checkout_date_end' => 'not-a-date',
61+
'created_start' => 'not-a-date',
62+
'created_end' => 'not-a-date',
63+
'last_updated_start' => 'not-a-date',
64+
'last_updated_end' => 'not-a-date',
65+
])->assertSessionHasErrors([
66+
'purchase_start', 'purchase_end',
67+
'checkout_date_start', 'checkout_date_end',
68+
'created_start', 'created_end',
69+
'last_updated_start', 'last_updated_end',
70+
]);
71+
72+
// End date must be on or after start date
73+
$this->sendRequest([
74+
'purchase_start' => '2024-12-31',
75+
'purchase_end' => '2024-01-01',
76+
'checkout_date_start' => '2024-12-31',
77+
'checkout_date_end' => '2024-01-01',
78+
'created_start' => '2024-12-31',
79+
'created_end' => '2024-01-01',
80+
'last_updated_start' => '2024-12-31',
81+
'last_updated_end' => '2024-01-01',
82+
])->assertSessionHasErrors([
83+
'purchase_end', 'checkout_date_end', 'created_end', 'last_updated_end',
84+
]);
85+
86+
// Non-numeric values are rejected, and last_updated_before must be an integer
87+
$this->sendRequest([
88+
'quantity_start' => 'abc',
89+
'quantity_end' => 'abc',
90+
'min_quantity_start' => 'abc',
91+
'min_quantity_end' => 'abc',
92+
'unit_cost_start' => 'abc',
93+
'unit_cost_end' => 'abc',
94+
'last_updated_before' => 'not-an-integer',
95+
])->assertSessionHasErrors([
96+
'quantity_start', 'quantity_end',
97+
'min_quantity_start', 'min_quantity_end',
98+
'unit_cost_start', 'unit_cost_end',
99+
'last_updated_before',
100+
]);
101+
102+
// End must be >= start for numeric ranges
103+
$this->sendRequest([
104+
'quantity_start' => 10, 'quantity_end' => 1,
105+
'min_quantity_start' => 10, 'min_quantity_end' => 1,
106+
'unit_cost_start' => 100, 'unit_cost_end' => 1,
107+
])->assertSessionHasErrors(['quantity_end', 'min_quantity_end', 'unit_cost_end']);
64108
}
65109

66110
public function test_custom_component_report_headers()

0 commit comments

Comments
 (0)