ProductSKUControllerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Jobs\ExportMafJob;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\ProductSKU;
  7. use App\Models\Role;
  8. use App\Models\User;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Facades\Bus;
  11. use Tests\TestCase;
  12. class ProductSKUControllerTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected $seed = true;
  16. private User $adminUser;
  17. private User $managerUser;
  18. private User $brigadierUser;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  23. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  24. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  25. }
  26. // ==================== Guest redirects ====================
  27. public function test_guest_cannot_access_product_sku_index(): void
  28. {
  29. $response = $this->get(route('product_sku.index'));
  30. $response->assertRedirect(route('login'));
  31. }
  32. public function test_guest_cannot_access_product_sku_show(): void
  33. {
  34. $sku = ProductSKU::factory()->create();
  35. $response = $this->get(route('product_sku.show', $sku));
  36. $response->assertRedirect(route('login'));
  37. }
  38. public function test_guest_cannot_update_product_sku(): void
  39. {
  40. $sku = ProductSKU::factory()->create();
  41. $response = $this->post(route('product_sku.update', $sku), []);
  42. $response->assertRedirect(route('login'));
  43. }
  44. // ==================== Authorization ====================
  45. public function test_brigadier_cannot_access_product_sku_index(): void
  46. {
  47. $response = $this->actingAs($this->brigadierUser)
  48. ->get(route('product_sku.index'));
  49. $response->assertStatus(403);
  50. }
  51. public function test_manager_cannot_export_mafs(): void
  52. {
  53. $response = $this->actingAs($this->managerUser)
  54. ->post(route('mafs.export'));
  55. $response->assertStatus(403);
  56. }
  57. // ==================== Index ====================
  58. public function test_admin_can_access_product_sku_index(): void
  59. {
  60. $response = $this->actingAs($this->adminUser)
  61. ->get(route('product_sku.index'));
  62. $response->assertStatus(200);
  63. $response->assertViewIs('products_sku.index');
  64. }
  65. public function test_manager_can_access_product_sku_index(): void
  66. {
  67. $response = $this->actingAs($this->managerUser)
  68. ->get(route('product_sku.index'));
  69. $response->assertStatus(200);
  70. $response->assertViewIs('products_sku.index');
  71. }
  72. public function test_maf_filter_options_include_empty_marker_for_factory_number(): void
  73. {
  74. ProductSKU::factory()->create(['factory_number' => null]);
  75. ProductSKU::factory()->create(['factory_number' => ' ']);
  76. ProductSKU::factory()->create(['factory_number' => 'FN-123456']);
  77. $response = $this->actingAs($this->adminUser)
  78. ->getJson(route('getFilters', [
  79. 'table' => 'product_sku',
  80. 'column' => 'factory_number',
  81. ]));
  82. $response->assertOk();
  83. $response->assertJsonFragment(['-пусто-']);
  84. $response->assertJsonFragment(['FN-123456']);
  85. }
  86. public function test_maf_empty_filter_matches_null_blank_and_null_date_values(): void
  87. {
  88. $orderWithNullFactory = Order::factory()->create(['object_address' => 'ул. Пустая фабрика']);
  89. $orderWithBlankFactory = Order::factory()->create(['object_address' => 'ул. Пробелы фабрика']);
  90. $orderWithNullDate = Order::factory()->create(['object_address' => 'ул. Пустая дата']);
  91. $orderWithBlankStatement = Order::factory()->create(['object_address' => 'ул. Пустая ведомость']);
  92. $orderWithFilledValue = Order::factory()->create(['object_address' => 'ул. Заполненная']);
  93. $product = Product::factory()->create();
  94. ProductSKU::factory()->create([
  95. 'order_id' => $orderWithNullFactory->id,
  96. 'product_id' => $product->id,
  97. 'factory_number' => null,
  98. 'manufacture_date' => '2026-04-01',
  99. 'statement_number' => 'STAT-1',
  100. ]);
  101. ProductSKU::factory()->create([
  102. 'order_id' => $orderWithBlankFactory->id,
  103. 'product_id' => $product->id,
  104. 'factory_number' => ' ',
  105. 'manufacture_date' => '2026-04-02',
  106. 'statement_number' => 'STAT-2',
  107. ]);
  108. ProductSKU::factory()->create([
  109. 'order_id' => $orderWithNullDate->id,
  110. 'product_id' => $product->id,
  111. 'factory_number' => 'FN-000001',
  112. 'manufacture_date' => null,
  113. 'statement_number' => 'STAT-3',
  114. ]);
  115. ProductSKU::factory()->create([
  116. 'order_id' => $orderWithBlankStatement->id,
  117. 'product_id' => $product->id,
  118. 'factory_number' => 'FN-000002',
  119. 'manufacture_date' => '2026-04-03',
  120. 'statement_number' => ' ',
  121. ]);
  122. ProductSKU::factory()->create([
  123. 'order_id' => $orderWithFilledValue->id,
  124. 'product_id' => $product->id,
  125. 'factory_number' => 'FN-999999',
  126. 'manufacture_date' => '2026-04-04',
  127. 'statement_number' => 'STAT-9',
  128. ]);
  129. $factoryResponse = $this->actingAs($this->adminUser)
  130. ->get(route('product_sku.index', [
  131. 'filters' => ['factory_number' => '-пусто-'],
  132. ]));
  133. $factoryResponse->assertOk();
  134. $factoryAddresses = $factoryResponse->viewData('products_sku')->pluck('object_address')->all();
  135. $this->assertContains('ул. Пустая фабрика', $factoryAddresses);
  136. $this->assertContains('ул. Пробелы фабрика', $factoryAddresses);
  137. $this->assertNotContains('ул. Заполненная', $factoryAddresses);
  138. $dateResponse = $this->actingAs($this->adminUser)
  139. ->get(route('product_sku.index', [
  140. 'filters' => ['manufacture_date' => '-пусто-'],
  141. ]));
  142. $dateResponse->assertOk();
  143. $dateAddresses = $dateResponse->viewData('products_sku')->pluck('object_address')->all();
  144. $this->assertContains('ул. Пустая дата', $dateAddresses);
  145. $this->assertNotContains('ул. Заполненная', $dateAddresses);
  146. $statementResponse = $this->actingAs($this->adminUser)
  147. ->get(route('product_sku.index', [
  148. 'filters' => ['statement_number' => '-пусто-'],
  149. ]));
  150. $statementResponse->assertOk();
  151. $statementAddresses = $statementResponse->viewData('products_sku')->pluck('object_address')->all();
  152. $this->assertContains('ул. Пустая ведомость', $statementAddresses);
  153. $this->assertNotContains('ул. Заполненная', $statementAddresses);
  154. }
  155. // ==================== Show ====================
  156. public function test_admin_can_view_product_sku(): void
  157. {
  158. $sku = ProductSKU::factory()->create();
  159. $response = $this->actingAs($this->adminUser)
  160. ->get(route('product_sku.show', $sku));
  161. $response->assertStatus(200);
  162. $response->assertViewIs('products_sku.edit');
  163. }
  164. public function test_manager_can_view_product_sku(): void
  165. {
  166. $sku = ProductSKU::factory()->create();
  167. $response = $this->actingAs($this->managerUser)
  168. ->get(route('product_sku.show', $sku));
  169. $response->assertStatus(200);
  170. $response->assertViewIs('products_sku.edit');
  171. }
  172. public function test_product_sku_show_uses_nav_context_back_url(): void
  173. {
  174. $sku = ProductSKU::factory()->create();
  175. $reclamation = \App\Models\Reclamation::factory()->create();
  176. $indexResponse = $this->actingAs($this->adminUser)
  177. ->get(route('reclamations.index'));
  178. $nav = $indexResponse->viewData('nav');
  179. $this->actingAs($this->adminUser)
  180. ->get(route('reclamations.show', [
  181. 'reclamation' => $reclamation,
  182. 'nav' => $nav,
  183. ]))
  184. ->assertOk();
  185. $response = $this->actingAs($this->adminUser)
  186. ->get(route('product_sku.show', [
  187. 'product_sku' => $sku,
  188. 'nav' => $nav,
  189. ]));
  190. $response->assertOk();
  191. $response->assertViewHas('nav', $nav);
  192. $response->assertViewHas('back_url', route('reclamations.show', [
  193. 'reclamation' => $reclamation,
  194. ]));
  195. }
  196. // ==================== Update ====================
  197. public function test_admin_can_update_product_sku(): void
  198. {
  199. $product = Product::factory()->create();
  200. $order = Order::factory()->create();
  201. $sku = ProductSKU::factory()->create([
  202. 'product_id' => $product->id,
  203. 'order_id' => $order->id,
  204. ]);
  205. $response = $this->actingAs($this->adminUser)
  206. ->post(route('product_sku.update', $sku), [
  207. 'product_id' => $product->id,
  208. 'order_id' => $order->id,
  209. 'status' => 'shipped',
  210. 'factory_number' => 'FN-999999',
  211. 'comment' => 'Updated by admin',
  212. ]);
  213. $response->assertRedirect();
  214. $this->assertDatabaseHas('products_sku', [
  215. 'id' => $sku->id,
  216. 'factory_number' => 'FN-999999',
  217. 'comment' => 'Updated by admin',
  218. ]);
  219. }
  220. public function test_manager_can_update_product_sku(): void
  221. {
  222. $product = Product::factory()->create();
  223. $order = Order::factory()->create();
  224. $sku = ProductSKU::factory()->create([
  225. 'product_id' => $product->id,
  226. 'order_id' => $order->id,
  227. ]);
  228. $response = $this->actingAs($this->managerUser)
  229. ->post(route('product_sku.update', $sku), [
  230. 'product_id' => $product->id,
  231. 'order_id' => $order->id,
  232. 'status' => 'needs',
  233. 'factory_number' => 'FN-777777',
  234. ]);
  235. $response->assertRedirect();
  236. }
  237. public function test_update_product_sku_redirects_to_parent_url_from_nav_context(): void
  238. {
  239. $product = Product::factory()->create();
  240. $order = Order::factory()->create();
  241. $sku = ProductSKU::factory()->create([
  242. 'product_id' => $product->id,
  243. 'order_id' => $order->id,
  244. ]);
  245. $reclamation = \App\Models\Reclamation::factory()->create();
  246. $nav = 'product-sku-nav-token';
  247. $response = $this->actingAs($this->adminUser)
  248. ->withSession([
  249. 'navigation' => [
  250. $nav => [
  251. 'updated_at' => time(),
  252. 'stack' => [
  253. route('reclamations.index'),
  254. route('reclamations.show', $reclamation),
  255. route('product_sku.show', $sku),
  256. ],
  257. ],
  258. ],
  259. ])
  260. ->post(route('product_sku.update', $sku), [
  261. 'nav' => $nav,
  262. 'product_id' => $product->id,
  263. 'order_id' => $order->id,
  264. 'status' => 'shipped',
  265. 'factory_number' => 'FN-NAV-123',
  266. 'comment' => 'Updated through nav',
  267. ]);
  268. $response->assertRedirect(route('reclamations.show', [
  269. 'reclamation' => $reclamation,
  270. ]));
  271. }
  272. // ==================== Export ====================
  273. public function test_admin_can_export_mafs(): void
  274. {
  275. Bus::fake();
  276. $response = $this->actingAs($this->adminUser)
  277. ->post(route('mafs.export'));
  278. $response->assertRedirect(route('product_sku.index'));
  279. $response->assertSessionHas('success');
  280. Bus::assertDispatched(ExportMafJob::class);
  281. }
  282. }