134 lines
4.8 KiB
PHP
Executable File
134 lines
4.8 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Tests\Feature;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Helpdesk\Models\Attachment;
|
|
use Modules\Helpdesk\Models\GitCommit;
|
|
use Modules\Helpdesk\Services\AttachmentService;
|
|
use Modules\Helpdesk\Tests\Concerns\BuildsHelpdeskFixtures;
|
|
use Tests\TestCase;
|
|
|
|
final class GitAndAttachmentTest extends TestCase
|
|
{
|
|
use BuildsHelpdeskFixtures;
|
|
use RefreshDatabase;
|
|
|
|
public function test_git_commit_can_be_tracked_against_repository_and_ticket(): void
|
|
{
|
|
$fixtures = $this->seedLookupData();
|
|
$repository = $this->repository(['project_id' => $fixtures['project']->id]);
|
|
$ticket = $this->ticket(['project_id' => $fixtures['project']->id]);
|
|
|
|
GitCommit::query()->create([
|
|
'repository_id' => $repository->id,
|
|
'ticket_id' => $ticket->id,
|
|
'hash' => str_repeat('b', 40),
|
|
'short_hash' => 'bbbbbbb',
|
|
'branch' => 'develop',
|
|
'author_name' => 'Mark Gregory',
|
|
'author_email' => 'mark@example.com',
|
|
'message' => 'Resolve ' . $ticket->reference . ' ticket save redirect',
|
|
'url' => 'https://example.com/commit/b',
|
|
'committed_at' => now(),
|
|
]);
|
|
|
|
$this->assertDatabaseHas('helpdesk_git_commits', [
|
|
'repository_id' => $repository->id,
|
|
'ticket_id' => $ticket->id,
|
|
'short_hash' => 'bbbbbbb',
|
|
]);
|
|
|
|
self::assertSame($ticket->id, $repository->commits()->firstOrFail()->ticket_id);
|
|
}
|
|
|
|
public function test_existing_ticket_supporting_files_route_stores_multiple_files(): void
|
|
{
|
|
Storage::fake('local');
|
|
config()->set('helpdesk.disk', 'local');
|
|
|
|
$user = $this->admin();
|
|
$ticket = $this->ticket(['owner' => $user]);
|
|
|
|
$this->actingAs($user)
|
|
->post(route('helpdesk.tickets.attachments.store', $ticket), [
|
|
'attachments' => [
|
|
UploadedFile::fake()->create('first-log.txt', 4, 'text/plain'),
|
|
UploadedFile::fake()->create('second-proof.pdf', 8, 'application/pdf'),
|
|
],
|
|
])
|
|
->assertRedirect(route('helpdesk.tickets.show', $ticket));
|
|
|
|
$this->assertDatabaseHas('helpdesk_attachments', [
|
|
'ticket_id' => $ticket->id,
|
|
'comment_id' => null,
|
|
'original_name' => 'first-log.txt',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('helpdesk_attachments', [
|
|
'ticket_id' => $ticket->id,
|
|
'comment_id' => null,
|
|
'original_name' => 'second-proof.pdf',
|
|
]);
|
|
}
|
|
|
|
public function test_existing_ticket_supporting_files_route_accepts_source_code_attachments(): void
|
|
{
|
|
Storage::fake('local');
|
|
config()->set('helpdesk.disk', 'local');
|
|
|
|
$user = $this->admin();
|
|
$ticket = $this->ticket(['owner' => $user]);
|
|
|
|
$this->actingAs($user)
|
|
->post(route('helpdesk.tickets.attachments.store', $ticket), [
|
|
'attachments' => [
|
|
UploadedFile::fake()->create('TicketStoreController.php', 4, 'text/plain'),
|
|
UploadedFile::fake()->create('ticket-editor.js', 4, 'text/plain'),
|
|
UploadedFile::fake()->create('warehouse-sync.rpgle', 4, 'text/plain'),
|
|
],
|
|
])
|
|
->assertRedirect(route('helpdesk.tickets.show', $ticket));
|
|
|
|
$this->assertDatabaseHas('helpdesk_attachments', [
|
|
'ticket_id' => $ticket->id,
|
|
'original_name' => 'TicketStoreController.php',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('helpdesk_attachments', [
|
|
'ticket_id' => $ticket->id,
|
|
'original_name' => 'ticket-editor.js',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('helpdesk_attachments', [
|
|
'ticket_id' => $ticket->id,
|
|
'original_name' => 'warehouse-sync.rpgle',
|
|
]);
|
|
}
|
|
|
|
public function test_attachment_service_stores_and_deletes_ticket_files(): void
|
|
{
|
|
Storage::fake('local');
|
|
config()->set('helpdesk.disk', 'local');
|
|
|
|
$user = $this->user();
|
|
$ticket = $this->ticket(['owner' => $user]);
|
|
$file = UploadedFile::fake()->create('evidence.pdf', 32, 'application/pdf');
|
|
|
|
app(AttachmentService::class)->storeMany($ticket, [$file], $user->id);
|
|
|
|
$attachment = Attachment::query()->where('ticket_id', $ticket->id)->firstOrFail();
|
|
Storage::disk('local')->assertExists($attachment->path);
|
|
self::assertSame('evidence.pdf', $attachment->original_name);
|
|
|
|
app(AttachmentService::class)->delete($attachment);
|
|
|
|
Storage::disk('local')->assertMissing($attachment->path);
|
|
$this->assertDatabaseMissing('helpdesk_attachments', ['id' => $attachment->id]);
|
|
}
|
|
}
|