Add validation for saving reports

This commit is contained in:
Marcus Moore 2023-12-11 16:20:36 -08:00
parent b9cda88363
commit 89c47c1879
No known key found for this signature in database
4 changed files with 15 additions and 17 deletions

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\SavedReport;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SavedReportsController extends Controller class SavedReportsController extends Controller
@ -10,9 +11,11 @@ class SavedReportsController extends Controller
{ {
$this->authorize('reports.view'); $this->authorize('reports.view');
$request->validate((new SavedReport)->getRules());
$report = $request->user()->savedReports()->create([ $report = $request->user()->savedReports()->create([
'name' => $request->get('report_name'), 'name' => $request->get('name'),
'options' => $request->except(['_token', 'report_name']), 'options' => $request->except(['_token', 'name']),
]); ]);
return redirect()->route('reports/custom', ['report' => $report->id]); return redirect()->route('reports/custom', ['report' => $report->id]);

View file

@ -4,10 +4,12 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
class SavedReport extends Model class SavedReport extends Model
{ {
use HasFactory; use HasFactory;
use ValidatingTrait;
/** /**
* The attributes that should be cast. * The attributes that should be cast.
@ -25,6 +27,10 @@ class SavedReport extends Model
]; ];
// @todo: add $rules // @todo: add $rules
protected $rules = [
'name' => 'required|unique:saved_reports,name',
'options' => 'array',
];
//we will need a bit to catch and store the name of the report. //we will need a bit to catch and store the name of the report.
//for now the blip above is creating the name, but can be confusing if multiple are made at once //for now the blip above is creating the name, but can be confusing if multiple are made at once

View file

@ -375,7 +375,7 @@
<form method="post" id="savetemplateform" action="{{ route("savedreports/store") }}"> <form method="post" id="savetemplateform" action="{{ route("savedreports/store") }}">
@csrf @csrf
<input type="hidden" id="savetemplateform" name="options"> <input type="hidden" id="savetemplateform" name="options">
<input type="text" id="report_name" name="report_name"> <input type="text" id="name" name="name">
{{-- this will be a box to name the report? --}} {{-- this will be a box to name the report? --}}
<button class = "btn btn-primary" style="width: 100%"> <button class = "btn btn-primary" style="width: 100%">
{{ trans('admin/reports/general.save_template') }} {{ trans('admin/reports/general.save_template') }}
@ -464,8 +464,8 @@
$('<input>').attr({ $('<input>').attr({
type: 'hidden', type: 'hidden',
name: 'report_name', name: 'name',
value: $('#report_name').val(), value: $('#name').val(),
}).appendTo(form); }).appendTo(form);
form.attr('action', '/reports/savedtemplate').submit(); form.attr('action', '/reports/savedtemplate').submit();

View file

@ -36,15 +36,6 @@ class SavedReportsTest extends TestCase
}]); }]);
} }
public function testCanOnlySeeOwnSavedCustomReports()
{
$this->markTestIncomplete('potentially...');
// create saved reports for two users
// load the route('reports/custom')
// ensure the view only has the current users reports ($saved_reports)
}
public function testCanSaveACustomReport() public function testCanSaveACustomReport()
{ {
$this->markTestIncomplete(); $this->markTestIncomplete();
@ -52,13 +43,11 @@ class SavedReportsTest extends TestCase
public function testSavingReportRequiresValidFields() public function testSavingReportRequiresValidFields()
{ {
$this->markTestIncomplete();
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->post(route('savedreports/store'), [ ->post(route('savedreports/store'), [
// //
]) ])
->assertSessionHasErrors('report_name'); ->assertSessionHasErrors('name');
} }
public function testSavingReportRequiresCorrectPermission() public function testSavingReportRequiresCorrectPermission()