diff --git a/app/Http/Controllers/ReportTemplatesController.php b/app/Http/Controllers/ReportTemplatesController.php index a05e477b37..27d56369de 100644 --- a/app/Http/Controllers/ReportTemplatesController.php +++ b/app/Http/Controllers/ReportTemplatesController.php @@ -15,10 +15,10 @@ class ReportTemplatesController extends Controller $this->authorize('reports.view'); // Ignore "options" rules since data does not come in under that key... - $request->validate(Arr::except((new ReportTemplate)->getRules(), 'options')); + $validated = $request->validate(Arr::except((new ReportTemplate)->getRules(), 'options')); $report = $request->user()->reportTemplates()->create([ - 'name' => $request->get('name'), + 'name' => $validated['name'], 'options' => $request->except(['_token', 'name']), ]); @@ -55,9 +55,10 @@ class ReportTemplatesController extends Controller { $this->authorize('reports.view'); - // @todo: validation + // Ignore "options" rules since data does not come in under that key... + $validated = $request->validate(Arr::except((new ReportTemplate)->getRules(), 'options')); - $reportTemplate->name = $request->input('name'); + $reportTemplate->name = $validated['name']; $reportTemplate->options = $request->except(['_token', 'name']); $reportTemplate->save(); diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php index 2be8ea3ed4..d1a00c5657 100644 --- a/app/Models/ReportTemplate.php +++ b/app/Models/ReportTemplate.php @@ -24,11 +24,8 @@ class ReportTemplate extends Model ]; protected $rules = [ - // @todo: this should probably be unique for each user so people don't get errors trying to use a name someone else already used... - // @todo: but enabling shared reports in the future would mean we would have name collisions then... 'name' => [ 'required', - 'unique:report_templates,name', ], 'options' => [ 'required', diff --git a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php index 5c94fd3e2c..8d4df153a6 100644 --- a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php +++ b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php @@ -25,6 +25,21 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi ->assertNotFound(); } + public function testUpdatingReportTemplateRequiresValidFields() + { + $user = User::factory()->canViewReports()->create(); + + $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create(); + + $this->actingAs($user) + ->post($this->getRoute($reportTemplate), [ + // + ]) + ->assertSessionHasErrors([ + 'name' => 'The name field is required.', + ]); + } + public function testCanUpdateAReportTemplate() { $user = User::factory()->canViewReports()->create();