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-28 14:42:44 -07:00
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
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-28 14:42:44 -07:00
|
|
|
#[Group('custom-reporting')]
|
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-11-07 16:54:55 -08:00
|
|
|
->post(route('report-templates.update', ReportTemplate::factory()->create()))
|
2024-10-31 12:34:06 -07:00
|
|
|
->assertNotFound();
|
2024-01-17 16:25:28 -08:00
|
|
|
}
|
|
|
|
|
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-11-07 16:54:55 -08:00
|
|
|
->post(route('report-templates.update', ReportTemplate::factory()->create()))
|
2024-10-31 12:34:06 -07:00
|
|
|
->assertNotFound();
|
2024-01-17 13:19:54 -08:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:02:10 -08:00
|
|
|
public function testUpdatingReportTemplateRequiresValidFields()
|
|
|
|
{
|
|
|
|
$user = User::factory()->canViewReports()->create();
|
|
|
|
|
|
|
|
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
2024-11-07 16:54:55 -08:00
|
|
|
->post(route('report-templates.update', $reportTemplate), [
|
2024-11-07 11:02:10 -08:00
|
|
|
//
|
|
|
|
])
|
|
|
|
->assertSessionHasErrors([
|
|
|
|
'name' => 'The name field is required.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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-11-06 12:29:31 -08:00
|
|
|
'name' => 'Original Name',
|
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-11-07 16:54:55 -08:00
|
|
|
->post(route('report-templates.update', $reportTemplate), [
|
2024-11-06 12:29:31 -08:00
|
|
|
'name' => 'Updated Name',
|
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();
|
2024-11-06 12:29:31 -08:00
|
|
|
$this->assertEquals('Updated Name', $reportTemplate->name);
|
2024-01-17 16:40:05 -08:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|