|
5 | 5 | use App\Models\User; |
6 | 6 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
7 | 7 | use Illuminate\Http\UploadedFile; |
| 8 | +use Illuminate\Support\Facades\Storage; |
8 | 9 | use Livewire\Livewire; |
9 | 10 |
|
10 | 11 | test('guests are redirected to the login page', function () { |
|
392 | 393 | }); |
393 | 394 | }); |
394 | 395 |
|
| 396 | +describe('can manage item photos', function () { |
| 397 | + test('can create an item with a photo', function () { |
| 398 | + Storage::fake(); |
| 399 | + $user = User::factory()->withTeam()->create(); |
| 400 | + $photo = UploadedFile::fake()->image('guitar.jpg'); |
| 401 | + |
| 402 | + Livewire::actingAs($user) |
| 403 | + ->test('pages::inventory.index') |
| 404 | + ->set('form.name', 'Guitar') |
| 405 | + ->set('form.type', ItemType::Item->value) |
| 406 | + ->set('form.photo', $photo) |
| 407 | + ->call('save') |
| 408 | + ->assertHasNoErrors(); |
| 409 | + |
| 410 | + $item = Item::where('name', 'Guitar')->first(); |
| 411 | + |
| 412 | + expect($item->photo_path)->not->toBeNull(); |
| 413 | + Storage::assertExists($item->photo_path); |
| 414 | + }); |
| 415 | + |
| 416 | + test('can create an item without a photo', function () { |
| 417 | + $user = User::factory()->withTeam()->create(); |
| 418 | + |
| 419 | + Livewire::actingAs($user) |
| 420 | + ->test('pages::inventory.index') |
| 421 | + ->set('form.name', 'Screwdriver') |
| 422 | + ->set('form.type', ItemType::Item->value) |
| 423 | + ->call('save') |
| 424 | + ->assertHasNoErrors(); |
| 425 | + |
| 426 | + expect(Item::where('name', 'Screwdriver')->first()->photo_path)->toBeNull(); |
| 427 | + }); |
| 428 | + |
| 429 | + test('can update an item with a new photo', function () { |
| 430 | + Storage::fake(); |
| 431 | + $user = User::factory()->withTeam()->create(); |
| 432 | + $item = Item::factory()->for($user->currentTeam)->create(['name' => 'Guitar', 'photo_path' => 'teams/1/items/guitar.jpg']); |
| 433 | + Storage::put('teams/1/items/guitar.jpg', 'old'); |
| 434 | + |
| 435 | + $newPhoto = UploadedFile::fake()->image('new-guitar.png'); |
| 436 | + |
| 437 | + Livewire::actingAs($user) |
| 438 | + ->test('pages::inventory.index') |
| 439 | + ->call('edit', $item->id) |
| 440 | + ->set('form.photo', $newPhoto) |
| 441 | + ->call('save') |
| 442 | + ->assertHasNoErrors(); |
| 443 | + |
| 444 | + $item->refresh(); |
| 445 | + |
| 446 | + expect($item->photo_path)->not->toBeNull(); |
| 447 | + Storage::assertExists($item->photo_path); |
| 448 | + Storage::assertMissing('teams/1/items/guitar.jpg'); |
| 449 | + }); |
| 450 | + |
| 451 | + test('can remove an existing photo', function () { |
| 452 | + Storage::fake(); |
| 453 | + $user = User::factory()->withTeam()->create(); |
| 454 | + $item = Item::factory()->for($user->currentTeam)->create(['name' => 'Guitar', 'photo_path' => 'teams/1/items/guitar.jpg']); |
| 455 | + Storage::put('teams/1/items/guitar.jpg', 'content'); |
| 456 | + |
| 457 | + Livewire::actingAs($user) |
| 458 | + ->test('pages::inventory.index') |
| 459 | + ->call('edit', $item->id) |
| 460 | + ->call('removePhoto') |
| 461 | + ->call('save') |
| 462 | + ->assertHasNoErrors(); |
| 463 | + |
| 464 | + expect($item->fresh()->photo_path)->toBeNull(); |
| 465 | + Storage::assertMissing('teams/1/items/guitar.jpg'); |
| 466 | + }); |
| 467 | + |
| 468 | + test('photo validation rejects non-image files', function () { |
| 469 | + $user = User::factory()->withTeam()->create(); |
| 470 | + $file = UploadedFile::fake()->create('document.pdf', 100, 'application/pdf'); |
| 471 | + |
| 472 | + Livewire::actingAs($user) |
| 473 | + ->test('pages::inventory.index') |
| 474 | + ->set('form.name', 'Guitar') |
| 475 | + ->set('form.type', ItemType::Item->value) |
| 476 | + ->set('form.photo', $file) |
| 477 | + ->call('save') |
| 478 | + ->assertHasErrors('form.photo'); |
| 479 | + }); |
| 480 | +}); |
| 481 | + |
395 | 482 | describe('can import items', function () { |
396 | 483 | test('can import items from amazon csv', function () { |
397 | 484 | $user = User::factory()->withTeam()->create(); |
|
0 commit comments