ReclamationControllerTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Jobs\ExportReclamationsJob;
  4. use App\Jobs\GenerateReclamationPaymentPack;
  5. use App\Models\File;
  6. use App\Models\Order;
  7. use App\Models\Product;
  8. use App\Models\ProductSKU;
  9. use App\Models\Reclamation;
  10. use App\Models\ReclamationDetail;
  11. use App\Models\ReclamationType;
  12. use App\Models\Reservation;
  13. use App\Models\Role;
  14. use App\Models\SparePart;
  15. use App\Models\SparePartOrder;
  16. use App\Models\User;
  17. use Illuminate\Foundation\Testing\RefreshDatabase;
  18. use Illuminate\Http\UploadedFile;
  19. use Illuminate\Support\Facades\Bus;
  20. use Illuminate\Support\Facades\Storage;
  21. use ReflectionProperty;
  22. use Tests\TestCase;
  23. class ReclamationControllerTest extends TestCase
  24. {
  25. use RefreshDatabase;
  26. protected $seed = true;
  27. private User $adminUser;
  28. private User $managerUser;
  29. private User $brigadierUser;
  30. protected function setUp(): void
  31. {
  32. parent::setUp();
  33. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  34. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  35. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  36. }
  37. // ==================== Authentication ====================
  38. public function test_guest_cannot_access_reclamations_index(): void
  39. {
  40. $response = $this->get(route('reclamations.index'));
  41. $response->assertRedirect(route('login'));
  42. }
  43. public function test_authenticated_user_can_access_reclamations_index(): void
  44. {
  45. $response = $this->actingAs($this->managerUser)
  46. ->get(route('reclamations.index'));
  47. $response->assertStatus(200);
  48. $response->assertViewIs('reclamations.index');
  49. }
  50. // ==================== Index ====================
  51. public function test_reclamations_index_displays_reclamations(): void
  52. {
  53. $reclamation = Reclamation::factory()->create();
  54. $response = $this->actingAs($this->managerUser)
  55. ->get(route('reclamations.index'));
  56. $response->assertStatus(200);
  57. }
  58. public function test_all_tab_displays_reclamations_of_every_type(): void
  59. {
  60. $dkr = Reclamation::factory()->dkr()->create(['reason' => 'Рекламация ДКР для общего списка']);
  61. $other = Reclamation::factory()->other()->create(['reason' => 'Прочая рекламация для общего списка']);
  62. $response = $this->actingAs($this->managerUser)
  63. ->get(route('reclamations.index'));
  64. $response->assertOk()
  65. ->assertViewHas('tab', 'all')
  66. ->assertSee($dkr->reason)
  67. ->assertSee($other->reason);
  68. }
  69. public function test_dkr_tab_displays_only_dkr_reclamations(): void
  70. {
  71. $dkr = Reclamation::factory()->dkr()->create(['reason' => 'Рекламация только вкладки ДКР']);
  72. $other = Reclamation::factory()->other()->create(['reason' => 'Рекламация типа Прочее']);
  73. $response = $this->actingAs($this->managerUser)
  74. ->get(route('reclamations.index', ['tab' => 'dkr']));
  75. $response->assertOk()
  76. ->assertViewHas('tab', ReclamationType::CODE_DKR)
  77. ->assertSee($dkr->reason)
  78. ->assertDontSee($other->reason);
  79. }
  80. public function test_export_with_dkr_tab_filter_contains_only_dkr_reclamations(): void
  81. {
  82. Bus::fake();
  83. $dkr = Reclamation::factory()->dkr()->create();
  84. Reclamation::factory()->other()->create();
  85. $response = $this->actingAs($this->managerUser)
  86. ->post(route('reclamations.export'), [
  87. 'withFilter' => '1',
  88. 'tab' => ReclamationType::CODE_DKR,
  89. ]);
  90. $response->assertRedirect();
  91. Bus::assertDispatched(ExportReclamationsJob::class, function (ExportReclamationsJob $job) use ($dkr) {
  92. $property = new ReflectionProperty($job, 'reclamationIds');
  93. return $property->getValue($job) === [$dkr->id];
  94. });
  95. }
  96. public function test_brigadier_sees_only_assigned_reclamations_with_allowed_statuses(): void
  97. {
  98. $visibleReclamation = Reclamation::factory()->create([
  99. 'brigadier_id' => $this->brigadierUser->id,
  100. 'status_id' => Reclamation::STATUS_IN_WORK,
  101. 'reason' => 'Видимая рекламация',
  102. ]);
  103. $hiddenByStatus = Reclamation::factory()->create([
  104. 'brigadier_id' => $this->brigadierUser->id,
  105. 'status_id' => Reclamation::STATUS_DONE,
  106. 'reason' => 'Скрытая по статусу',
  107. ]);
  108. $hiddenByBrigadier = Reclamation::factory()->create([
  109. 'brigadier_id' => User::factory()->create(['role' => Role::BRIGADIER])->id,
  110. 'status_id' => Reclamation::STATUS_IN_WORK,
  111. 'reason' => 'Скрытая по бригадиру',
  112. ]);
  113. $response = $this->actingAs($this->brigadierUser)
  114. ->get(route('reclamations.index'));
  115. $response->assertStatus(200);
  116. $response->assertSee($visibleReclamation->reason);
  117. $response->assertDontSee($hiddenByStatus->reason);
  118. $response->assertDontSee($hiddenByBrigadier->reason);
  119. }
  120. // ==================== Create ====================
  121. public function test_can_create_reclamation_for_order(): void
  122. {
  123. $order = Order::factory()->create();
  124. $product = Product::factory()->create();
  125. $productSku = ProductSKU::factory()->create([
  126. 'order_id' => $order->id,
  127. 'product_id' => $product->id,
  128. ]);
  129. $response = $this->actingAs($this->managerUser)
  130. ->post(route('reclamations.create', $order), [
  131. 'skus' => [$productSku->id],
  132. ]);
  133. $response->assertRedirect();
  134. $this->assertDatabaseHas('reclamations', [
  135. 'order_id' => $order->id,
  136. 'reclamation_type_id' => ReclamationType::idForCode(ReclamationType::CODE_DKR),
  137. 'user_id' => $this->managerUser->id,
  138. 'status_id' => Reclamation::STATUS_NEW,
  139. ]);
  140. }
  141. public function test_creating_reclamation_from_dkr_order_ignores_spoofed_type(): void
  142. {
  143. $order = Order::factory()->create();
  144. $productSku = ProductSKU::factory()->create([
  145. 'order_id' => $order->id,
  146. 'product_id' => Product::factory(),
  147. ]);
  148. $this->actingAs($this->managerUser)
  149. ->post(route('reclamations.create', $order), [
  150. 'skus' => [$productSku->id],
  151. 'reclamation_type_id' => ReclamationType::idForCode(ReclamationType::CODE_OTHER),
  152. ])
  153. ->assertRedirect();
  154. $this->assertDatabaseHas('reclamations', [
  155. 'order_id' => $order->id,
  156. 'reclamation_type_id' => ReclamationType::idForCode(ReclamationType::CODE_DKR),
  157. ]);
  158. }
  159. public function test_creating_reclamation_from_order_preserves_nav_token(): void
  160. {
  161. $order = Order::factory()->create();
  162. $product = Product::factory()->create();
  163. $productSku = ProductSKU::factory()->create([
  164. 'order_id' => $order->id,
  165. 'product_id' => $product->id,
  166. ]);
  167. $response = $this->actingAs($this->managerUser)
  168. ->withSession([
  169. 'navigation' => [
  170. 'order-nav-token' => [
  171. 'updated_at' => time(),
  172. 'stack' => [
  173. route('order.index'),
  174. route('order.show', $order),
  175. ],
  176. ],
  177. ],
  178. ])
  179. ->post(route('reclamations.create', ['order' => $order, 'nav' => 'order-nav-token']), [
  180. 'skus' => [$productSku->id],
  181. 'nav' => 'order-nav-token',
  182. ]);
  183. $location = $response->headers->get('Location');
  184. $this->assertNotNull($location);
  185. $this->assertStringContainsString('nav=order-nav-token', $location);
  186. }
  187. public function test_creating_reclamation_attaches_skus(): void
  188. {
  189. $order = Order::factory()->create();
  190. $product = Product::factory()->create();
  191. $productSku1 = ProductSKU::factory()->create([
  192. 'order_id' => $order->id,
  193. 'product_id' => $product->id,
  194. ]);
  195. $productSku2 = ProductSKU::factory()->create([
  196. 'order_id' => $order->id,
  197. 'product_id' => $product->id,
  198. ]);
  199. $this->actingAs($this->managerUser)
  200. ->post(route('reclamations.create', $order), [
  201. 'skus' => [$productSku1->id, $productSku2->id],
  202. ]);
  203. $reclamation = Reclamation::where('order_id', $order->id)->first();
  204. $this->assertCount(2, $reclamation->skus);
  205. }
  206. // ==================== Show ====================
  207. public function test_can_view_reclamation_details(): void
  208. {
  209. $reclamation = Reclamation::factory()->create();
  210. $response = $this->actingAs($this->managerUser)
  211. ->get(route('reclamations.show', $reclamation));
  212. $response->assertStatus(200);
  213. $response->assertViewIs('reclamations.edit');
  214. }
  215. public function test_reclamation_card_displays_type_and_payment_action_only_for_dkr(): void
  216. {
  217. $dkr = Reclamation::factory()->dkr()->create();
  218. $other = Reclamation::factory()->other()->create();
  219. $this->actingAs($this->managerUser)
  220. ->get(route('reclamations.show', $dkr))
  221. ->assertOk()
  222. ->assertSee('Тип рекламации')
  223. ->assertSee('ДКР')
  224. ->assertSee('Пакет документов на оплату');
  225. $this->actingAs($this->managerUser)
  226. ->get(route('reclamations.show', $other))
  227. ->assertOk()
  228. ->assertSee('Тип рекламации')
  229. ->assertSee('Прочее')
  230. ->assertDontSee('Пакет документов на оплату');
  231. }
  232. public function test_reclamation_show_uses_nav_context_for_back_url(): void
  233. {
  234. $reclamation = Reclamation::factory()->create();
  235. $indexResponse = $this->actingAs($this->managerUser)
  236. ->get(route('reclamations.index', [
  237. 'filters' => ['comment' => 'КС готова'],
  238. ]));
  239. $nav = $indexResponse->viewData('nav');
  240. $response = $this->actingAs($this->managerUser)
  241. ->get(route('reclamations.show', [
  242. 'reclamation' => $reclamation,
  243. 'nav' => $nav,
  244. ]));
  245. $response->assertOk();
  246. $response->assertViewHas('nav', $nav);
  247. $response->assertViewHas('back_url', function (string $backUrl): bool {
  248. if (!str_starts_with($backUrl, route('reclamations.index'))) {
  249. return false;
  250. }
  251. $query = parse_url($backUrl, PHP_URL_QUERY);
  252. parse_str((string) $query, $params);
  253. return ($params['filters']['comment'] ?? null) === 'КС готова';
  254. });
  255. }
  256. public function test_reclamations_index_starts_new_navigation_context(): void
  257. {
  258. $response = $this->actingAs($this->managerUser)
  259. ->withSession([
  260. 'navigation' => [
  261. 'existing-card-nav' => [
  262. 'updated_at' => time(),
  263. 'stack' => [
  264. route('order.show', Order::factory()->create()),
  265. route('reclamations.show', Reclamation::factory()->create()),
  266. ],
  267. ],
  268. ],
  269. ])
  270. ->get(route('reclamations.index', ['nav' => 'existing-card-nav']));
  271. $response->assertOk();
  272. $response->assertViewHas('nav', function (string $nav): bool {
  273. return $nav !== 'existing-card-nav';
  274. });
  275. }
  276. public function test_reclamation_show_returns_to_order_after_coming_back_from_catalog(): void
  277. {
  278. $order = Order::factory()->create();
  279. $reclamation = Reclamation::factory()->create([
  280. 'order_id' => $order->id,
  281. 'user_id' => $this->managerUser->id,
  282. ]);
  283. $response = $this->actingAs($this->managerUser)
  284. ->withSession([
  285. 'navigation' => [
  286. 'order-reclamation-nav' => [
  287. 'updated_at' => time(),
  288. 'stack' => [
  289. route('order.show', $order),
  290. route('reclamations.show', $reclamation),
  291. route('catalog.show', Product::factory()->create()),
  292. ],
  293. ],
  294. ],
  295. ])
  296. ->get(route('reclamations.show', [
  297. 'reclamation' => $reclamation,
  298. 'nav' => 'order-reclamation-nav',
  299. ]));
  300. $response->assertOk();
  301. $response->assertViewHas('back_url', route('order.show', [
  302. 'order' => $order,
  303. 'nav' => 'order-reclamation-nav',
  304. ]));
  305. }
  306. public function test_existing_reclamation_opened_from_order_keeps_order_as_back_target_after_catalog(): void
  307. {
  308. $order = Order::factory()->create([
  309. 'object_address' => 'ул. Навигационная, д. 7',
  310. ]);
  311. $product = Product::factory()->create();
  312. $productSku = ProductSKU::factory()->create([
  313. 'order_id' => $order->id,
  314. 'product_id' => $product->id,
  315. ]);
  316. $reclamation = Reclamation::factory()->create([
  317. 'order_id' => $order->id,
  318. 'user_id' => $this->managerUser->id,
  319. ]);
  320. $reclamation->skus()->attach($productSku->id);
  321. $indexResponse = $this->actingAs($this->managerUser)
  322. ->get(route('order.index'));
  323. $nav = $indexResponse->viewData('nav');
  324. $orderResponse = $this->actingAs($this->managerUser)
  325. ->get(route('order.show', ['order' => $order, 'nav' => $nav]));
  326. $orderResponse->assertOk();
  327. $reclamationResponse = $this->actingAs($this->managerUser)
  328. ->get(route('reclamations.show', ['reclamation' => $reclamation, 'nav' => $nav]));
  329. $reclamationResponse->assertOk();
  330. $reclamationResponse->assertViewHas('back_url', route('order.show', [
  331. 'order' => $order,
  332. 'nav' => $nav,
  333. ]));
  334. $catalogResponse = $this->actingAs($this->managerUser)
  335. ->get(route('catalog.show', ['product' => $product, 'nav' => $nav]));
  336. $catalogResponse->assertOk();
  337. $reclamationBackResponse = $this->actingAs($this->managerUser)
  338. ->get(route('reclamations.show', ['reclamation' => $reclamation, 'nav' => $nav]));
  339. $reclamationBackResponse->assertOk();
  340. $reclamationBackResponse->assertViewHas('back_url', route('order.show', [
  341. 'order' => $order,
  342. 'nav' => $nav,
  343. ]));
  344. }
  345. public function test_reclamation_details_show_spare_part_note_in_input_instead_of_used_in_maf(): void
  346. {
  347. $sparePart = \App\Models\SparePart::factory()->create([
  348. 'article' => 'SP-100',
  349. 'used_in_maf' => 'Старое значение',
  350. 'note' => 'Показать это примечание',
  351. ]);
  352. $reclamation = Reclamation::factory()->create();
  353. $reclamation->spareParts()->attach($sparePart->id, [
  354. 'quantity' => 1,
  355. 'with_documents' => false,
  356. 'status' => 'pending',
  357. 'reserved_qty' => 0,
  358. 'issued_qty' => 0,
  359. ]);
  360. $response = $this->actingAs($this->managerUser)
  361. ->get(route('reclamations.show', $reclamation));
  362. $response->assertOk();
  363. $response->assertSee('SP-100 (Показать это примечание)');
  364. $response->assertDontSee('SP-100 (Старое значение)');
  365. }
  366. public function test_brigadier_cannot_view_reclamation_details_with_hidden_status(): void
  367. {
  368. $reclamation = Reclamation::factory()->create([
  369. 'brigadier_id' => $this->brigadierUser->id,
  370. 'status_id' => Reclamation::STATUS_DONE,
  371. ]);
  372. $response = $this->actingAs($this->brigadierUser)
  373. ->get(route('reclamations.show', $reclamation));
  374. $response->assertStatus(403);
  375. }
  376. // ==================== Update ====================
  377. public function test_can_update_reclamation(): void
  378. {
  379. $reclamation = Reclamation::factory()->create([
  380. 'reason' => 'Старая причина',
  381. ]);
  382. // Route uses POST, not PUT. All required fields must be sent.
  383. $response = $this->actingAs($this->managerUser)
  384. ->post(route('reclamations.update', $reclamation), [
  385. 'user_id' => $reclamation->user_id,
  386. 'status_id' => $reclamation->status_id,
  387. 'create_date' => $reclamation->create_date,
  388. 'finish_date' => $reclamation->finish_date,
  389. 'reason' => 'Новая причина',
  390. 'guarantee' => 'Гарантия',
  391. 'whats_done' => 'Что сделано',
  392. ]);
  393. $location = $response->headers->get('Location');
  394. $this->assertNotNull($location);
  395. $this->assertStringContainsString('/reclamations/show/' . $reclamation->id, $location);
  396. $this->assertStringContainsString('nav=', $location);
  397. $this->assertDatabaseHas('reclamations', [
  398. 'id' => $reclamation->id,
  399. 'reason' => 'Новая причина',
  400. ]);
  401. }
  402. public function test_update_redirects_with_nav_token(): void
  403. {
  404. $reclamation = Reclamation::factory()->create([
  405. 'reason' => 'Старая причина',
  406. ]);
  407. $nav = 'nav-test-token';
  408. $response = $this->actingAs($this->managerUser)
  409. ->withSession([
  410. 'navigation' => [
  411. $nav => [
  412. 'updated_at' => time(),
  413. 'stack' => [
  414. route('reclamations.index'),
  415. route('reclamations.show', $reclamation),
  416. ],
  417. ],
  418. ],
  419. ])
  420. ->post(route('reclamations.update', $reclamation), [
  421. 'nav' => $nav,
  422. 'user_id' => $reclamation->user_id,
  423. 'status_id' => $reclamation->status_id,
  424. 'create_date' => $reclamation->create_date,
  425. 'finish_date' => $reclamation->finish_date,
  426. 'reason' => 'Новая причина',
  427. 'guarantee' => 'Гарантия',
  428. 'whats_done' => 'Что сделано',
  429. ]);
  430. $response->assertRedirect(route('reclamations.show', [
  431. 'reclamation' => $reclamation,
  432. 'nav' => $nav,
  433. ]));
  434. }
  435. public function test_ajax_update_returns_no_content_without_redirect_location(): void
  436. {
  437. $reclamation = Reclamation::factory()->create([
  438. 'reason' => 'Старая причина',
  439. ]);
  440. $response = $this->actingAs($this->managerUser)
  441. ->withHeader('X-Requested-With', 'XMLHttpRequest')
  442. ->post(route('reclamations.update', $reclamation), [
  443. 'nav' => 'ajax-nav-token',
  444. 'user_id' => $reclamation->user_id,
  445. 'status_id' => $reclamation->status_id,
  446. 'create_date' => $reclamation->create_date,
  447. 'finish_date' => $reclamation->finish_date,
  448. 'reason' => 'Новая причина',
  449. 'guarantee' => 'Гарантия',
  450. 'whats_done' => 'Что сделано',
  451. ]);
  452. $response->assertNoContent();
  453. $this->assertNull($response->headers->get('Location'));
  454. }
  455. // ==================== Delete ====================
  456. public function test_can_delete_reclamation(): void
  457. {
  458. $reclamation = Reclamation::factory()->create();
  459. $reclamationId = $reclamation->id;
  460. $response = $this->actingAs($this->adminUser)
  461. ->delete(route('reclamations.delete', $reclamation));
  462. $response->assertRedirect(route('reclamations.index'));
  463. $this->assertDatabaseMissing('reclamations', ['id' => $reclamationId]);
  464. }
  465. // ==================== Photo Before Management ====================
  466. public function test_can_upload_photo_before(): void
  467. {
  468. Storage::fake('public');
  469. $reclamation = Reclamation::factory()->create();
  470. // Use create() instead of image() to avoid GD extension requirement
  471. $photo = UploadedFile::fake()->create('photo_before.jpg', 100, 'image/jpeg');
  472. $response = $this->actingAs($this->managerUser)
  473. ->post(route('reclamations.upload-photo-before', $reclamation), [
  474. 'photo' => [$photo],
  475. ]);
  476. $response->assertRedirect();
  477. $this->assertCount(1, $reclamation->fresh()->photos_before);
  478. }
  479. public function test_can_upload_photo_before_in_webp_format(): void
  480. {
  481. Storage::fake('public');
  482. $reclamation = Reclamation::factory()->create();
  483. $photo = UploadedFile::fake()->create('photo_before.webp', 100, 'image/webp');
  484. $response = $this->actingAs($this->managerUser)
  485. ->post(route('reclamations.upload-photo-before', $reclamation), [
  486. 'photo' => [$photo],
  487. ]);
  488. $response->assertRedirect();
  489. $saved = $reclamation->fresh()->photos_before->first();
  490. $this->assertNotNull($saved);
  491. $this->assertSame('photo_before.webp', $saved->original_name);
  492. }
  493. public function test_upload_photo_before_preserves_unicode_and_quotes_filename(): void
  494. {
  495. Storage::fake('public');
  496. $reclamation = Reclamation::factory()->create();
  497. $filename = "Фото «до» 'левая' \"камера\".jpg";
  498. $photo = UploadedFile::fake()->create($filename, 100, 'image/jpeg');
  499. $response = $this->actingAs($this->managerUser)
  500. ->post(route('reclamations.upload-photo-before', $reclamation), [
  501. 'photo' => [$photo],
  502. ]);
  503. $response->assertRedirect();
  504. $saved = $reclamation->fresh()->photos_before->first();
  505. $this->assertNotNull($saved);
  506. $this->assertEquals($filename, $saved->original_name);
  507. $this->assertEquals('reclamations/' . $reclamation->id . '/photo_before/' . $filename, $saved->path);
  508. Storage::disk('public')->assertExists($saved->path);
  509. }
  510. public function test_can_delete_photo_before(): void
  511. {
  512. Storage::fake('public');
  513. $reclamation = Reclamation::factory()->create();
  514. $file = File::factory()->create();
  515. $reclamation->photos_before()->attach($file);
  516. $response = $this->actingAs($this->adminUser)
  517. ->delete(route('reclamations.delete-photo-before', [$reclamation, $file]));
  518. $response->assertRedirect();
  519. $this->assertCount(0, $reclamation->fresh()->photos_before);
  520. }
  521. // ==================== Photo After Management ====================
  522. public function test_can_upload_photo_after(): void
  523. {
  524. Storage::fake('public');
  525. $reclamation = Reclamation::factory()->create();
  526. // Use create() instead of image() to avoid GD extension requirement
  527. $photo = UploadedFile::fake()->create('photo_after.jpg', 100, 'image/jpeg');
  528. $response = $this->actingAs($this->managerUser)
  529. ->post(route('reclamations.upload-photo-after', $reclamation), [
  530. 'photo' => [$photo],
  531. ]);
  532. $response->assertRedirect();
  533. $this->assertCount(1, $reclamation->fresh()->photos_after);
  534. }
  535. public function test_can_upload_photo_after_in_webp_format(): void
  536. {
  537. Storage::fake('public');
  538. $reclamation = Reclamation::factory()->create();
  539. $photo = UploadedFile::fake()->create('photo_after.webp', 100, 'image/webp');
  540. $response = $this->actingAs($this->managerUser)
  541. ->post(route('reclamations.upload-photo-after', $reclamation), [
  542. 'photo' => [$photo],
  543. ]);
  544. $response->assertRedirect();
  545. $saved = $reclamation->fresh()->photos_after->first();
  546. $this->assertNotNull($saved);
  547. $this->assertSame('photo_after.webp', $saved->original_name);
  548. }
  549. public function test_can_delete_photo_after(): void
  550. {
  551. Storage::fake('public');
  552. $reclamation = Reclamation::factory()->create();
  553. $file = File::factory()->create();
  554. $reclamation->photos_after()->attach($file);
  555. // This route requires admin role
  556. $response = $this->actingAs($this->adminUser)
  557. ->delete(route('reclamations.delete-photo-after', [$reclamation, $file]));
  558. $response->assertRedirect();
  559. $this->assertCount(0, $reclamation->fresh()->photos_after);
  560. }
  561. // ==================== Document Management ====================
  562. public function test_can_upload_document(): void
  563. {
  564. Storage::fake('public');
  565. $reclamation = Reclamation::factory()->create();
  566. $document = UploadedFile::fake()->create('document.pdf', 100);
  567. $response = $this->actingAs($this->managerUser)
  568. ->post(route('reclamations.upload-document', $reclamation), [
  569. 'document' => [$document],
  570. ]);
  571. $response->assertRedirect();
  572. $this->assertCount(1, $reclamation->fresh()->documents);
  573. }
  574. public function test_upload_document_preserves_unicode_and_quotes_filename(): void
  575. {
  576. Storage::fake('public');
  577. $reclamation = Reclamation::factory()->create();
  578. $filename = "Рекламация «док» 'версия' \"A\".pdf";
  579. $document = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  580. $response = $this->actingAs($this->managerUser)
  581. ->post(route('reclamations.upload-document', $reclamation), [
  582. 'document' => [$document],
  583. ]);
  584. $response->assertRedirect();
  585. $saved = $reclamation->fresh()->documents->first();
  586. $this->assertNotNull($saved);
  587. $this->assertEquals($filename, $saved->original_name);
  588. $this->assertEquals('reclamations/' . $reclamation->id . '/document/' . $filename, $saved->path);
  589. Storage::disk('public')->assertExists($saved->path);
  590. }
  591. public function test_can_delete_document(): void
  592. {
  593. Storage::fake('public');
  594. $reclamation = Reclamation::factory()->create();
  595. $file = File::factory()->create();
  596. $reclamation->documents()->attach($file);
  597. // This route requires admin role
  598. $response = $this->actingAs($this->adminUser)
  599. ->delete(route('reclamations.delete-document', [$reclamation, $file]));
  600. $response->assertRedirect();
  601. $this->assertCount(0, $reclamation->fresh()->documents);
  602. }
  603. // ==================== Act Management ====================
  604. public function test_can_upload_act(): void
  605. {
  606. Storage::fake('public');
  607. $reclamation = Reclamation::factory()->create();
  608. $act = UploadedFile::fake()->create('act.pdf', 100);
  609. $response = $this->actingAs($this->managerUser)
  610. ->post(route('reclamations.upload-act', $reclamation), [
  611. 'acts' => [$act],
  612. ]);
  613. $response->assertRedirect();
  614. $this->assertCount(1, $reclamation->fresh()->acts);
  615. }
  616. public function test_upload_act_preserves_unicode_and_quotes_filename(): void
  617. {
  618. Storage::fake('public');
  619. $reclamation = Reclamation::factory()->create();
  620. $filename = "Акт «сервис» 'этап' \"01\".pdf";
  621. $act = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  622. $response = $this->actingAs($this->managerUser)
  623. ->post(route('reclamations.upload-act', $reclamation), [
  624. 'acts' => [$act],
  625. ]);
  626. $response->assertRedirect();
  627. $saved = $reclamation->fresh()->acts->first();
  628. $this->assertNotNull($saved);
  629. $this->assertEquals($filename, $saved->original_name);
  630. $this->assertEquals('reclamations/' . $reclamation->id . '/act/' . $filename, $saved->path);
  631. Storage::disk('public')->assertExists($saved->path);
  632. }
  633. public function test_can_delete_act(): void
  634. {
  635. Storage::fake('public');
  636. $reclamation = Reclamation::factory()->create();
  637. $file = File::factory()->create();
  638. $reclamation->acts()->attach($file);
  639. // This route requires admin role
  640. $response = $this->actingAs($this->adminUser)
  641. ->delete(route('reclamations.delete-act', [$reclamation, $file]));
  642. $response->assertRedirect();
  643. $this->assertCount(0, $reclamation->fresh()->acts);
  644. }
  645. // ==================== Spare Parts Reservation ====================
  646. public function test_update_spare_parts_creates_reservations(): void
  647. {
  648. $reclamation = Reclamation::factory()->create();
  649. $sparePart = SparePart::factory()->create();
  650. // Create available stock
  651. SparePartOrder::factory()
  652. ->inStock()
  653. ->withDocuments(false)
  654. ->withQuantity(10)
  655. ->forSparePart($sparePart)
  656. ->create();
  657. $response = $this->actingAs($this->managerUser)
  658. ->post(route('reclamations.update-spare-parts', $reclamation), [
  659. 'rows' => [
  660. [
  661. 'spare_part_id' => $sparePart->id,
  662. 'quantity' => 3,
  663. 'with_documents' => false,
  664. ],
  665. ],
  666. ]);
  667. $response->assertRedirect();
  668. // Check spare part is attached
  669. $this->assertTrue($reclamation->fresh()->spareParts->contains($sparePart->id));
  670. // Check reservation was created
  671. $this->assertDatabaseHas('reservations', [
  672. 'reclamation_id' => $reclamation->id,
  673. 'spare_part_id' => $sparePart->id,
  674. 'reserved_qty' => 3,
  675. 'status' => Reservation::STATUS_ACTIVE,
  676. ]);
  677. }
  678. public function test_update_spare_parts_cancels_removed_reservations(): void
  679. {
  680. $reclamation = Reclamation::factory()->create();
  681. $sparePart = SparePart::factory()->create();
  682. $order = SparePartOrder::factory()
  683. ->inStock()
  684. ->withDocuments(false)
  685. ->withQuantity(10)
  686. ->forSparePart($sparePart)
  687. ->create();
  688. // Create existing reservation
  689. Reservation::factory()
  690. ->active()
  691. ->withQuantity(5)
  692. ->withDocuments(false)
  693. ->fromOrder($order)
  694. ->forReclamation($reclamation)
  695. ->create();
  696. // Attach spare part
  697. $reclamation->spareParts()->attach($sparePart->id, [
  698. 'quantity' => 5,
  699. 'with_documents' => false,
  700. 'reserved_qty' => 5,
  701. ]);
  702. // Send empty rows to remove spare part
  703. $response = $this->actingAs($this->managerUser)
  704. ->post(route('reclamations.update-spare-parts', $reclamation), [
  705. 'rows' => [],
  706. ]);
  707. $response->assertRedirect();
  708. // Check spare part is detached
  709. $this->assertFalse($reclamation->fresh()->spareParts->contains($sparePart->id));
  710. // Check reservation was cancelled
  711. $this->assertDatabaseHas('reservations', [
  712. 'reclamation_id' => $reclamation->id,
  713. 'spare_part_id' => $sparePart->id,
  714. 'status' => Reservation::STATUS_CANCELLED,
  715. ]);
  716. }
  717. // ==================== Details Management ====================
  718. public function test_update_details_creates_reclamation_detail(): void
  719. {
  720. $reclamation = Reclamation::factory()->create();
  721. $response = $this->actingAs($this->managerUser)
  722. ->post(route('reclamations.update-details', $reclamation), [
  723. 'name' => ['Деталь 1', 'Деталь 2'],
  724. 'quantity' => ['2', '3'], // Controller casts to int, send as strings like form data
  725. ]);
  726. $response->assertRedirect();
  727. $response->assertSessionHasNoErrors();
  728. $this->assertDatabaseHas('reclamation_details', [
  729. 'reclamation_id' => $reclamation->id,
  730. 'name' => 'Деталь 1',
  731. 'quantity' => 2,
  732. ]);
  733. $this->assertDatabaseHas('reclamation_details', [
  734. 'reclamation_id' => $reclamation->id,
  735. 'name' => 'Деталь 2',
  736. 'quantity' => 3,
  737. ]);
  738. }
  739. public function test_update_details_removes_detail_with_zero_quantity(): void
  740. {
  741. $reclamation = Reclamation::factory()->create();
  742. ReclamationDetail::create([
  743. 'reclamation_id' => $reclamation->id,
  744. 'name' => 'Деталь для удаления',
  745. 'quantity' => 5,
  746. ]);
  747. $response = $this->actingAs($this->managerUser)
  748. ->post(route('reclamations.update-details', $reclamation), [
  749. 'name' => ['Деталь для удаления'],
  750. 'quantity' => ['0'], // Send as string like form data
  751. ]);
  752. $response->assertRedirect();
  753. $this->assertDatabaseMissing('reclamation_details', [
  754. 'reclamation_id' => $reclamation->id,
  755. 'name' => 'Деталь для удаления',
  756. ]);
  757. }
  758. // ==================== Generation ====================
  759. public function test_can_generate_reclamation_pack(): void
  760. {
  761. $reclamation = Reclamation::factory()->create();
  762. $response = $this->actingAs($this->managerUser)
  763. ->get(route('order.generate-reclamation-pack', $reclamation));
  764. $response->assertRedirect();
  765. $response->assertSessionHas('success');
  766. }
  767. public function test_can_generate_reclamation_payment_pack_for_dkr(): void
  768. {
  769. Bus::fake([GenerateReclamationPaymentPack::class]);
  770. $reclamation = Reclamation::factory()->dkr()->create();
  771. $this->actingAs($this->managerUser)
  772. ->get(route('reclamation.generate-reclamation-payment-pack', $reclamation))
  773. ->assertRedirect()
  774. ->assertSessionHas('success');
  775. Bus::assertDispatched(GenerateReclamationPaymentPack::class);
  776. }
  777. public function test_cannot_generate_reclamation_payment_pack_for_other_type(): void
  778. {
  779. Bus::fake([GenerateReclamationPaymentPack::class]);
  780. $reclamation = Reclamation::factory()->other()->create();
  781. $this->actingAs($this->managerUser)
  782. ->get(route('reclamation.generate-reclamation-payment-pack', $reclamation))
  783. ->assertForbidden();
  784. Bus::assertNotDispatched(GenerateReclamationPaymentPack::class);
  785. }
  786. public function test_can_generate_photos_before_pack(): void
  787. {
  788. $reclamation = Reclamation::factory()->create();
  789. $response = $this->actingAs($this->managerUser)
  790. ->get(route('reclamation.generate-photos-before-pack', $reclamation));
  791. $response->assertRedirect();
  792. $response->assertSessionHas('success');
  793. }
  794. public function test_can_generate_photos_after_pack(): void
  795. {
  796. $reclamation = Reclamation::factory()->create();
  797. $response = $this->actingAs($this->managerUser)
  798. ->get(route('reclamation.generate-photos-after-pack', $reclamation));
  799. $response->assertRedirect();
  800. $response->assertSessionHas('success');
  801. }
  802. }