ReclamationControllerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\File;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\ProductSKU;
  7. use App\Models\Reclamation;
  8. use App\Models\ReclamationDetail;
  9. use App\Models\Reservation;
  10. use App\Models\Role;
  11. use App\Models\SparePart;
  12. use App\Models\SparePartOrder;
  13. use App\Models\User;
  14. use Illuminate\Foundation\Testing\RefreshDatabase;
  15. use Illuminate\Http\UploadedFile;
  16. use Illuminate\Support\Facades\Storage;
  17. use Tests\TestCase;
  18. class ReclamationControllerTest extends TestCase
  19. {
  20. use RefreshDatabase;
  21. protected $seed = true;
  22. private User $adminUser;
  23. private User $managerUser;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  28. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  29. }
  30. // ==================== Authentication ====================
  31. public function test_guest_cannot_access_reclamations_index(): void
  32. {
  33. $response = $this->get(route('reclamations.index'));
  34. $response->assertRedirect(route('login'));
  35. }
  36. public function test_authenticated_user_can_access_reclamations_index(): void
  37. {
  38. $response = $this->actingAs($this->managerUser)
  39. ->get(route('reclamations.index'));
  40. $response->assertStatus(200);
  41. $response->assertViewIs('reclamations.index');
  42. }
  43. // ==================== Index ====================
  44. public function test_reclamations_index_displays_reclamations(): void
  45. {
  46. $reclamation = Reclamation::factory()->create();
  47. $response = $this->actingAs($this->managerUser)
  48. ->get(route('reclamations.index'));
  49. $response->assertStatus(200);
  50. }
  51. // ==================== Create ====================
  52. public function test_can_create_reclamation_for_order(): void
  53. {
  54. $order = Order::factory()->create();
  55. $product = Product::factory()->create();
  56. $productSku = ProductSKU::factory()->create([
  57. 'order_id' => $order->id,
  58. 'product_id' => $product->id,
  59. ]);
  60. $response = $this->actingAs($this->managerUser)
  61. ->post(route('reclamations.create', $order), [
  62. 'skus' => [$productSku->id],
  63. ]);
  64. $response->assertRedirect();
  65. $this->assertDatabaseHas('reclamations', [
  66. 'order_id' => $order->id,
  67. 'user_id' => $this->managerUser->id,
  68. 'status_id' => Reclamation::STATUS_NEW,
  69. ]);
  70. }
  71. public function test_creating_reclamation_attaches_skus(): void
  72. {
  73. $order = Order::factory()->create();
  74. $product = Product::factory()->create();
  75. $productSku1 = ProductSKU::factory()->create([
  76. 'order_id' => $order->id,
  77. 'product_id' => $product->id,
  78. ]);
  79. $productSku2 = ProductSKU::factory()->create([
  80. 'order_id' => $order->id,
  81. 'product_id' => $product->id,
  82. ]);
  83. $this->actingAs($this->managerUser)
  84. ->post(route('reclamations.create', $order), [
  85. 'skus' => [$productSku1->id, $productSku2->id],
  86. ]);
  87. $reclamation = Reclamation::where('order_id', $order->id)->first();
  88. $this->assertCount(2, $reclamation->skus);
  89. }
  90. // ==================== Show ====================
  91. public function test_can_view_reclamation_details(): void
  92. {
  93. $reclamation = Reclamation::factory()->create();
  94. $response = $this->actingAs($this->managerUser)
  95. ->get(route('reclamations.show', $reclamation));
  96. $response->assertStatus(200);
  97. $response->assertViewIs('reclamations.edit');
  98. }
  99. // ==================== Update ====================
  100. public function test_can_update_reclamation(): void
  101. {
  102. $reclamation = Reclamation::factory()->create([
  103. 'reason' => 'Старая причина',
  104. ]);
  105. // Route uses POST, not PUT. All required fields must be sent.
  106. $response = $this->actingAs($this->managerUser)
  107. ->post(route('reclamations.update', $reclamation), [
  108. 'user_id' => $reclamation->user_id,
  109. 'status_id' => $reclamation->status_id,
  110. 'create_date' => $reclamation->create_date,
  111. 'finish_date' => $reclamation->finish_date,
  112. 'reason' => 'Новая причина',
  113. 'guarantee' => 'Гарантия',
  114. 'whats_done' => 'Что сделано',
  115. ]);
  116. $response->assertRedirect(route('reclamations.show', $reclamation));
  117. $this->assertDatabaseHas('reclamations', [
  118. 'id' => $reclamation->id,
  119. 'reason' => 'Новая причина',
  120. ]);
  121. }
  122. // ==================== Delete ====================
  123. public function test_can_delete_reclamation(): void
  124. {
  125. $reclamation = Reclamation::factory()->create();
  126. $reclamationId = $reclamation->id;
  127. $response = $this->actingAs($this->adminUser)
  128. ->delete(route('reclamations.delete', $reclamation));
  129. $response->assertRedirect(route('reclamations.index'));
  130. $this->assertDatabaseMissing('reclamations', ['id' => $reclamationId]);
  131. }
  132. // ==================== Photo Before Management ====================
  133. public function test_can_upload_photo_before(): void
  134. {
  135. Storage::fake('public');
  136. $reclamation = Reclamation::factory()->create();
  137. // Use create() instead of image() to avoid GD extension requirement
  138. $photo = UploadedFile::fake()->create('photo_before.jpg', 100, 'image/jpeg');
  139. $response = $this->actingAs($this->managerUser)
  140. ->post(route('reclamations.upload-photo-before', $reclamation), [
  141. 'photo' => [$photo],
  142. ]);
  143. $response->assertRedirect();
  144. $this->assertCount(1, $reclamation->fresh()->photos_before);
  145. }
  146. public function test_can_delete_photo_before(): void
  147. {
  148. Storage::fake('public');
  149. $reclamation = Reclamation::factory()->create();
  150. $file = File::factory()->create();
  151. $reclamation->photos_before()->attach($file);
  152. $response = $this->actingAs($this->adminUser)
  153. ->delete(route('reclamations.delete-photo-before', [$reclamation, $file]));
  154. $response->assertRedirect();
  155. $this->assertCount(0, $reclamation->fresh()->photos_before);
  156. }
  157. // ==================== Photo After Management ====================
  158. public function test_can_upload_photo_after(): void
  159. {
  160. Storage::fake('public');
  161. $reclamation = Reclamation::factory()->create();
  162. // Use create() instead of image() to avoid GD extension requirement
  163. $photo = UploadedFile::fake()->create('photo_after.jpg', 100, 'image/jpeg');
  164. $response = $this->actingAs($this->managerUser)
  165. ->post(route('reclamations.upload-photo-after', $reclamation), [
  166. 'photo' => [$photo],
  167. ]);
  168. $response->assertRedirect();
  169. $this->assertCount(1, $reclamation->fresh()->photos_after);
  170. }
  171. public function test_can_delete_photo_after(): void
  172. {
  173. Storage::fake('public');
  174. $reclamation = Reclamation::factory()->create();
  175. $file = File::factory()->create();
  176. $reclamation->photos_after()->attach($file);
  177. // This route requires admin role
  178. $response = $this->actingAs($this->adminUser)
  179. ->delete(route('reclamations.delete-photo-after', [$reclamation, $file]));
  180. $response->assertRedirect();
  181. $this->assertCount(0, $reclamation->fresh()->photos_after);
  182. }
  183. // ==================== Document Management ====================
  184. public function test_can_upload_document(): void
  185. {
  186. Storage::fake('public');
  187. $reclamation = Reclamation::factory()->create();
  188. $document = UploadedFile::fake()->create('document.pdf', 100);
  189. $response = $this->actingAs($this->managerUser)
  190. ->post(route('reclamations.upload-document', $reclamation), [
  191. 'document' => [$document],
  192. ]);
  193. $response->assertRedirect();
  194. $this->assertCount(1, $reclamation->fresh()->documents);
  195. }
  196. public function test_can_delete_document(): void
  197. {
  198. Storage::fake('public');
  199. $reclamation = Reclamation::factory()->create();
  200. $file = File::factory()->create();
  201. $reclamation->documents()->attach($file);
  202. // This route requires admin role
  203. $response = $this->actingAs($this->adminUser)
  204. ->delete(route('reclamations.delete-document', [$reclamation, $file]));
  205. $response->assertRedirect();
  206. $this->assertCount(0, $reclamation->fresh()->documents);
  207. }
  208. // ==================== Act Management ====================
  209. public function test_can_upload_act(): void
  210. {
  211. Storage::fake('public');
  212. $reclamation = Reclamation::factory()->create();
  213. $act = UploadedFile::fake()->create('act.pdf', 100);
  214. $response = $this->actingAs($this->managerUser)
  215. ->post(route('reclamations.upload-act', $reclamation), [
  216. 'acts' => [$act],
  217. ]);
  218. $response->assertRedirect();
  219. $this->assertCount(1, $reclamation->fresh()->acts);
  220. }
  221. public function test_can_delete_act(): void
  222. {
  223. Storage::fake('public');
  224. $reclamation = Reclamation::factory()->create();
  225. $file = File::factory()->create();
  226. $reclamation->acts()->attach($file);
  227. // This route requires admin role
  228. $response = $this->actingAs($this->adminUser)
  229. ->delete(route('reclamations.delete-act', [$reclamation, $file]));
  230. $response->assertRedirect();
  231. $this->assertCount(0, $reclamation->fresh()->acts);
  232. }
  233. // ==================== Spare Parts Reservation ====================
  234. public function test_update_spare_parts_creates_reservations(): void
  235. {
  236. $reclamation = Reclamation::factory()->create();
  237. $sparePart = SparePart::factory()->create();
  238. // Create available stock
  239. SparePartOrder::factory()
  240. ->inStock()
  241. ->withDocuments(false)
  242. ->withQuantity(10)
  243. ->forSparePart($sparePart)
  244. ->create();
  245. $response = $this->actingAs($this->managerUser)
  246. ->post(route('reclamations.update-spare-parts', $reclamation), [
  247. 'rows' => [
  248. [
  249. 'spare_part_id' => $sparePart->id,
  250. 'quantity' => 3,
  251. 'with_documents' => false,
  252. ],
  253. ],
  254. ]);
  255. $response->assertRedirect();
  256. // Check spare part is attached
  257. $this->assertTrue($reclamation->fresh()->spareParts->contains($sparePart->id));
  258. // Check reservation was created
  259. $this->assertDatabaseHas('reservations', [
  260. 'reclamation_id' => $reclamation->id,
  261. 'spare_part_id' => $sparePart->id,
  262. 'reserved_qty' => 3,
  263. 'status' => Reservation::STATUS_ACTIVE,
  264. ]);
  265. }
  266. public function test_update_spare_parts_cancels_removed_reservations(): void
  267. {
  268. $reclamation = Reclamation::factory()->create();
  269. $sparePart = SparePart::factory()->create();
  270. $order = SparePartOrder::factory()
  271. ->inStock()
  272. ->withDocuments(false)
  273. ->withQuantity(10)
  274. ->forSparePart($sparePart)
  275. ->create();
  276. // Create existing reservation
  277. Reservation::factory()
  278. ->active()
  279. ->withQuantity(5)
  280. ->withDocuments(false)
  281. ->fromOrder($order)
  282. ->forReclamation($reclamation)
  283. ->create();
  284. // Attach spare part
  285. $reclamation->spareParts()->attach($sparePart->id, [
  286. 'quantity' => 5,
  287. 'with_documents' => false,
  288. 'reserved_qty' => 5,
  289. ]);
  290. // Send empty rows to remove spare part
  291. $response = $this->actingAs($this->managerUser)
  292. ->post(route('reclamations.update-spare-parts', $reclamation), [
  293. 'rows' => [],
  294. ]);
  295. $response->assertRedirect();
  296. // Check spare part is detached
  297. $this->assertFalse($reclamation->fresh()->spareParts->contains($sparePart->id));
  298. // Check reservation was cancelled
  299. $this->assertDatabaseHas('reservations', [
  300. 'reclamation_id' => $reclamation->id,
  301. 'spare_part_id' => $sparePart->id,
  302. 'status' => Reservation::STATUS_CANCELLED,
  303. ]);
  304. }
  305. // ==================== Details Management ====================
  306. public function test_update_details_creates_reclamation_detail(): void
  307. {
  308. $reclamation = Reclamation::factory()->create();
  309. $response = $this->actingAs($this->managerUser)
  310. ->post(route('reclamations.update-details', $reclamation), [
  311. 'name' => ['Деталь 1', 'Деталь 2'],
  312. 'quantity' => ['2', '3'], // Controller casts to int, send as strings like form data
  313. ]);
  314. $response->assertRedirect();
  315. $response->assertSessionHasNoErrors();
  316. $this->assertDatabaseHas('reclamation_details', [
  317. 'reclamation_id' => $reclamation->id,
  318. 'name' => 'Деталь 1',
  319. 'quantity' => 2,
  320. ]);
  321. $this->assertDatabaseHas('reclamation_details', [
  322. 'reclamation_id' => $reclamation->id,
  323. 'name' => 'Деталь 2',
  324. 'quantity' => 3,
  325. ]);
  326. }
  327. public function test_update_details_removes_detail_with_zero_quantity(): void
  328. {
  329. $reclamation = Reclamation::factory()->create();
  330. ReclamationDetail::create([
  331. 'reclamation_id' => $reclamation->id,
  332. 'name' => 'Деталь для удаления',
  333. 'quantity' => 5,
  334. ]);
  335. $response = $this->actingAs($this->managerUser)
  336. ->post(route('reclamations.update-details', $reclamation), [
  337. 'name' => ['Деталь для удаления'],
  338. 'quantity' => ['0'], // Send as string like form data
  339. ]);
  340. $response->assertRedirect();
  341. $this->assertDatabaseMissing('reclamation_details', [
  342. 'reclamation_id' => $reclamation->id,
  343. 'name' => 'Деталь для удаления',
  344. ]);
  345. }
  346. // ==================== Generation ====================
  347. public function test_can_generate_reclamation_pack(): void
  348. {
  349. $reclamation = Reclamation::factory()->create();
  350. $response = $this->actingAs($this->managerUser)
  351. ->get(route('order.generate-reclamation-pack', $reclamation));
  352. $response->assertRedirect();
  353. $response->assertSessionHas('success');
  354. }
  355. public function test_can_generate_photos_before_pack(): void
  356. {
  357. $reclamation = Reclamation::factory()->create();
  358. $response = $this->actingAs($this->managerUser)
  359. ->get(route('reclamation.generate-photos-before-pack', $reclamation));
  360. $response->assertRedirect();
  361. $response->assertSessionHas('success');
  362. }
  363. public function test_can_generate_photos_after_pack(): void
  364. {
  365. $reclamation = Reclamation::factory()->create();
  366. $response = $this->actingAs($this->managerUser)
  367. ->get(route('reclamation.generate-photos-after-pack', $reclamation));
  368. $response->assertRedirect();
  369. $response->assertSessionHas('success');
  370. }
  371. }