MafOrderControllerTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\MafOrder;
  4. use App\Models\Product;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\TestCase;
  9. class MafOrderControllerTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. protected $seed = true;
  13. private User $adminUser;
  14. private User $managerUser;
  15. private User $assistantHeadUser;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  20. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  21. $this->assistantHeadUser = User::factory()->create(['role' => Role::ASSISTANT_HEAD]);
  22. }
  23. // ==================== Authentication ====================
  24. public function test_guest_cannot_access_maf_orders_index(): void
  25. {
  26. $response = $this->get(route('maf_order.index'));
  27. $response->assertRedirect(route('login'));
  28. }
  29. public function test_authenticated_admin_can_access_maf_orders_index(): void
  30. {
  31. $response = $this->actingAs($this->adminUser)
  32. ->get(route('maf_order.index'));
  33. $response->assertStatus(200);
  34. $response->assertViewIs('maf_orders.index');
  35. }
  36. public function test_manager_cannot_access_maf_orders_index(): void
  37. {
  38. $response = $this->actingAs($this->managerUser)
  39. ->get(route('maf_order.index'));
  40. $response->assertStatus(403);
  41. }
  42. public function test_assistant_head_can_access_maf_orders_index(): void
  43. {
  44. $response = $this->actingAs($this->assistantHeadUser)
  45. ->get(route('maf_order.index'));
  46. $response->assertStatus(200);
  47. $response->assertViewIs('maf_orders.index');
  48. }
  49. // ==================== Index ====================
  50. public function test_maf_orders_index_displays_orders(): void
  51. {
  52. MafOrder::factory()->create();
  53. $response = $this->actingAs($this->adminUser)
  54. ->get(route('maf_order.index'));
  55. $response->assertStatus(200);
  56. }
  57. // ==================== Store (create) ====================
  58. public function test_can_create_maf_order(): void
  59. {
  60. $product = Product::factory()->create();
  61. $response = $this->actingAs($this->adminUser)
  62. ->post(route('maf_order.store'), [
  63. 'product_id' => $product->id,
  64. 'quantity' => 5,
  65. 'order_number' => 'MO-TEST-001',
  66. ]);
  67. $response->assertRedirect();
  68. $this->assertDatabaseHas('maf_orders', [
  69. 'product_id' => $product->id,
  70. 'quantity' => 5,
  71. 'order_number' => 'MO-TEST-001',
  72. 'in_stock' => 0,
  73. ]);
  74. }
  75. public function test_store_maf_order_requires_product_id(): void
  76. {
  77. $response = $this->actingAs($this->adminUser)
  78. ->post(route('maf_order.store'), [
  79. 'quantity' => 5,
  80. ]);
  81. $response->assertSessionHasErrors('product_id');
  82. }
  83. public function test_store_maf_order_requires_quantity(): void
  84. {
  85. $product = Product::factory()->create();
  86. $response = $this->actingAs($this->adminUser)
  87. ->post(route('maf_order.store'), [
  88. 'product_id' => $product->id,
  89. ]);
  90. $response->assertSessionHasErrors('quantity');
  91. }
  92. public function test_guest_cannot_create_maf_order(): void
  93. {
  94. $product = Product::factory()->create();
  95. $response = $this->post(route('maf_order.store'), [
  96. 'product_id' => $product->id,
  97. 'quantity' => 3,
  98. ]);
  99. $response->assertRedirect(route('login'));
  100. }
  101. // ==================== Show ====================
  102. public function test_can_view_maf_order_details(): void
  103. {
  104. $mafOrder = MafOrder::factory()->create();
  105. $response = $this->actingAs($this->adminUser)
  106. ->get(route('maf_order.show', $mafOrder));
  107. $response->assertStatus(200);
  108. $response->assertViewIs('maf_orders.edit');
  109. }
  110. public function test_guest_cannot_view_maf_order_details(): void
  111. {
  112. $mafOrder = MafOrder::factory()->create();
  113. $response = $this->get(route('maf_order.show', $mafOrder));
  114. $response->assertRedirect(route('login'));
  115. }
  116. // ==================== Update ====================
  117. public function test_can_update_maf_order(): void
  118. {
  119. $product = Product::factory()->create();
  120. $mafOrder = MafOrder::factory()->create([
  121. 'product_id' => $product->id,
  122. 'quantity' => 3,
  123. ]);
  124. $response = $this->actingAs($this->adminUser)
  125. ->post(route('maf_order.update', $mafOrder), [
  126. 'product_id' => $product->id,
  127. 'quantity' => 10,
  128. 'order_number' => 'MO-UPDATED',
  129. ]);
  130. $response->assertRedirect();
  131. $this->assertDatabaseHas('maf_orders', [
  132. 'id' => $mafOrder->id,
  133. 'quantity' => 10,
  134. ]);
  135. }
  136. public function test_guest_cannot_update_maf_order(): void
  137. {
  138. $product = Product::factory()->create();
  139. $mafOrder = MafOrder::factory()->create(['product_id' => $product->id]);
  140. $response = $this->post(route('maf_order.update', $mafOrder), [
  141. 'product_id' => $product->id,
  142. 'quantity' => 10,
  143. ]);
  144. $response->assertRedirect(route('login'));
  145. }
  146. // ==================== Destroy ====================
  147. public function test_can_delete_maf_order(): void
  148. {
  149. $mafOrder = MafOrder::factory()->create();
  150. $mafOrderId = $mafOrder->id;
  151. $response = $this->actingAs($this->adminUser)
  152. ->delete(route('maf_order.delete', $mafOrder));
  153. $response->assertRedirect();
  154. $this->assertSoftDeleted('maf_orders', ['id' => $mafOrderId]);
  155. }
  156. public function test_guest_cannot_delete_maf_order(): void
  157. {
  158. $mafOrder = MafOrder::factory()->create();
  159. $response = $this->delete(route('maf_order.delete', $mafOrder));
  160. $response->assertRedirect(route('login'));
  161. }
  162. // ==================== SetInStock ====================
  163. public function test_set_in_stock_updates_stock_and_status(): void
  164. {
  165. $mafOrder = MafOrder::factory()->create([
  166. 'quantity' => 8,
  167. 'in_stock' => 0,
  168. 'status' => 'active',
  169. ]);
  170. $response = $this->actingAs($this->adminUser)
  171. ->post(route('maf_order.set_in_stock', $mafOrder));
  172. $response->assertRedirect(route('maf_order.show', $mafOrder));
  173. $this->assertDatabaseHas('maf_orders', [
  174. 'id' => $mafOrder->id,
  175. 'in_stock' => 8,
  176. 'status' => 'на складе',
  177. ]);
  178. }
  179. public function test_guest_cannot_set_in_stock(): void
  180. {
  181. $mafOrder = MafOrder::factory()->create(['quantity' => 5]);
  182. $response = $this->post(route('maf_order.set_in_stock', $mafOrder));
  183. $response->assertRedirect(route('login'));
  184. }
  185. public function test_set_order_in_stock_updates_all_rows_by_order_number(): void
  186. {
  187. $first = MafOrder::factory()->create([
  188. 'order_number' => 'MO-BULK-001',
  189. 'quantity' => 3,
  190. 'in_stock' => 0,
  191. 'status' => 'заказан',
  192. ]);
  193. $second = MafOrder::factory()->create([
  194. 'order_number' => 'MO-BULK-001',
  195. 'quantity' => 7,
  196. 'in_stock' => 1,
  197. 'status' => 'заказан',
  198. ]);
  199. $other = MafOrder::factory()->create([
  200. 'order_number' => 'MO-BULK-002',
  201. 'quantity' => 9,
  202. 'in_stock' => 2,
  203. 'status' => 'заказан',
  204. ]);
  205. $response = $this->actingAs($this->adminUser)
  206. ->post(route('maf_order.set_order_in_stock'), [
  207. 'bulk_order_number' => 'MO-BULK-001',
  208. ]);
  209. $response->assertRedirect(route('maf_order.index'));
  210. $this->assertDatabaseHas('maf_orders', [
  211. 'id' => $first->id,
  212. 'in_stock' => 3,
  213. 'status' => 'на складе',
  214. ]);
  215. $this->assertDatabaseHas('maf_orders', [
  216. 'id' => $second->id,
  217. 'in_stock' => 7,
  218. 'status' => 'на складе',
  219. ]);
  220. $this->assertDatabaseHas('maf_orders', [
  221. 'id' => $other->id,
  222. 'in_stock' => 2,
  223. 'status' => 'заказан',
  224. ]);
  225. }
  226. public function test_guest_cannot_set_order_in_stock(): void
  227. {
  228. $response = $this->post(route('maf_order.set_order_in_stock'), [
  229. 'bulk_order_number' => 'MO-BULK-001',
  230. ]);
  231. $response->assertRedirect(route('login'));
  232. }
  233. }