3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-04-02 22:11:01 +00:00
Files
snipe-it/tests/Feature/ReportTemplates/StoreReportTemplateTest.php
2026-03-16 17:40:57 -07:00

67 lines
2.2 KiB
PHP

<?php
namespace Tests\Feature\ReportTemplates;
use App\Models\ReportTemplate;
use App\Models\User;
use PHPUnit\Framework\Attributes\Group;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
#[Group('custom-reporting')]
class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequirement
{
public function test_requires_permission()
{
$this->actingAs(User::factory()->create())
->post(route('report-templates.store'))
->assertForbidden();
}
public function test_saving_report_template_requires_valid_fields()
{
$this->actingAs(User::factory()->canViewReports()->create())
->post(route('report-templates.store'), [
'name' => '',
])
->assertSessionHasErrors('name');
}
public function test_redirecting_after_validation_error_restores_inputs()
{
$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(['template' => function (ReportTemplate $reportTemplate) {
return data_get($reportTemplate, 'options.by_company_id') === [2, 3];
}]);
}
public function test_can_save_a_report_template()
{
$user = User::factory()->canViewReports()->create();
$this->actingAs($user)
->post(route('report-templates.store'), [
'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']);
}
}