Files
WMS/Modules/Helpdesk/Tests/Feature/TicketConversationTest.php
T
2026-06-12 15:11:51 +01:00

105 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Modules\Helpdesk\Livewire\TicketShow;
use Modules\Helpdesk\Models\Comment;
use Modules\Helpdesk\Tests\Concerns\BuildsHelpdeskFixtures;
use Tests\TestCase;
final class TicketConversationTest extends TestCase
{
use BuildsHelpdeskFixtures;
use RefreshDatabase;
public function test_owner_can_add_public_response_to_ticket(): void
{
$fixtures = $this->seedLookupData();
$owner = $this->user();
$ticket = $this->ticket(['owner' => $owner, 'status_id' => $fixtures['open']->id, 'priority_id' => $fixtures['normal']->id]);
Livewire::actingAs($owner)
->test(TicketShow::class, ['ticket' => $ticket])
->set('commentBody', '<p>Please can you check this update?</p>')
->call('addComment')
->assertHasNoErrors()
->assertDispatched('helpdesk-comment-added');
$this->assertDatabaseHas('helpdesk_comments', [
'ticket_id' => $ticket->id,
'parent_id' => null,
'user_id' => $owner->id,
'is_internal' => false,
]);
}
public function test_comment_can_have_a_nested_reply(): void
{
$fixtures = $this->seedLookupData();
$owner = $this->user();
$ticket = $this->ticket(['owner' => $owner, 'status_id' => $fixtures['open']->id, 'priority_id' => $fixtures['normal']->id]);
$comment = Comment::query()->create([
'ticket_id' => $ticket->id,
'user_id' => $owner->id,
'body' => '<p>Parent response</p>',
'is_internal' => false,
]);
Livewire::actingAs($owner)
->test(TicketShow::class, ['ticket' => $ticket])
->call('startReply', $comment->id)
->set('replyBody', '<p>Nested reply body</p>')
->call('addReply')
->assertHasNoErrors()
->assertDispatched('helpdesk-reply-added');
$this->assertDatabaseHas('helpdesk_comments', [
'ticket_id' => $ticket->id,
'parent_id' => $comment->id,
'body' => '<p>Nested reply body</p>',
]);
}
public function test_agent_can_update_status_to_closed_and_priority(): void
{
Mail::fake();
$fixtures = $this->seedLookupData();
$owner = $this->user();
$agent = $this->agent();
$ticket = $this->ticket(['owner' => $owner, 'agent' => $agent, 'status_id' => $fixtures['open']->id, 'priority_id' => $fixtures['normal']->id]);
Livewire::actingAs($agent)
->test(TicketShow::class, ['ticket' => $ticket])
->set('status_id', $fixtures['closed']->id)
->call('updateStatus')
->assertHasNoErrors()
->assertDispatched('helpdesk-status-updated')
->set('priority_id', $fixtures['high']->id)
->call('updatePriority')
->assertHasNoErrors()
->assertDispatched('helpdesk-priority-updated');
$ticket->refresh();
self::assertNotNull($ticket->completed_at);
self::assertSame($fixtures['closed']->id, $ticket->status_id);
self::assertSame($fixtures['high']->id, $ticket->priority_id);
}
public function test_unassigned_agent_cannot_update_another_users_ticket(): void
{
$fixtures = $this->seedLookupData();
$owner = $this->user();
$agent = $this->agent();
$ticket = $this->ticket(['owner' => $owner, 'agent_id' => null, 'status_id' => $fixtures['open']->id, 'priority_id' => $fixtures['normal']->id]);
Livewire::actingAs($agent)
->test(TicketShow::class, ['ticket' => $ticket])
->assertForbidden();
}
}