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

61 lines
2 KiB
PHP
Raw Normal View History

2024-01-17 13:19:54 -08:00
<?php
2024-01-17 16:25:28 -08:00
namespace Tests\Feature\ReportTemplates;
2024-01-17 13:19:54 -08:00
use App\Models\ReportTemplate;
use App\Models\User;
2024-10-22 14:22:19 -07:00
use Tests\Concerns\TestsPermissionsRequirement;
2024-01-17 13:19:54 -08:00
use Tests\TestCase;
2024-10-22 14:22:19 -07:00
class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequirement
2024-01-17 13:19:54 -08:00
{
2024-10-22 14:22:19 -07:00
public function testRequiresPermission()
2024-01-17 13:19:54 -08:00
{
2024-01-17 16:25:28 -08:00
$this->actingAs(User::factory()->create())
2024-10-28 14:24:47 -07:00
->post($this->getRoute(ReportTemplate::factory()->create()))
2024-01-17 16:25:28 -08:00
->assertForbidden();
}
2024-10-22 17:50:40 -07:00
public function testCannotUpdateAnotherUsersReportTemplate()
2024-01-17 16:25:28 -08:00
{
2024-10-22 17:50:40 -07:00
$this->actingAs(User::factory()->canViewReports()->create())
2024-10-28 14:24:47 -07:00
->post($this->getRoute(ReportTemplate::factory()->create()))
2024-10-22 17:50:40 -07:00
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
2024-01-17 13:19:54 -08:00
}
public function testCanUpdateAReportTemplate()
{
$user = User::factory()->canViewReports()->create();
2024-10-23 16:40:03 -07:00
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create([
2024-01-17 16:25:28 -08:00
'options' => [
'id' => 1,
2024-01-17 16:40:05 -08:00
'category' => 1,
'by_category_id' => 2,
2024-01-17 16:25:28 -08:00
'company' => 1,
'by_company_id' => [1, 2],
],
]);
2024-01-17 13:19:54 -08:00
$this->actingAs($user)
2024-10-28 14:24:47 -07:00
->post($this->getRoute($reportTemplate), [
2024-01-17 16:25:28 -08:00
'id' => 1,
2024-01-17 16:40:05 -08:00
'company' => 1,
2024-01-17 16:25:28 -08:00
'by_company_id' => [3],
2024-01-17 16:40:05 -08:00
]);
$reportTemplate->refresh();
$this->assertEquals(1, $reportTemplate->checkmarkValue('id'));
$this->assertEquals(0, $reportTemplate->checkmarkValue('category'));
$this->assertEquals([], $reportTemplate->selectValues('by_category_id'));
$this->assertEquals(1, $reportTemplate->checkmarkValue('company'));
$this->assertEquals([3], $reportTemplate->selectValues('by_company_id'));
2024-01-17 13:19:54 -08:00
}
2024-10-28 14:24:47 -07:00
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.update', $reportTemplate);
}
2024-01-17 13:19:54 -08:00
}