ReclamationControllerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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_upload_photo_before_preserves_unicode_and_quotes_filename(): void
  147. {
  148. Storage::fake('public');
  149. $reclamation = Reclamation::factory()->create();
  150. $filename = "Фото «до» 'левая' \"камера\".jpg";
  151. $photo = UploadedFile::fake()->create($filename, 100, 'image/jpeg');
  152. $response = $this->actingAs($this->managerUser)
  153. ->post(route('reclamations.upload-photo-before', $reclamation), [
  154. 'photo' => [$photo],
  155. ]);
  156. $response->assertRedirect();
  157. $saved = $reclamation->fresh()->photos_before->first();
  158. $this->assertNotNull($saved);
  159. $this->assertEquals($filename, $saved->original_name);
  160. $this->assertEquals('reclamations/' . $reclamation->id . '/photo_before/' . $filename, $saved->path);
  161. Storage::disk('public')->assertExists($saved->path);
  162. }
  163. public function test_can_delete_photo_before(): void
  164. {
  165. Storage::fake('public');
  166. $reclamation = Reclamation::factory()->create();
  167. $file = File::factory()->create();
  168. $reclamation->photos_before()->attach($file);
  169. $response = $this->actingAs($this->adminUser)
  170. ->delete(route('reclamations.delete-photo-before', [$reclamation, $file]));
  171. $response->assertRedirect();
  172. $this->assertCount(0, $reclamation->fresh()->photos_before);
  173. }
  174. // ==================== Photo After Management ====================
  175. public function test_can_upload_photo_after(): void
  176. {
  177. Storage::fake('public');
  178. $reclamation = Reclamation::factory()->create();
  179. // Use create() instead of image() to avoid GD extension requirement
  180. $photo = UploadedFile::fake()->create('photo_after.jpg', 100, 'image/jpeg');
  181. $response = $this->actingAs($this->managerUser)
  182. ->post(route('reclamations.upload-photo-after', $reclamation), [
  183. 'photo' => [$photo],
  184. ]);
  185. $response->assertRedirect();
  186. $this->assertCount(1, $reclamation->fresh()->photos_after);
  187. }
  188. public function test_can_delete_photo_after(): void
  189. {
  190. Storage::fake('public');
  191. $reclamation = Reclamation::factory()->create();
  192. $file = File::factory()->create();
  193. $reclamation->photos_after()->attach($file);
  194. // This route requires admin role
  195. $response = $this->actingAs($this->adminUser)
  196. ->delete(route('reclamations.delete-photo-after', [$reclamation, $file]));
  197. $response->assertRedirect();
  198. $this->assertCount(0, $reclamation->fresh()->photos_after);
  199. }
  200. // ==================== Document Management ====================
  201. public function test_can_upload_document(): void
  202. {
  203. Storage::fake('public');
  204. $reclamation = Reclamation::factory()->create();
  205. $document = UploadedFile::fake()->create('document.pdf', 100);
  206. $response = $this->actingAs($this->managerUser)
  207. ->post(route('reclamations.upload-document', $reclamation), [
  208. 'document' => [$document],
  209. ]);
  210. $response->assertRedirect();
  211. $this->assertCount(1, $reclamation->fresh()->documents);
  212. }
  213. public function test_upload_document_preserves_unicode_and_quotes_filename(): void
  214. {
  215. Storage::fake('public');
  216. $reclamation = Reclamation::factory()->create();
  217. $filename = "Рекламация «док» 'версия' \"A\".pdf";
  218. $document = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  219. $response = $this->actingAs($this->managerUser)
  220. ->post(route('reclamations.upload-document', $reclamation), [
  221. 'document' => [$document],
  222. ]);
  223. $response->assertRedirect();
  224. $saved = $reclamation->fresh()->documents->first();
  225. $this->assertNotNull($saved);
  226. $this->assertEquals($filename, $saved->original_name);
  227. $this->assertEquals('reclamations/' . $reclamation->id . '/document/' . $filename, $saved->path);
  228. Storage::disk('public')->assertExists($saved->path);
  229. }
  230. public function test_can_delete_document(): void
  231. {
  232. Storage::fake('public');
  233. $reclamation = Reclamation::factory()->create();
  234. $file = File::factory()->create();
  235. $reclamation->documents()->attach($file);
  236. // This route requires admin role
  237. $response = $this->actingAs($this->adminUser)
  238. ->delete(route('reclamations.delete-document', [$reclamation, $file]));
  239. $response->assertRedirect();
  240. $this->assertCount(0, $reclamation->fresh()->documents);
  241. }
  242. // ==================== Act Management ====================
  243. public function test_can_upload_act(): void
  244. {
  245. Storage::fake('public');
  246. $reclamation = Reclamation::factory()->create();
  247. $act = UploadedFile::fake()->create('act.pdf', 100);
  248. $response = $this->actingAs($this->managerUser)
  249. ->post(route('reclamations.upload-act', $reclamation), [
  250. 'acts' => [$act],
  251. ]);
  252. $response->assertRedirect();
  253. $this->assertCount(1, $reclamation->fresh()->acts);
  254. }
  255. public function test_upload_act_preserves_unicode_and_quotes_filename(): void
  256. {
  257. Storage::fake('public');
  258. $reclamation = Reclamation::factory()->create();
  259. $filename = "Акт «сервис» 'этап' \"01\".pdf";
  260. $act = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  261. $response = $this->actingAs($this->managerUser)
  262. ->post(route('reclamations.upload-act', $reclamation), [
  263. 'acts' => [$act],
  264. ]);
  265. $response->assertRedirect();
  266. $saved = $reclamation->fresh()->acts->first();
  267. $this->assertNotNull($saved);
  268. $this->assertEquals($filename, $saved->original_name);
  269. $this->assertEquals('reclamations/' . $reclamation->id . '/act/' . $filename, $saved->path);
  270. Storage::disk('public')->assertExists($saved->path);
  271. }
  272. public function test_can_delete_act(): void
  273. {
  274. Storage::fake('public');
  275. $reclamation = Reclamation::factory()->create();
  276. $file = File::factory()->create();
  277. $reclamation->acts()->attach($file);
  278. // This route requires admin role
  279. $response = $this->actingAs($this->adminUser)
  280. ->delete(route('reclamations.delete-act', [$reclamation, $file]));
  281. $response->assertRedirect();
  282. $this->assertCount(0, $reclamation->fresh()->acts);
  283. }
  284. // ==================== Spare Parts Reservation ====================
  285. public function test_update_spare_parts_creates_reservations(): void
  286. {
  287. $reclamation = Reclamation::factory()->create();
  288. $sparePart = SparePart::factory()->create();
  289. // Create available stock
  290. SparePartOrder::factory()
  291. ->inStock()
  292. ->withDocuments(false)
  293. ->withQuantity(10)
  294. ->forSparePart($sparePart)
  295. ->create();
  296. $response = $this->actingAs($this->managerUser)
  297. ->post(route('reclamations.update-spare-parts', $reclamation), [
  298. 'rows' => [
  299. [
  300. 'spare_part_id' => $sparePart->id,
  301. 'quantity' => 3,
  302. 'with_documents' => false,
  303. ],
  304. ],
  305. ]);
  306. $response->assertRedirect();
  307. // Check spare part is attached
  308. $this->assertTrue($reclamation->fresh()->spareParts->contains($sparePart->id));
  309. // Check reservation was created
  310. $this->assertDatabaseHas('reservations', [
  311. 'reclamation_id' => $reclamation->id,
  312. 'spare_part_id' => $sparePart->id,
  313. 'reserved_qty' => 3,
  314. 'status' => Reservation::STATUS_ACTIVE,
  315. ]);
  316. }
  317. public function test_update_spare_parts_cancels_removed_reservations(): void
  318. {
  319. $reclamation = Reclamation::factory()->create();
  320. $sparePart = SparePart::factory()->create();
  321. $order = SparePartOrder::factory()
  322. ->inStock()
  323. ->withDocuments(false)
  324. ->withQuantity(10)
  325. ->forSparePart($sparePart)
  326. ->create();
  327. // Create existing reservation
  328. Reservation::factory()
  329. ->active()
  330. ->withQuantity(5)
  331. ->withDocuments(false)
  332. ->fromOrder($order)
  333. ->forReclamation($reclamation)
  334. ->create();
  335. // Attach spare part
  336. $reclamation->spareParts()->attach($sparePart->id, [
  337. 'quantity' => 5,
  338. 'with_documents' => false,
  339. 'reserved_qty' => 5,
  340. ]);
  341. // Send empty rows to remove spare part
  342. $response = $this->actingAs($this->managerUser)
  343. ->post(route('reclamations.update-spare-parts', $reclamation), [
  344. 'rows' => [],
  345. ]);
  346. $response->assertRedirect();
  347. // Check spare part is detached
  348. $this->assertFalse($reclamation->fresh()->spareParts->contains($sparePart->id));
  349. // Check reservation was cancelled
  350. $this->assertDatabaseHas('reservations', [
  351. 'reclamation_id' => $reclamation->id,
  352. 'spare_part_id' => $sparePart->id,
  353. 'status' => Reservation::STATUS_CANCELLED,
  354. ]);
  355. }
  356. // ==================== Details Management ====================
  357. public function test_update_details_creates_reclamation_detail(): void
  358. {
  359. $reclamation = Reclamation::factory()->create();
  360. $response = $this->actingAs($this->managerUser)
  361. ->post(route('reclamations.update-details', $reclamation), [
  362. 'name' => ['Деталь 1', 'Деталь 2'],
  363. 'quantity' => ['2', '3'], // Controller casts to int, send as strings like form data
  364. ]);
  365. $response->assertRedirect();
  366. $response->assertSessionHasNoErrors();
  367. $this->assertDatabaseHas('reclamation_details', [
  368. 'reclamation_id' => $reclamation->id,
  369. 'name' => 'Деталь 1',
  370. 'quantity' => 2,
  371. ]);
  372. $this->assertDatabaseHas('reclamation_details', [
  373. 'reclamation_id' => $reclamation->id,
  374. 'name' => 'Деталь 2',
  375. 'quantity' => 3,
  376. ]);
  377. }
  378. public function test_update_details_removes_detail_with_zero_quantity(): void
  379. {
  380. $reclamation = Reclamation::factory()->create();
  381. ReclamationDetail::create([
  382. 'reclamation_id' => $reclamation->id,
  383. 'name' => 'Деталь для удаления',
  384. 'quantity' => 5,
  385. ]);
  386. $response = $this->actingAs($this->managerUser)
  387. ->post(route('reclamations.update-details', $reclamation), [
  388. 'name' => ['Деталь для удаления'],
  389. 'quantity' => ['0'], // Send as string like form data
  390. ]);
  391. $response->assertRedirect();
  392. $this->assertDatabaseMissing('reclamation_details', [
  393. 'reclamation_id' => $reclamation->id,
  394. 'name' => 'Деталь для удаления',
  395. ]);
  396. }
  397. // ==================== Generation ====================
  398. public function test_can_generate_reclamation_pack(): void
  399. {
  400. $reclamation = Reclamation::factory()->create();
  401. $response = $this->actingAs($this->managerUser)
  402. ->get(route('order.generate-reclamation-pack', $reclamation));
  403. $response->assertRedirect();
  404. $response->assertSessionHas('success');
  405. }
  406. public function test_can_generate_photos_before_pack(): void
  407. {
  408. $reclamation = Reclamation::factory()->create();
  409. $response = $this->actingAs($this->managerUser)
  410. ->get(route('reclamation.generate-photos-before-pack', $reclamation));
  411. $response->assertRedirect();
  412. $response->assertSessionHas('success');
  413. }
  414. public function test_can_generate_photos_after_pack(): void
  415. {
  416. $reclamation = Reclamation::factory()->create();
  417. $response = $this->actingAs($this->managerUser)
  418. ->get(route('reclamation.generate-photos-after-pack', $reclamation));
  419. $response->assertRedirect();
  420. $response->assertSessionHas('success');
  421. }
  422. }