snipe-it/tests/Feature/ReportTemplates/ReportTemplateTest.php

92 lines
3.1 KiB
PHP
Raw Normal View History

2023-12-11 14:29:33 -08:00
<?php
2023-12-18 12:55:48 -08:00
namespace Tests\Feature\ReportTemplates;
2023-12-11 14:29:33 -08:00
2023-12-18 12:55:48 -08:00
use App\Models\ReportTemplate;
2023-12-11 14:29:33 -08:00
use App\Models\User;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
2023-12-18 12:55:48 -08:00
class ReportTemplateTest extends TestCase
2023-12-11 14:29:33 -08:00
{
use InteractsWithSettings;
public function testCanLoadCustomReportPage()
{
$this->actingAs(User::factory()->canViewReports()->create())
->get(route('reports/custom'))
->assertOk()
2023-12-18 12:55:48 -08:00
->assertViewHas(['reportTemplate' => function (ReportTemplate $report) {
2023-12-11 14:29:33 -08:00
// the view should have an empty report by default
return $report->exists() === false;
}]);
}
2023-12-20 16:37:27 -08:00
public function testCanLoadASavedReportTemplate()
2023-12-11 14:29:33 -08:00
{
$user = User::factory()->canViewReports()->create();
2023-12-20 16:37:27 -08:00
$reportTemplate = ReportTemplate::factory()->make(['name' => 'My Awesome Template']);
2023-12-18 12:55:48 -08:00
$user->reportTemplates()->save($reportTemplate);
2023-12-11 14:29:33 -08:00
$this->actingAs($user)
2023-12-20 16:37:27 -08:00
->get(route('report-templates.show', $reportTemplate))
2023-12-11 14:29:33 -08:00
->assertOk()
2023-12-18 12:55:48 -08:00
->assertViewHas(['reportTemplate' => function (ReportTemplate $viewReport) use ($reportTemplate) {
return $viewReport->is($reportTemplate);
2023-12-11 14:29:33 -08:00
}]);
}
2023-12-20 16:37:27 -08:00
public function testCanSaveAReportTemplate()
2023-12-11 14:29:33 -08:00
{
$user = User::factory()->canViewReports()->create();
$this->actingAs($user)
2023-12-18 12:55:48 -08:00
->post(route('report-templates.store'), [
2023-12-20 16:37:27 -08:00
'name' => 'My Awesome Template',
'company' => '1',
'by_company_id' => ['1', '2'],
])
->assertRedirect();
2023-12-18 12:55:48 -08:00
$template = $user->reportTemplates->first(function ($report) {
2023-12-20 16:37:27 -08:00
return $report->name === 'My Awesome Template';
});
2023-12-18 12:55:48 -08:00
$this->assertNotNull($template);
$this->assertEquals('1', $template->options['company']);
$this->assertEquals(['1', '2'], $template->options['by_company_id']);
2023-12-11 14:29:33 -08:00
}
2023-12-20 16:37:27 -08:00
public function testReportTemplateRequiresValidFields()
{
$this->actingAs(User::factory()->canViewReports()->create())
2023-12-18 12:55:48 -08:00
->post(route('report-templates.store'), [
2024-01-02 18:14:17 -08:00
'name' => '',
])
2023-12-11 16:20:36 -08:00
->assertSessionHasErrors('name');
}
2024-01-02 17:47:52 -08:00
public function testRedirectingAfterValidationErrorRestoresInputs()
{
2024-01-02 18:14:17 -08:00
$this->actingAs(User::factory()->canViewReports()->create())
// start on the custom report page
->from(route('reports/custom'))
->followingRedirects()
->post(route('report-templates.store'), [
'name' => '',
// set some values to ensure they are still present
// when returning to the custom report page.
'by_company_id' => [2, 3]
])->assertViewHas(['reportTemplate' => function (ReportTemplate $reportTemplate) {
return data_get($reportTemplate, 'options.by_company_id') === [2, 3];
}]);
2024-01-02 17:47:52 -08:00
}
2023-12-20 16:37:27 -08:00
public function testSavingReportTemplateRequiresCorrectPermission()
{
$this->actingAs(User::factory()->create())
2023-12-18 12:55:48 -08:00
->post(route('report-templates.store'))
->assertForbidden();
}
2023-12-11 14:29:33 -08:00
}