OrderControllerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. $response = $this->actingAs($this->managerUser)
  50. ->get(route('order.index'));
  51. $response->assertStatus(200);
  52. $response->assertSee($order->object_address);
  53. }
  54. public function test_brigadier_sees_only_assigned_orders(): void
  55. {
  56. $assignedOrder = Order::factory()->create([
  57. 'brigadier_id' => $this->brigadierUser->id,
  58. ]);
  59. $otherOrder = Order::factory()->create([
  60. 'brigadier_id' => User::factory()->create(['role' => Role::BRIGADIER])->id,
  61. ]);
  62. $response = $this->actingAs($this->brigadierUser)
  63. ->get(route('order.index'));
  64. $response->assertStatus(200);
  65. $response->assertSee($assignedOrder->object_address);
  66. $response->assertDontSee($otherOrder->object_address);
  67. }
  68. // ==================== Create ====================
  69. public function test_can_view_create_order_form(): void
  70. {
  71. $response = $this->actingAs($this->adminUser)
  72. ->get(route('order.create'));
  73. $response->assertStatus(200);
  74. $response->assertViewIs('orders.edit');
  75. }
  76. // ==================== Store ====================
  77. public function test_can_create_new_order(): void
  78. {
  79. $district = District::factory()->create();
  80. $area = Area::factory()->create();
  81. $objectType = ObjectType::factory()->create();
  82. $response = $this->actingAs($this->managerUser)
  83. ->post(route('order.store'), [
  84. 'name' => 'Тестовый заказ',
  85. 'user_id' => $this->managerUser->id,
  86. 'district_id' => $district->id,
  87. 'area_id' => $area->id,
  88. 'object_address' => 'ул. Тестовая, д. 1',
  89. 'object_type_id' => $objectType->id,
  90. 'comment' => 'Тестовый комментарий',
  91. ]);
  92. $response->assertRedirect();
  93. $this->assertDatabaseHas('orders', [
  94. 'object_address' => 'ул. Тестовая, д. 1',
  95. 'order_status_id' => Order::STATUS_NEW,
  96. ]);
  97. }
  98. public function test_creating_order_sets_tg_group_name(): void
  99. {
  100. $district = District::factory()->create(['shortname' => 'ЦАО']);
  101. $area = Area::factory()->create(['name' => 'Тверской']);
  102. $objectType = ObjectType::factory()->create();
  103. $this->actingAs($this->managerUser)
  104. ->post(route('order.store'), [
  105. 'name' => 'Тестовый заказ',
  106. 'user_id' => $this->managerUser->id,
  107. 'district_id' => $district->id,
  108. 'area_id' => $area->id,
  109. 'object_address' => 'ул. Пушкина',
  110. 'object_type_id' => $objectType->id,
  111. ]);
  112. $order = Order::where('object_address', 'ул. Пушкина')->first();
  113. $this->assertStringContainsString('ЦАО', $order->tg_group_name);
  114. $this->assertStringContainsString('Тверской', $order->tg_group_name);
  115. }
  116. public function test_can_update_existing_order(): void
  117. {
  118. $order = Order::factory()->create([
  119. 'object_address' => 'Старый адрес',
  120. ]);
  121. $response = $this->actingAs($this->managerUser)
  122. ->post(route('order.store'), [
  123. 'id' => $order->id,
  124. 'name' => $order->name,
  125. 'user_id' => $order->user_id,
  126. 'district_id' => $order->district_id,
  127. 'area_id' => $order->area_id,
  128. 'object_address' => 'Новый адрес',
  129. 'object_type_id' => $order->object_type_id,
  130. ]);
  131. $response->assertRedirect();
  132. $this->assertDatabaseHas('orders', [
  133. 'id' => $order->id,
  134. 'object_address' => 'Новый адрес',
  135. ]);
  136. }
  137. // ==================== Show ====================
  138. public function test_can_view_order_details(): void
  139. {
  140. $order = Order::factory()->create();
  141. $response = $this->actingAs($this->managerUser)
  142. ->get(route('order.show', $order));
  143. $response->assertStatus(200);
  144. $response->assertViewIs('orders.show');
  145. $response->assertSee($order->object_address);
  146. }
  147. // ==================== Edit ====================
  148. public function test_can_view_edit_order_form(): void
  149. {
  150. $order = Order::factory()->create();
  151. $response = $this->actingAs($this->managerUser)
  152. ->get(route('order.edit', $order));
  153. $response->assertStatus(200);
  154. $response->assertViewIs('orders.edit');
  155. }
  156. // ==================== Destroy ====================
  157. public function test_can_delete_order(): void
  158. {
  159. $order = Order::factory()->create();
  160. $orderId = $order->id;
  161. $response = $this->actingAs($this->adminUser)
  162. ->delete(route('order.destroy', $order));
  163. $response->assertRedirect(route('order.index'));
  164. // Order uses SoftDeletes, so check deleted_at is set
  165. $this->assertSoftDeleted('orders', ['id' => $orderId]);
  166. }
  167. public function test_deleting_order_removes_related_product_skus(): void
  168. {
  169. $order = Order::factory()->create();
  170. $product = Product::factory()->create();
  171. $sku = ProductSKU::factory()->create([
  172. 'order_id' => $order->id,
  173. 'product_id' => $product->id,
  174. ]);
  175. $this->actingAs($this->adminUser)
  176. ->delete(route('order.destroy', $order));
  177. // ProductSKU uses SoftDeletes, so check deleted_at is set
  178. $this->assertSoftDeleted('products_sku', ['id' => $sku->id]);
  179. }
  180. // ==================== Search ====================
  181. public function test_search_returns_matching_orders(): void
  182. {
  183. $order = Order::factory()->create([
  184. 'object_address' => 'ул. Уникальная Тестовая, д. 999',
  185. ]);
  186. $otherOrder = Order::factory()->create([
  187. 'object_address' => 'ул. Другая, д. 1',
  188. ]);
  189. $response = $this->actingAs($this->managerUser)
  190. ->get(route('order.index', ['s' => 'Уникальная Тестовая']));
  191. $response->assertStatus(200);
  192. $response->assertSee($order->object_address);
  193. $response->assertDontSee($otherOrder->object_address);
  194. }
  195. // ==================== MAF Operations ====================
  196. public function test_get_maf_to_order_assigns_available_maf(): void
  197. {
  198. $product = Product::factory()->create();
  199. $order = Order::factory()->create();
  200. $productSku = ProductSKU::factory()->create([
  201. 'order_id' => $order->id,
  202. 'product_id' => $product->id,
  203. 'maf_order_id' => null,
  204. 'status' => 'требуется',
  205. ]);
  206. $mafOrder = MafOrder::factory()->create([
  207. 'product_id' => $product->id,
  208. 'in_stock' => 5,
  209. ]);
  210. // This route requires admin role
  211. $response = $this->actingAs($this->adminUser)
  212. ->get(route('order.get-maf', $order));
  213. $response->assertRedirect(route('order.show', $order));
  214. $productSku->refresh();
  215. $this->assertEquals($mafOrder->id, $productSku->maf_order_id);
  216. $this->assertEquals('отгружен', $productSku->status);
  217. $mafOrder->refresh();
  218. $this->assertEquals(4, $mafOrder->in_stock);
  219. }
  220. public function test_revert_maf_returns_maf_to_stock(): void
  221. {
  222. $product = Product::factory()->create();
  223. $order = Order::factory()->create();
  224. $mafOrder = MafOrder::factory()->create([
  225. 'product_id' => $product->id,
  226. 'in_stock' => 3,
  227. ]);
  228. $productSku = ProductSKU::factory()->create([
  229. 'order_id' => $order->id,
  230. 'product_id' => $product->id,
  231. 'maf_order_id' => $mafOrder->id,
  232. 'status' => 'отгружен',
  233. ]);
  234. // This route requires admin role
  235. $response = $this->actingAs($this->adminUser)
  236. ->get(route('order.revert-maf', $order));
  237. $response->assertRedirect(route('order.show', $order));
  238. $productSku->refresh();
  239. $this->assertNull($productSku->maf_order_id);
  240. $this->assertEquals('требуется', $productSku->status);
  241. $mafOrder->refresh();
  242. $this->assertEquals(4, $mafOrder->in_stock);
  243. }
  244. public function test_move_maf_transfers_sku_to_another_order(): void
  245. {
  246. $product = Product::factory()->create();
  247. $order1 = Order::factory()->create();
  248. $order2 = Order::factory()->create();
  249. $productSku = ProductSKU::factory()->create([
  250. 'order_id' => $order1->id,
  251. 'product_id' => $product->id,
  252. ]);
  253. // This route requires admin role
  254. $response = $this->actingAs($this->adminUser)
  255. ->post(route('order.move-maf'), [
  256. 'new_order_id' => $order2->id,
  257. 'ids' => json_encode([$productSku->id]),
  258. ]);
  259. $response->assertStatus(200);
  260. $response->assertJson(['success' => true]);
  261. $productSku->refresh();
  262. $this->assertEquals($order2->id, $productSku->order_id);
  263. }
  264. // ==================== Photo Management ====================
  265. public function test_can_upload_photo_to_order(): void
  266. {
  267. Storage::fake('public');
  268. $order = Order::factory()->create();
  269. // Use create() instead of image() to avoid GD extension requirement
  270. $photo = UploadedFile::fake()->create('photo.jpg', 100, 'image/jpeg');
  271. $response = $this->actingAs($this->managerUser)
  272. ->post(route('order.upload-photo', $order), [
  273. 'photo' => [$photo],
  274. ]);
  275. $response->assertRedirect(route('order.show', $order));
  276. $this->assertCount(1, $order->fresh()->photos);
  277. }
  278. public function test_can_delete_photo_from_order(): void
  279. {
  280. Storage::fake('public');
  281. $order = Order::factory()->create();
  282. $file = File::factory()->create();
  283. $order->photos()->attach($file);
  284. // This route requires admin role
  285. $response = $this->actingAs($this->adminUser)
  286. ->delete(route('order.delete-photo', [$order, $file]));
  287. $response->assertRedirect(route('order.show', $order));
  288. $this->assertCount(0, $order->fresh()->photos);
  289. }
  290. public function test_can_delete_all_photos_from_order(): void
  291. {
  292. Storage::fake('public');
  293. $order = Order::factory()->create();
  294. $files = File::factory()->count(3)->create();
  295. $order->photos()->attach($files->pluck('id'));
  296. // This route requires admin role
  297. $response = $this->actingAs($this->adminUser)
  298. ->delete(route('order.delete-all-photos', $order));
  299. $response->assertRedirect(route('order.show', $order));
  300. $this->assertCount(0, $order->fresh()->photos);
  301. }
  302. // ==================== Document Management ====================
  303. public function test_can_upload_document_to_order(): void
  304. {
  305. Storage::fake('public');
  306. $order = Order::factory()->create();
  307. $document = UploadedFile::fake()->create('document.pdf', 100);
  308. $response = $this->actingAs($this->managerUser)
  309. ->post(route('order.upload-document', $order), [
  310. 'document' => [$document],
  311. ]);
  312. $response->assertRedirect(route('order.show', $order));
  313. $this->assertCount(1, $order->fresh()->documents);
  314. }
  315. public function test_upload_document_limits_to_5_files(): void
  316. {
  317. Storage::fake('public');
  318. $order = Order::factory()->create();
  319. $documents = [];
  320. for ($i = 0; $i < 7; $i++) {
  321. $documents[] = UploadedFile::fake()->create("document{$i}.pdf", 100);
  322. }
  323. $this->actingAs($this->managerUser)
  324. ->post(route('order.upload-document', $order), [
  325. 'document' => $documents,
  326. ]);
  327. $this->assertCount(5, $order->fresh()->documents);
  328. }
  329. // ==================== Generation ====================
  330. public function test_generate_installation_pack_requires_correct_status(): void
  331. {
  332. $order = Order::factory()->create([
  333. 'order_status_id' => Order::STATUS_NEW,
  334. ]);
  335. $response = $this->actingAs($this->managerUser)
  336. ->get(route('order.generate-installation-pack', $order));
  337. $response->assertRedirect(route('order.show', $order));
  338. $response->assertSessionHas('danger');
  339. }
  340. public function test_generate_installation_pack_succeeds_with_correct_status(): void
  341. {
  342. $product = Product::factory()->create();
  343. $mafOrder = MafOrder::factory()->create(['product_id' => $product->id]);
  344. $order = Order::factory()->create([
  345. 'order_status_id' => Order::STATUS_READY_TO_MOUNT,
  346. ]);
  347. // Create SKU with MAF assigned
  348. ProductSKU::factory()->create([
  349. 'order_id' => $order->id,
  350. 'product_id' => $product->id,
  351. 'maf_order_id' => $mafOrder->id,
  352. ]);
  353. $response = $this->actingAs($this->managerUser)
  354. ->get(route('order.generate-installation-pack', $order));
  355. $response->assertRedirect(route('order.show', $order));
  356. $response->assertSessionHas('success');
  357. }
  358. // ==================== Export ====================
  359. public function test_can_export_orders(): void
  360. {
  361. Order::factory()->count(3)->create();
  362. // This route requires admin role
  363. $response = $this->actingAs($this->adminUser)
  364. ->post(route('order.export'));
  365. $response->assertRedirect(route('order.index'));
  366. $response->assertSessionHas('success');
  367. }
  368. public function test_can_export_single_order(): void
  369. {
  370. $order = Order::factory()->create();
  371. // This route requires admin role
  372. $response = $this->actingAs($this->adminUser)
  373. ->post(route('order.export-one', $order));
  374. $response->assertRedirect(route('order.show', $order));
  375. $response->assertSessionHas('success');
  376. }
  377. }