A free open source IT asset/license management system
Allow storing report templates for components
Marcus Moore5f1c459318ee
diff --git a/app/Http/Controllers/ReportTemplatesController.php b/app/Http/Controllers/ReportTemplatesController.phpindex 66bea955f0..4a7c9bff60 100644--- a/app/Http/Controllers/ReportTemplatesController.php+++ b/app/Http/Controllers/ReportTemplatesController.php@@ -18,6 +18,7 @@ class ReportTemplatesController extends Controller$validated = $request->validate(Arr::except((new ReportTemplate)->getRules(), 'options'));$report = $request->user()->reportTemplates()->create([+ 'type' => $validated['type'],'name' => $validated['name'],'options' => $request->except(['_token', 'name', 'is_shared']),'is_shared' => $request->has('is_shared'),diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.phpindex 90b80b4919..5651911078 100644--- a/app/Models/ReportTemplate.php+++ b/app/Models/ReportTemplate.php@@ -26,12 +26,17 @@ class ReportTemplate extends Modelprotected $fillable = ['created_by',+ 'type','name','options','is_shared',];protected $rules = [+ 'type' => [+ 'required',+ 'in:asset,component',+ ],'name' => ['required','string',diff --git a/resources/views/reports/custom/asset.blade.php b/resources/views/reports/custom/asset.blade.phpindex 2e648dc959..8edde51e7d 100644--- a/resources/views/reports/custom/asset.blade.php+++ b/resources/views/reports/custom/asset.blade.php@@ -912,6 +912,12 @@value: $('#name').val(),}).appendTo(form);+ $('<input>').attr({+ type: 'hidden',+ name: 'type',+ value: 'asset',+ }).appendTo(form);+form.attr('action', '{{ route('report-templates.store') }}').submit();});diff --git a/resources/views/reports/custom/component.blade.php b/resources/views/reports/custom/component.blade.phpindex 97c086ee51..a1bf13e194 100644--- a/resources/views/reports/custom/component.blade.php+++ b/resources/views/reports/custom/component.blade.php@@ -608,18 +608,24 @@keepEmptyValues: true,});- {{--$("#savetemplateform").submit(function(e) {--}}- {{-- e.preventDefault(e);--}}-- {{-- let form = $('#custom-report-form');--}}- {{-- $('<input>').attr({--}}- {{-- type: 'hidden',--}}- {{-- name: 'name',--}}- {{-- value: $('#name').val(),--}}- {{-- }).appendTo(form);--}}-- {{-- form.attr('action', '{{ route('report-templates.store') }}').submit();--}}- {{--});--}}+ $("#savetemplateform").submit(function(e) {+ e.preventDefault(e);++ let form = $('#custom-report-form');+ $('<input>').attr({+ type: 'hidden',+ name: 'name',+ value: $('#name').val(),+ }).appendTo(form);++ $('<input>').attr({+ type: 'hidden',+ name: 'type',+ value: 'component',+ }).appendTo(form);++ form.attr('action', '{{ route('report-templates.store') }}').submit();+ });// $('#saved_report_select')// .on('select2:select', function (event) {diff --git a/tests/Feature/ReportTemplates/StoreReportTemplateTest.php b/tests/Feature/ReportTemplates/StoreReportTemplateTest.phpindex 87391e7bbc..b1bba901d3 100644--- a/tests/Feature/ReportTemplates/StoreReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/StoreReportTemplateTest.php@@ -22,9 +22,10 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir{$this->actingAs(User::factory()->canViewReports()->create())->post(route('report-templates.store'), [+ 'type' => 'something-else','name' => '',])- ->assertSessionHasErrors('name');+ ->assertSessionHasErrors(['type', 'name']);}public function test_redirecting_after_validation_error_restores_inputs()@@ -43,24 +44,49 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir}]);}- public function test_can_save_a_report_template()+ public function test_can_save_an_asset_report_template(){$user = User::factory()->canViewReports()->create();- $this->actingAs($user)+ $response = $this->actingAs($user)->post(route('report-templates.store'), [+ 'type' => 'asset','name' => 'My Awesome Template','company' => '1','by_company_id' => ['1', '2'],- ])- ->assertRedirect();+ ]);++ $template = $user->reportTemplates->first(function ($report) {+ return $report->name === 'My Awesome Template';+ });++ $this->assertNotNull($template);+ $this->assertEquals('asset', $template->type);+ $this->assertEquals('1', $template->options['company']);+ $this->assertEquals(['1', '2'], $template->options['by_company_id']);+ $response->assertRedirect(route('report-templates.show', $template));+ }++ public function test_can_save_a_component_report_template()+ {+ $user = User::factory()->canViewReports()->create();++ $response = $this->actingAs($user)+ ->post(route('report-templates.store'), [+ 'type' => 'component',+ 'name' => 'My Awesome Template',+ 'company' => '1',+ 'by_company_id' => ['1', '2'],+ ]);$template = $user->reportTemplates->first(function ($report) {return $report->name === 'My Awesome Template';});$this->assertNotNull($template);+ $this->assertEquals('component', $template->type);$this->assertEquals('1', $template->options['company']);$this->assertEquals(['1', '2'], $template->options['by_company_id']);+ $response->assertRedirect(route('report-templates.show', $template));}}