Certificate manager built on Laravel 13, Livewire 4, and Flux. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
use App\Models\Certificate;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\Livewire;
|
|
|
|
beforeEach(function () {
|
|
Storage::fake('local');
|
|
});
|
|
|
|
function storedCertificate(User $user, array $attributes = []): Certificate
|
|
{
|
|
$path = 'certificates/'.$user->id.'/'.uniqid().'.pdf';
|
|
Storage::disk('local')->put($path, 'file-contents');
|
|
|
|
return Certificate::factory()->for($user)->create(array_merge(['file_path' => $path], $attributes));
|
|
}
|
|
|
|
test('an owner can download their certificate', function () {
|
|
$user = User::factory()->create();
|
|
$certificate = storedCertificate($user);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$this->get(route('certificates.download', $certificate))->assertOk();
|
|
});
|
|
|
|
test('a user cannot download another users certificate', function () {
|
|
$certificate = storedCertificate(User::factory()->create());
|
|
|
|
$this->actingAs(User::factory()->create());
|
|
|
|
$this->get(route('certificates.download', $certificate))->assertNotFound();
|
|
});
|
|
|
|
test('editing a certificate updates its details', function () {
|
|
$user = User::factory()->create();
|
|
$certificate = storedCertificate($user, ['title' => 'Old']);
|
|
|
|
$this->actingAs($user);
|
|
|
|
Livewire::test('pages::certificates.edit', ['certificate' => $certificate])
|
|
->set('title', 'New Title')
|
|
->set('issuer', 'New Issuer')
|
|
->set('certificate_number', 'NR-42')
|
|
->set('issued_at', now()->subYear()->toDateString())
|
|
->set('expires_at', now()->addYear()->toDateString())
|
|
->set('notes', 'Updated notes.')
|
|
->call('save')
|
|
->assertHasNoErrors();
|
|
|
|
$certificate->refresh();
|
|
|
|
expect($certificate->title)->toBe('New Title')
|
|
->and($certificate->issuer)->toBe('New Issuer')
|
|
->and($certificate->certificate_number)->toBe('NR-42')
|
|
->and($certificate->notes)->toBe('Updated notes.');
|
|
});
|
|
|
|
test('deleting a certificate removes its stored file', function () {
|
|
$user = User::factory()->create();
|
|
$certificate = storedCertificate($user);
|
|
$path = $certificate->file_path;
|
|
|
|
$this->actingAs($user);
|
|
|
|
Livewire::test('pages::certificates.index')
|
|
->call('delete', $certificate->id);
|
|
|
|
expect(Certificate::find($certificate->id))->toBeNull();
|
|
Storage::disk('local')->assertMissing($path);
|
|
});
|