actingAs(User::factory()->create()) ->post($this->getRoute()) ->assertForbidden(); } public function testSavingReportTemplateRequiresValidFields() { $this->actingAs(User::factory()->canViewReports()->create()) ->post($this->getRoute(), [ 'name' => '', ]) ->assertSessionHasErrors('name'); } public function testRedirectingAfterValidationErrorRestoresInputs() { $this->actingAs(User::factory()->canViewReports()->create()) // start on the custom report page ->from(route('reports/custom')) ->followingRedirects() ->post($this->getRoute(), [ 'name' => '', // set some values to ensure they are still present // when returning to the custom report page. 'by_company_id' => [2, 3] ])->assertViewHas(['template' => function (ReportTemplate $reportTemplate) { return data_get($reportTemplate, 'options.by_company_id') === [2, 3]; }]); } public function testCanSaveAReportTemplate() { $user = User::factory()->canViewReports()->create(); $this->actingAs($user) ->post($this->getRoute(), [ '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('1', $template->options['company']); $this->assertEquals(['1', '2'], $template->options['by_company_id']); } private function getRoute(): string { return route('report-templates.store'); } }