Add test validation test for update method and remove name uniqueness constraint

This commit is contained in:
Marcus Moore 2024-11-07 11:02:10 -08:00
parent 8873137ed0
commit c5710b858e
No known key found for this signature in database
3 changed files with 20 additions and 7 deletions

View file

@ -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();

View file

@ -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',

View file

@ -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();