diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php index 0ec2f854c4..3cc90d3c7e 100644 --- a/app/Models/ReportTemplate.php +++ b/app/Models/ReportTemplate.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Watson\Validating\ValidatingTrait; class ReportTemplate extends Model @@ -42,6 +43,11 @@ class ReportTemplate extends Model }); } + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + public function checkmarkValue(string $property): string { // Assuming we're using the null object pattern, diff --git a/database/factories/ReportTemplateFactory.php b/database/factories/ReportTemplateFactory.php index bb5cac5963..86f861f0ce 100644 --- a/database/factories/ReportTemplateFactory.php +++ b/database/factories/ReportTemplateFactory.php @@ -15,7 +15,9 @@ class ReportTemplateFactory extends Factory { return [ 'name' => $this->faker->word(), - 'options' => [], + 'options' => [ + 'id' => '1', + ], ]; } } diff --git a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php index 5ff0194915..c015e74450 100644 --- a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php +++ b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php @@ -1,7 +1,9 @@ markTestIncomplete(); + $this->actingAs(User::factory()->create()) + ->post(route('report-templates.destroy', 1)) + ->assertForbidden(); } public function testCanDeleteAReportTemplate() { - $this->markTestIncomplete(); + $user = User::factory()->canViewReports()->create(); + $reportTemplate = ReportTemplate::factory()->for($user)->create(); + + $this->actingAs($user) + ->delete(route('report-templates.destroy', $reportTemplate)) + ->assertRedirect(route('reports/custom')); + + $this->assertFalse($reportTemplate->exists()); } } diff --git a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php index 213bc32c88..02036d7b9a 100644 --- a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php +++ b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php @@ -1,6 +1,6 @@ markTestIncomplete(); + $this->actingAs(User::factory()->create()) + ->post(route('report-templates.update', 1)) + ->assertForbidden(); + } + + public function testCanLoadEditReportTemplatePage() + { + $user = User::factory()->canViewReports()->create(); + $reportTemplate = ReportTemplate::factory()->for($user)->create(); + + $this->actingAs($user) + ->get(route('report-templates.edit', $reportTemplate)) + ->assertOk(); } public function testUpdatingReportTemplateRequiresValidFields() { $this->markTestIncomplete(); + + $this->actingAs(User::factory()->canViewReports()->create()) + ->post(route('report-templates.update', ReportTemplate::factory()->create())) + // @todo: name isn't being passed in this case + ->assertSessionHasErrors('name'); } public function testCanUpdateAReportTemplate() @@ -27,12 +44,23 @@ class UpdateReportTemplateTest extends TestCase $user = User::factory()->canViewReports()->create(); - $reportTemplate = ReportTemplate::factory()->for($user)->create(); + $reportTemplate = ReportTemplate::factory()->for($user)->create([ + 'options' => [ + 'id' => 1, + 'company' => 1, + 'by_company_id' => [1, 2], + ], + ]); $this->actingAs($user) - ->get(route('report-templates.edit', $reportTemplate)) + ->post(route('report-templates.update', $reportTemplate), [ + 'id' => 1, + 'by_company_id' => [3], + ]) ->assertOk(); // @todo: + $reportTemplate->fresh(); + dd($reportTemplate->options); } }