OrderControllerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Dictionary\Area;
  4. use App\Models\Dictionary\District;
  5. use App\Models\File;
  6. use App\Models\MafOrder;
  7. use App\Models\ObjectType;
  8. use App\Models\Order;
  9. use App\Models\OrderStatus;
  10. use App\Models\Product;
  11. use App\Models\ProductSKU;
  12. use App\Models\Role;
  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 OrderControllerTest extends TestCase
  19. {
  20. use RefreshDatabase;
  21. protected $seed = true;
  22. private User $adminUser;
  23. private User $managerUser;
  24. private User $brigadierUser;
  25. protected function setUp(): void
  26. {
  27. parent::setUp();
  28. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  29. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  30. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  31. }
  32. // ==================== Authentication ====================
  33. public function test_guest_cannot_access_orders_index(): void
  34. {
  35. $response = $this->get(route('order.index'));
  36. $response->assertRedirect(route('login'));
  37. }
  38. public function test_authenticated_user_can_access_orders_index(): void
  39. {
  40. $response = $this->actingAs($this->managerUser)
  41. ->get(route('order.index'));
  42. $response->assertStatus(200);
  43. $response->assertViewIs('orders.index');
  44. }
  45. // ==================== Index ====================
  46. public function test_orders_index_displays_orders(): void
  47. {
  48. $order = Order::factory()->create([
  49. 'object_address' => 'ул. Проверочная, д. 101',
  50. ]);
  51. $response = $this->actingAs($this->managerUser)
  52. ->get(route('order.index'));
  53. $response->assertStatus(200);
  54. $response->assertSee($order->object_address);
  55. }
  56. public function test_brigadier_sees_only_assigned_orders(): void
  57. {
  58. $assignedOrder = Order::factory()->create([
  59. 'brigadier_id' => $this->brigadierUser->id,
  60. 'object_address' => 'ул. Бригадирская, д. 10',
  61. ]);
  62. $otherOrder = Order::factory()->create([
  63. 'brigadier_id' => User::factory()->create(['role' => Role::BRIGADIER])->id,
  64. 'object_address' => 'ул. Чужая, д. 20',
  65. ]);
  66. $response = $this->actingAs($this->brigadierUser)
  67. ->get(route('order.index'));
  68. $response->assertStatus(200);
  69. $response->assertSee($assignedOrder->object_address);
  70. $response->assertDontSee($otherOrder->object_address);
  71. }
  72. // ==================== Create ====================
  73. public function test_can_view_create_order_form(): void
  74. {
  75. $response = $this->actingAs($this->adminUser)
  76. ->get(route('order.create'));
  77. $response->assertStatus(200);
  78. $response->assertViewIs('orders.edit');
  79. }
  80. // ==================== Store ====================
  81. public function test_can_create_new_order(): void
  82. {
  83. $district = District::factory()->create();
  84. $area = Area::factory()->create();
  85. $objectType = ObjectType::factory()->create();
  86. $response = $this->actingAs($this->managerUser)
  87. ->post(route('order.store'), [
  88. 'name' => 'Тестовый заказ',
  89. 'user_id' => $this->managerUser->id,
  90. 'district_id' => $district->id,
  91. 'area_id' => $area->id,
  92. 'object_address' => 'ул. Тестовая, д. 1',
  93. 'object_type_id' => $objectType->id,
  94. 'comment' => 'Тестовый комментарий',
  95. ]);
  96. $response->assertRedirect();
  97. $this->assertDatabaseHas('orders', [
  98. 'object_address' => 'ул. Тестовая, д. 1',
  99. 'order_status_id' => Order::STATUS_NEW,
  100. ]);
  101. }
  102. public function test_creating_order_sets_tg_group_name(): void
  103. {
  104. $district = District::factory()->create(['shortname' => 'ЦАО']);
  105. $area = Area::factory()->create(['name' => 'Тверской']);
  106. $objectType = ObjectType::factory()->create();
  107. $this->actingAs($this->managerUser)
  108. ->post(route('order.store'), [
  109. 'name' => 'Тестовый заказ',
  110. 'user_id' => $this->managerUser->id,
  111. 'district_id' => $district->id,
  112. 'area_id' => $area->id,
  113. 'object_address' => 'ул. Пушкина',
  114. 'object_type_id' => $objectType->id,
  115. ]);
  116. $order = Order::where('object_address', 'ул. Пушкина')->first();
  117. $this->assertStringContainsString('ЦАО', $order->tg_group_name);
  118. $this->assertStringContainsString('Тверской', $order->tg_group_name);
  119. }
  120. public function test_can_update_existing_order(): void
  121. {
  122. $order = Order::factory()->create([
  123. 'object_address' => 'Старый адрес',
  124. ]);
  125. $response = $this->actingAs($this->managerUser)
  126. ->post(route('order.store'), [
  127. 'id' => $order->id,
  128. 'name' => $order->name,
  129. 'user_id' => $order->user_id,
  130. 'district_id' => $order->district_id,
  131. 'area_id' => $order->area_id,
  132. 'object_address' => 'Новый адрес',
  133. 'object_type_id' => $order->object_type_id,
  134. ]);
  135. $response->assertRedirect();
  136. $this->assertDatabaseHas('orders', [
  137. 'id' => $order->id,
  138. 'object_address' => 'Новый адрес',
  139. ]);
  140. }
  141. // ==================== Show ====================
  142. public function test_can_view_order_details(): void
  143. {
  144. $order = Order::factory()->create([
  145. 'object_address' => 'ул. Детальная, д. 5',
  146. ]);
  147. $response = $this->actingAs($this->managerUser)
  148. ->get(route('order.show', $order));
  149. $response->assertStatus(200);
  150. $response->assertViewIs('orders.show');
  151. $response->assertSee($order->object_address);
  152. }
  153. // ==================== Edit ====================
  154. public function test_can_view_edit_order_form(): void
  155. {
  156. $order = Order::factory()->create();
  157. $response = $this->actingAs($this->managerUser)
  158. ->get(route('order.edit', $order));
  159. $response->assertStatus(200);
  160. $response->assertViewIs('orders.edit');
  161. }
  162. // ==================== Destroy ====================
  163. public function test_can_delete_order(): void
  164. {
  165. $order = Order::factory()->create();
  166. $orderId = $order->id;
  167. $response = $this->actingAs($this->adminUser)
  168. ->delete(route('order.destroy', $order));
  169. $response->assertRedirect(route('order.index'));
  170. // Order uses SoftDeletes, so check deleted_at is set
  171. $this->assertSoftDeleted('orders', ['id' => $orderId]);
  172. }
  173. public function test_deleting_order_removes_related_product_skus(): void
  174. {
  175. $order = Order::factory()->create();
  176. $product = Product::factory()->create();
  177. $sku = ProductSKU::factory()->create([
  178. 'order_id' => $order->id,
  179. 'product_id' => $product->id,
  180. ]);
  181. $this->actingAs($this->adminUser)
  182. ->delete(route('order.destroy', $order));
  183. // ProductSKU uses SoftDeletes, so check deleted_at is set
  184. $this->assertSoftDeleted('products_sku', ['id' => $sku->id]);
  185. }
  186. // ==================== Search ====================
  187. public function test_search_returns_matching_orders(): void
  188. {
  189. $order = Order::factory()->create([
  190. 'object_address' => 'ул. Уникальная Тестовая, д. 999',
  191. ]);
  192. $otherOrder = Order::factory()->create([
  193. 'object_address' => 'ул. Другая, д. 1',
  194. ]);
  195. $response = $this->actingAs($this->managerUser)
  196. ->get(route('order.index', ['s' => 'Уникальная Тестовая']));
  197. $response->assertStatus(200);
  198. $response->assertSee($order->object_address);
  199. $response->assertDontSee($otherOrder->object_address);
  200. }
  201. // ==================== MAF Operations ====================
  202. public function test_get_maf_to_order_assigns_available_maf(): void
  203. {
  204. $product = Product::factory()->create();
  205. $order = Order::factory()->create();
  206. $productSku = ProductSKU::factory()->create([
  207. 'order_id' => $order->id,
  208. 'product_id' => $product->id,
  209. 'maf_order_id' => null,
  210. 'status' => 'требуется',
  211. ]);
  212. $mafOrder = MafOrder::factory()->create([
  213. 'product_id' => $product->id,
  214. 'in_stock' => 5,
  215. ]);
  216. // This route requires admin role
  217. $response = $this->actingAs($this->adminUser)
  218. ->get(route('order.get-maf', $order));
  219. $response->assertRedirect(route('order.show', $order));
  220. $response->assertSessionHas('success', function ($messages) {
  221. return str_contains(implode(' ', (array) $messages), 'Привязано МАФ: 1.');
  222. });
  223. $productSku->refresh();
  224. $this->assertEquals($mafOrder->id, $productSku->maf_order_id);
  225. $this->assertEquals('отгружен', $productSku->status);
  226. $mafOrder->refresh();
  227. $this->assertEquals(4, $mafOrder->in_stock);
  228. }
  229. public function test_get_maf_to_order_shows_error_when_not_enough_stock(): void
  230. {
  231. $product = Product::factory()->create();
  232. $order = Order::factory()->create();
  233. $productSku = ProductSKU::factory()->create([
  234. 'order_id' => $order->id,
  235. 'product_id' => $product->id,
  236. 'maf_order_id' => null,
  237. 'status' => 'требуется',
  238. ]);
  239. $response = $this->actingAs($this->adminUser)
  240. ->get(route('order.get-maf', $order));
  241. $response->assertRedirect(route('order.show', $order));
  242. $response->assertSessionHas('danger', function ($messages) {
  243. return str_contains(implode(' ', (array) $messages), 'Не удалось привязать 1 шт. МАФ');
  244. });
  245. $productSku->refresh();
  246. $this->assertNull($productSku->maf_order_id);
  247. $this->assertEquals('требуется', $productSku->status);
  248. }
  249. public function test_revert_maf_returns_maf_to_stock(): void
  250. {
  251. $product = Product::factory()->create();
  252. $order = Order::factory()->create();
  253. $mafOrder = MafOrder::factory()->create([
  254. 'product_id' => $product->id,
  255. 'in_stock' => 3,
  256. ]);
  257. $productSku = ProductSKU::factory()->create([
  258. 'order_id' => $order->id,
  259. 'product_id' => $product->id,
  260. 'maf_order_id' => $mafOrder->id,
  261. 'status' => 'отгружен',
  262. ]);
  263. // This route requires admin role
  264. $response = $this->actingAs($this->adminUser)
  265. ->get(route('order.revert-maf', $order));
  266. $response->assertRedirect(route('order.show', $order));
  267. $response->assertSessionHas('success', function ($messages) {
  268. return str_contains(implode(' ', (array) $messages), 'Отвязано МАФ: 1.');
  269. });
  270. $productSku->refresh();
  271. $this->assertNull($productSku->maf_order_id);
  272. $this->assertEquals('требуется', $productSku->status);
  273. $mafOrder->refresh();
  274. $this->assertEquals(4, $mafOrder->in_stock);
  275. }
  276. public function test_revert_maf_shows_error_when_nothing_is_attached(): void
  277. {
  278. $order = Order::factory()->create();
  279. ProductSKU::factory()->create([
  280. 'order_id' => $order->id,
  281. 'maf_order_id' => null,
  282. 'status' => 'требуется',
  283. ]);
  284. $response = $this->actingAs($this->adminUser)
  285. ->get(route('order.revert-maf', $order));
  286. $response->assertRedirect(route('order.show', $order));
  287. $response->assertSessionHas('danger', function ($messages) {
  288. return str_contains(implode(' ', (array) $messages), 'Нет МАФ для отвязки');
  289. });
  290. }
  291. public function test_store_order_cannot_remove_attached_maf_from_order_list(): void
  292. {
  293. $attachedProduct = Product::factory()->create();
  294. $otherProduct = Product::factory()->create();
  295. $order = Order::factory()->create([
  296. 'order_status_id' => Order::STATUS_NEW,
  297. ]);
  298. $mafOrder = MafOrder::factory()->create([
  299. 'product_id' => $attachedProduct->id,
  300. 'in_stock' => 1,
  301. ]);
  302. $attachedSku = ProductSKU::factory()->create([
  303. 'order_id' => $order->id,
  304. 'product_id' => $attachedProduct->id,
  305. 'maf_order_id' => $mafOrder->id,
  306. 'status' => 'отгружен',
  307. ]);
  308. ProductSKU::factory()->create([
  309. 'order_id' => $order->id,
  310. 'product_id' => $otherProduct->id,
  311. 'maf_order_id' => null,
  312. 'status' => 'требуется',
  313. ]);
  314. $response = $this->actingAs($this->adminUser)
  315. ->post(route('order.store'), [
  316. 'id' => $order->id,
  317. 'products' => [$otherProduct->id],
  318. 'quantity' => [1],
  319. ]);
  320. $response->assertRedirect();
  321. $response->assertSessionHas('danger', function ($messages) {
  322. return str_contains(implode(' ', (array) $messages), 'Нельзя удалить привязанные МАФ');
  323. });
  324. $this->assertDatabaseHas('products_sku', [
  325. 'id' => $attachedSku->id,
  326. 'maf_order_id' => $mafOrder->id,
  327. 'deleted_at' => null,
  328. ]);
  329. }
  330. public function test_store_order_cannot_add_new_positions_when_has_attached_maf(): void
  331. {
  332. $attachedProduct = Product::factory()->create();
  333. $existingProduct = Product::factory()->create();
  334. $newProduct = Product::factory()->create();
  335. $order = Order::factory()->create([
  336. 'order_status_id' => Order::STATUS_NEW,
  337. ]);
  338. $mafOrder = MafOrder::factory()->create([
  339. 'product_id' => $attachedProduct->id,
  340. 'in_stock' => 1,
  341. ]);
  342. ProductSKU::factory()->create([
  343. 'order_id' => $order->id,
  344. 'product_id' => $attachedProduct->id,
  345. 'maf_order_id' => $mafOrder->id,
  346. 'status' => 'отгружен',
  347. ]);
  348. ProductSKU::factory()->create([
  349. 'order_id' => $order->id,
  350. 'product_id' => $existingProduct->id,
  351. 'maf_order_id' => null,
  352. 'status' => 'требуется',
  353. ]);
  354. $response = $this->actingAs($this->adminUser)
  355. ->post(route('order.store'), [
  356. 'id' => $order->id,
  357. 'products' => [$attachedProduct->id, $existingProduct->id, $newProduct->id],
  358. 'quantity' => [1, 1, 1],
  359. ]);
  360. $response->assertRedirect();
  361. $response->assertSessionHas('danger', function ($messages) {
  362. return str_contains(implode(' ', (array) $messages), 'Нельзя добавлять новые позиции МАФ');
  363. });
  364. $this->assertDatabaseMissing('products_sku', [
  365. 'order_id' => $order->id,
  366. 'product_id' => $newProduct->id,
  367. 'deleted_at' => null,
  368. ]);
  369. }
  370. public function test_move_maf_transfers_sku_to_another_order(): void
  371. {
  372. $product = Product::factory()->create();
  373. $order1 = Order::factory()->create();
  374. $order2 = Order::factory()->create();
  375. $productSku = ProductSKU::factory()->create([
  376. 'order_id' => $order1->id,
  377. 'product_id' => $product->id,
  378. ]);
  379. // This route requires admin role
  380. $response = $this->actingAs($this->adminUser)
  381. ->post(route('order.move-maf'), [
  382. 'new_order_id' => $order2->id,
  383. 'ids' => json_encode([$productSku->id]),
  384. ]);
  385. $response->assertStatus(200);
  386. $response->assertJson(['success' => true]);
  387. $productSku->refresh();
  388. $this->assertEquals($order2->id, $productSku->order_id);
  389. }
  390. // ==================== Photo Management ====================
  391. public function test_can_upload_photo_to_order(): void
  392. {
  393. Storage::fake('public');
  394. $order = Order::factory()->create();
  395. // Use create() instead of image() to avoid GD extension requirement
  396. $photo = UploadedFile::fake()->create('photo.jpg', 100, 'image/jpeg');
  397. $response = $this->actingAs($this->managerUser)
  398. ->post(route('order.upload-photo', $order), [
  399. 'photo' => [$photo],
  400. ]);
  401. $response->assertRedirect(route('order.show', $order));
  402. $this->assertCount(1, $order->fresh()->photos);
  403. }
  404. public function test_can_delete_photo_from_order(): void
  405. {
  406. Storage::fake('public');
  407. $order = Order::factory()->create();
  408. $file = File::factory()->create();
  409. $order->photos()->attach($file);
  410. // This route requires admin role
  411. $response = $this->actingAs($this->adminUser)
  412. ->delete(route('order.delete-photo', [$order, $file]));
  413. $response->assertRedirect(route('order.show', $order));
  414. $this->assertCount(0, $order->fresh()->photos);
  415. }
  416. public function test_can_delete_all_photos_from_order(): void
  417. {
  418. Storage::fake('public');
  419. $order = Order::factory()->create();
  420. $files = File::factory()->count(3)->create();
  421. $order->photos()->attach($files->pluck('id'));
  422. // This route requires admin role
  423. $response = $this->actingAs($this->adminUser)
  424. ->delete(route('order.delete-all-photos', $order));
  425. $response->assertRedirect(route('order.show', $order));
  426. $this->assertCount(0, $order->fresh()->photos);
  427. }
  428. // ==================== Document Management ====================
  429. public function test_can_upload_document_to_order(): void
  430. {
  431. Storage::fake('public');
  432. $order = Order::factory()->create();
  433. $document = UploadedFile::fake()->create('document.pdf', 100);
  434. $response = $this->actingAs($this->managerUser)
  435. ->post(route('order.upload-document', $order), [
  436. 'document' => [$document],
  437. ]);
  438. $response->assertRedirect(route('order.show', $order));
  439. $this->assertCount(1, $order->fresh()->documents);
  440. }
  441. public function test_upload_document_preserves_unicode_and_quotes_filename(): void
  442. {
  443. Storage::fake('public');
  444. $order = Order::factory()->create();
  445. $filename = "Документ «смета» 'финал' \"v2\".pdf";
  446. $document = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  447. $response = $this->actingAs($this->managerUser)
  448. ->post(route('order.upload-document', $order), [
  449. 'document' => [$document],
  450. ]);
  451. $response->assertRedirect(route('order.show', $order));
  452. $saved = $order->fresh()->documents->first();
  453. $this->assertNotNull($saved);
  454. $this->assertEquals($filename, $saved->original_name);
  455. $this->assertEquals('orders/' . $order->id . '/document/' . $filename, $saved->path);
  456. Storage::disk('public')->assertExists($saved->path);
  457. }
  458. public function test_upload_statement_preserves_unicode_and_quotes_filename(): void
  459. {
  460. Storage::fake('public');
  461. $order = Order::factory()->create();
  462. $filename = "Акт «приемки» 'этап 1' \"финал\".pdf";
  463. $statement = UploadedFile::fake()->create($filename, 100, 'application/pdf');
  464. $response = $this->actingAs($this->managerUser)
  465. ->post(route('order.upload-statement', $order), [
  466. 'statement' => [$statement],
  467. ]);
  468. $response->assertRedirect(route('order.show', $order));
  469. $saved = $order->fresh()->statements->first();
  470. $this->assertNotNull($saved);
  471. $this->assertEquals($filename, $saved->original_name);
  472. $this->assertEquals('orders/' . $order->id . '/statement/' . $filename, $saved->path);
  473. Storage::disk('public')->assertExists($saved->path);
  474. }
  475. public function test_upload_document_limits_to_5_files(): void
  476. {
  477. Storage::fake('public');
  478. $order = Order::factory()->create();
  479. $documents = [];
  480. for ($i = 0; $i < 7; $i++) {
  481. $documents[] = UploadedFile::fake()->create("document{$i}.pdf", 100);
  482. }
  483. $this->actingAs($this->managerUser)
  484. ->post(route('order.upload-document', $order), [
  485. 'document' => $documents,
  486. ]);
  487. $this->assertCount(5, $order->fresh()->documents);
  488. }
  489. // ==================== Generation ====================
  490. public function test_generate_installation_pack_requires_correct_status(): void
  491. {
  492. $order = Order::factory()->create([
  493. 'order_status_id' => Order::STATUS_NEW,
  494. ]);
  495. $response = $this->actingAs($this->managerUser)
  496. ->get(route('order.generate-installation-pack', $order));
  497. $response->assertRedirect(route('order.show', $order));
  498. $response->assertSessionHas('danger');
  499. }
  500. public function test_generate_installation_pack_succeeds_with_correct_status(): void
  501. {
  502. $product = Product::factory()->create();
  503. $mafOrder = MafOrder::factory()->create(['product_id' => $product->id]);
  504. $order = Order::factory()->create([
  505. 'order_status_id' => Order::STATUS_READY_TO_MOUNT,
  506. ]);
  507. // Create SKU with MAF assigned
  508. ProductSKU::factory()->create([
  509. 'order_id' => $order->id,
  510. 'product_id' => $product->id,
  511. 'maf_order_id' => $mafOrder->id,
  512. ]);
  513. $response = $this->actingAs($this->managerUser)
  514. ->get(route('order.generate-installation-pack', $order));
  515. $response->assertRedirect(route('order.show', $order));
  516. $response->assertSessionHas('success');
  517. }
  518. // ==================== Export ====================
  519. public function test_can_export_orders(): void
  520. {
  521. Order::factory()->count(3)->create();
  522. // This route requires admin role
  523. $response = $this->actingAs($this->adminUser)
  524. ->post(route('order.export'));
  525. $response->assertRedirect(route('order.index'));
  526. $response->assertSessionHas('success');
  527. }
  528. public function test_can_export_single_order(): void
  529. {
  530. $order = Order::factory()->create();
  531. // This route requires admin role
  532. $response = $this->actingAs($this->adminUser)
  533. ->post(route('order.export-one', $order));
  534. $response->assertRedirect(route('order.show', $order));
  535. $response->assertSessionHas('success');
  536. }
  537. }