| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <?php
- namespace Tests\Feature;
- use App\Models\File;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\ProductSKU;
- use App\Models\Reclamation;
- use App\Models\ReclamationDetail;
- use App\Models\Reservation;
- use App\Models\Role;
- use App\Models\SparePart;
- use App\Models\SparePartOrder;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class ReclamationControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private User $adminUser;
- private User $managerUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
- $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
- }
- // ==================== Authentication ====================
- public function test_guest_cannot_access_reclamations_index(): void
- {
- $response = $this->get(route('reclamations.index'));
- $response->assertRedirect(route('login'));
- }
- public function test_authenticated_user_can_access_reclamations_index(): void
- {
- $response = $this->actingAs($this->managerUser)
- ->get(route('reclamations.index'));
- $response->assertStatus(200);
- $response->assertViewIs('reclamations.index');
- }
- // ==================== Index ====================
- public function test_reclamations_index_displays_reclamations(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->get(route('reclamations.index'));
- $response->assertStatus(200);
- }
- // ==================== Create ====================
- public function test_can_create_reclamation_for_order(): void
- {
- $order = Order::factory()->create();
- $product = Product::factory()->create();
- $productSku = ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- ]);
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.create', $order), [
- 'skus' => [$productSku->id],
- ]);
- $response->assertRedirect();
- $this->assertDatabaseHas('reclamations', [
- 'order_id' => $order->id,
- 'user_id' => $this->managerUser->id,
- 'status_id' => Reclamation::STATUS_NEW,
- ]);
- }
- public function test_creating_reclamation_attaches_skus(): void
- {
- $order = Order::factory()->create();
- $product = Product::factory()->create();
- $productSku1 = ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- ]);
- $productSku2 = ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- ]);
- $this->actingAs($this->managerUser)
- ->post(route('reclamations.create', $order), [
- 'skus' => [$productSku1->id, $productSku2->id],
- ]);
- $reclamation = Reclamation::where('order_id', $order->id)->first();
- $this->assertCount(2, $reclamation->skus);
- }
- // ==================== Show ====================
- public function test_can_view_reclamation_details(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->get(route('reclamations.show', $reclamation));
- $response->assertStatus(200);
- $response->assertViewIs('reclamations.edit');
- }
- // ==================== Update ====================
- public function test_can_update_reclamation(): void
- {
- $reclamation = Reclamation::factory()->create([
- 'reason' => 'Старая причина',
- ]);
- // Route uses POST, not PUT. All required fields must be sent.
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.update', $reclamation), [
- 'user_id' => $reclamation->user_id,
- 'status_id' => $reclamation->status_id,
- 'create_date' => $reclamation->create_date,
- 'finish_date' => $reclamation->finish_date,
- 'reason' => 'Новая причина',
- 'guarantee' => 'Гарантия',
- 'whats_done' => 'Что сделано',
- ]);
- $response->assertRedirect(route('reclamations.show', $reclamation));
- $this->assertDatabaseHas('reclamations', [
- 'id' => $reclamation->id,
- 'reason' => 'Новая причина',
- ]);
- }
- // ==================== Delete ====================
- public function test_can_delete_reclamation(): void
- {
- $reclamation = Reclamation::factory()->create();
- $reclamationId = $reclamation->id;
- $response = $this->actingAs($this->adminUser)
- ->delete(route('reclamations.delete', $reclamation));
- $response->assertRedirect(route('reclamations.index'));
- $this->assertDatabaseMissing('reclamations', ['id' => $reclamationId]);
- }
- // ==================== Photo Before Management ====================
- public function test_can_upload_photo_before(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- // Use create() instead of image() to avoid GD extension requirement
- $photo = UploadedFile::fake()->create('photo_before.jpg', 100, 'image/jpeg');
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.upload-photo-before', $reclamation), [
- 'photo' => [$photo],
- ]);
- $response->assertRedirect();
- $this->assertCount(1, $reclamation->fresh()->photos_before);
- }
- public function test_can_delete_photo_before(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $file = File::factory()->create();
- $reclamation->photos_before()->attach($file);
- $response = $this->actingAs($this->adminUser)
- ->delete(route('reclamations.delete-photo-before', [$reclamation, $file]));
- $response->assertRedirect();
- $this->assertCount(0, $reclamation->fresh()->photos_before);
- }
- // ==================== Photo After Management ====================
- public function test_can_upload_photo_after(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- // Use create() instead of image() to avoid GD extension requirement
- $photo = UploadedFile::fake()->create('photo_after.jpg', 100, 'image/jpeg');
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.upload-photo-after', $reclamation), [
- 'photo' => [$photo],
- ]);
- $response->assertRedirect();
- $this->assertCount(1, $reclamation->fresh()->photos_after);
- }
- public function test_can_delete_photo_after(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $file = File::factory()->create();
- $reclamation->photos_after()->attach($file);
- // This route requires admin role
- $response = $this->actingAs($this->adminUser)
- ->delete(route('reclamations.delete-photo-after', [$reclamation, $file]));
- $response->assertRedirect();
- $this->assertCount(0, $reclamation->fresh()->photos_after);
- }
- // ==================== Document Management ====================
- public function test_can_upload_document(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $document = UploadedFile::fake()->create('document.pdf', 100);
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.upload-document', $reclamation), [
- 'document' => [$document],
- ]);
- $response->assertRedirect();
- $this->assertCount(1, $reclamation->fresh()->documents);
- }
- public function test_can_delete_document(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $file = File::factory()->create();
- $reclamation->documents()->attach($file);
- // This route requires admin role
- $response = $this->actingAs($this->adminUser)
- ->delete(route('reclamations.delete-document', [$reclamation, $file]));
- $response->assertRedirect();
- $this->assertCount(0, $reclamation->fresh()->documents);
- }
- // ==================== Act Management ====================
- public function test_can_upload_act(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $act = UploadedFile::fake()->create('act.pdf', 100);
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.upload-act', $reclamation), [
- 'acts' => [$act],
- ]);
- $response->assertRedirect();
- $this->assertCount(1, $reclamation->fresh()->acts);
- }
- public function test_can_delete_act(): void
- {
- Storage::fake('public');
- $reclamation = Reclamation::factory()->create();
- $file = File::factory()->create();
- $reclamation->acts()->attach($file);
- // This route requires admin role
- $response = $this->actingAs($this->adminUser)
- ->delete(route('reclamations.delete-act', [$reclamation, $file]));
- $response->assertRedirect();
- $this->assertCount(0, $reclamation->fresh()->acts);
- }
- // ==================== Spare Parts Reservation ====================
- public function test_update_spare_parts_creates_reservations(): void
- {
- $reclamation = Reclamation::factory()->create();
- $sparePart = SparePart::factory()->create();
- // Create available stock
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.update-spare-parts', $reclamation), [
- 'rows' => [
- [
- 'spare_part_id' => $sparePart->id,
- 'quantity' => 3,
- 'with_documents' => false,
- ],
- ],
- ]);
- $response->assertRedirect();
- // Check spare part is attached
- $this->assertTrue($reclamation->fresh()->spareParts->contains($sparePart->id));
- // Check reservation was created
- $this->assertDatabaseHas('reservations', [
- 'reclamation_id' => $reclamation->id,
- 'spare_part_id' => $sparePart->id,
- 'reserved_qty' => 3,
- 'status' => Reservation::STATUS_ACTIVE,
- ]);
- }
- public function test_update_spare_parts_cancels_removed_reservations(): void
- {
- $reclamation = Reclamation::factory()->create();
- $sparePart = SparePart::factory()->create();
- $order = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- // Create existing reservation
- Reservation::factory()
- ->active()
- ->withQuantity(5)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forReclamation($reclamation)
- ->create();
- // Attach spare part
- $reclamation->spareParts()->attach($sparePart->id, [
- 'quantity' => 5,
- 'with_documents' => false,
- 'reserved_qty' => 5,
- ]);
- // Send empty rows to remove spare part
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.update-spare-parts', $reclamation), [
- 'rows' => [],
- ]);
- $response->assertRedirect();
- // Check spare part is detached
- $this->assertFalse($reclamation->fresh()->spareParts->contains($sparePart->id));
- // Check reservation was cancelled
- $this->assertDatabaseHas('reservations', [
- 'reclamation_id' => $reclamation->id,
- 'spare_part_id' => $sparePart->id,
- 'status' => Reservation::STATUS_CANCELLED,
- ]);
- }
- // ==================== Details Management ====================
- public function test_update_details_creates_reclamation_detail(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.update-details', $reclamation), [
- 'name' => ['Деталь 1', 'Деталь 2'],
- 'quantity' => ['2', '3'], // Controller casts to int, send as strings like form data
- ]);
- $response->assertRedirect();
- $response->assertSessionHasNoErrors();
- $this->assertDatabaseHas('reclamation_details', [
- 'reclamation_id' => $reclamation->id,
- 'name' => 'Деталь 1',
- 'quantity' => 2,
- ]);
- $this->assertDatabaseHas('reclamation_details', [
- 'reclamation_id' => $reclamation->id,
- 'name' => 'Деталь 2',
- 'quantity' => 3,
- ]);
- }
- public function test_update_details_removes_detail_with_zero_quantity(): void
- {
- $reclamation = Reclamation::factory()->create();
- ReclamationDetail::create([
- 'reclamation_id' => $reclamation->id,
- 'name' => 'Деталь для удаления',
- 'quantity' => 5,
- ]);
- $response = $this->actingAs($this->managerUser)
- ->post(route('reclamations.update-details', $reclamation), [
- 'name' => ['Деталь для удаления'],
- 'quantity' => ['0'], // Send as string like form data
- ]);
- $response->assertRedirect();
- $this->assertDatabaseMissing('reclamation_details', [
- 'reclamation_id' => $reclamation->id,
- 'name' => 'Деталь для удаления',
- ]);
- }
- // ==================== Generation ====================
- public function test_can_generate_reclamation_pack(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->get(route('order.generate-reclamation-pack', $reclamation));
- $response->assertRedirect();
- $response->assertSessionHas('success');
- }
- public function test_can_generate_photos_before_pack(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->get(route('reclamation.generate-photos-before-pack', $reclamation));
- $response->assertRedirect();
- $response->assertSessionHas('success');
- }
- public function test_can_generate_photos_after_pack(): void
- {
- $reclamation = Reclamation::factory()->create();
- $response = $this->actingAs($this->managerUser)
- ->get(route('reclamation.generate-photos-after-pack', $reclamation));
- $response->assertRedirect();
- $response->assertSessionHas('success');
- }
- }
|