ProductSKUControllerTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Jobs\ExportMafJob;
  4. use App\Jobs\ExportMafRegistryJob;
  5. use App\Models\Order;
  6. use App\Models\Permission;
  7. use App\Models\Product;
  8. use App\Models\ProductSKU;
  9. use App\Models\Role;
  10. use App\Models\User;
  11. use Database\Seeders\RbacSeeder;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Illuminate\Http\UploadedFile;
  14. use Illuminate\Support\Facades\Bus;
  15. use Illuminate\Support\Facades\Storage;
  16. use Tests\TestCase;
  17. class ProductSKUControllerTest extends TestCase
  18. {
  19. use RefreshDatabase;
  20. protected $seed = true;
  21. private User $adminUser;
  22. private User $managerUser;
  23. private User $brigadierUser;
  24. private User $assistantHeadUser;
  25. protected function setUp(): void
  26. {
  27. parent::setUp();
  28. $this->seed(RbacSeeder::class);
  29. $this->adminUser = $this->createUserWithRole(Role::ADMIN);
  30. $this->managerUser = $this->createUserWithRole(Role::MANAGER);
  31. $this->brigadierUser = $this->createUserWithRole(Role::BRIGADIER);
  32. $this->assistantHeadUser = $this->createUserWithRole(Role::ASSISTANT_HEAD);
  33. }
  34. private function createUserWithRole(string $roleSlug): User
  35. {
  36. return User::factory()->create([
  37. 'role' => $roleSlug,
  38. 'role_id' => Role::query()->where('slug', $roleSlug)->value('id'),
  39. ]);
  40. }
  41. // ==================== Guest redirects ====================
  42. public function test_guest_cannot_access_product_sku_index(): void
  43. {
  44. $response = $this->get(route('product_sku.index'));
  45. $response->assertRedirect(route('login'));
  46. }
  47. public function test_guest_cannot_access_product_sku_show(): void
  48. {
  49. $sku = ProductSKU::factory()->create();
  50. $response = $this->get(route('product_sku.show', $sku));
  51. $response->assertRedirect(route('login'));
  52. }
  53. public function test_guest_cannot_update_product_sku(): void
  54. {
  55. $sku = ProductSKU::factory()->create();
  56. $response = $this->post(route('product_sku.update', $sku), []);
  57. $response->assertRedirect(route('login'));
  58. }
  59. // ==================== Authorization ====================
  60. public function test_brigadier_cannot_access_product_sku_index(): void
  61. {
  62. $response = $this->actingAs($this->brigadierUser)
  63. ->get(route('product_sku.index'));
  64. $response->assertStatus(403);
  65. }
  66. public function test_manager_cannot_export_mafs(): void
  67. {
  68. $response = $this->actingAs($this->managerUser)
  69. ->post(route('mafs.export'));
  70. $response->assertStatus(403);
  71. }
  72. public function test_manager_cannot_export_maf_registry(): void
  73. {
  74. $response = $this->actingAs($this->managerUser)
  75. ->post(route('mafs.registry'), ['upd_number' => 'UPD-001']);
  76. $response->assertStatus(403);
  77. }
  78. // ==================== Index ====================
  79. public function test_admin_can_access_product_sku_index(): void
  80. {
  81. $response = $this->actingAs($this->adminUser)
  82. ->get(route('product_sku.index'));
  83. $response->assertStatus(200);
  84. $response->assertViewIs('products_sku.index');
  85. }
  86. public function test_manager_can_access_product_sku_index(): void
  87. {
  88. $response = $this->actingAs($this->managerUser)
  89. ->get(route('product_sku.index'));
  90. $response->assertStatus(200);
  91. $response->assertViewIs('products_sku.index');
  92. }
  93. public function test_maf_filter_options_include_empty_marker_for_factory_number(): void
  94. {
  95. ProductSKU::factory()->create(['factory_number' => null]);
  96. ProductSKU::factory()->create(['factory_number' => ' ']);
  97. ProductSKU::factory()->create(['factory_number' => 'FN-123456']);
  98. $response = $this->actingAs($this->adminUser)
  99. ->getJson(route('getFilters', [
  100. 'table' => 'product_sku',
  101. 'column' => 'factory_number',
  102. ]));
  103. $response->assertOk();
  104. $response->assertJsonFragment(['-пусто-']);
  105. $response->assertJsonFragment(['FN-123456']);
  106. }
  107. public function test_maf_empty_filter_matches_null_blank_and_null_date_values(): void
  108. {
  109. $orderWithNullFactory = Order::factory()->create(['object_address' => 'ул. Пустая фабрика']);
  110. $orderWithBlankFactory = Order::factory()->create(['object_address' => 'ул. Пробелы фабрика']);
  111. $orderWithNullDate = Order::factory()->create(['object_address' => 'ул. Пустая дата']);
  112. $orderWithBlankStatement = Order::factory()->create(['object_address' => 'ул. Пустая ведомость']);
  113. $orderWithFilledValue = Order::factory()->create(['object_address' => 'ул. Заполненная']);
  114. $product = Product::factory()->create();
  115. ProductSKU::factory()->create([
  116. 'order_id' => $orderWithNullFactory->id,
  117. 'product_id' => $product->id,
  118. 'factory_number' => null,
  119. 'manufacture_date' => '2026-04-01',
  120. 'statement_number' => 'STAT-1',
  121. ]);
  122. ProductSKU::factory()->create([
  123. 'order_id' => $orderWithBlankFactory->id,
  124. 'product_id' => $product->id,
  125. 'factory_number' => ' ',
  126. 'manufacture_date' => '2026-04-02',
  127. 'statement_number' => 'STAT-2',
  128. ]);
  129. ProductSKU::factory()->create([
  130. 'order_id' => $orderWithNullDate->id,
  131. 'product_id' => $product->id,
  132. 'factory_number' => 'FN-000001',
  133. 'manufacture_date' => null,
  134. 'statement_number' => 'STAT-3',
  135. ]);
  136. ProductSKU::factory()->create([
  137. 'order_id' => $orderWithBlankStatement->id,
  138. 'product_id' => $product->id,
  139. 'factory_number' => 'FN-000002',
  140. 'manufacture_date' => '2026-04-03',
  141. 'statement_number' => ' ',
  142. ]);
  143. ProductSKU::factory()->create([
  144. 'order_id' => $orderWithFilledValue->id,
  145. 'product_id' => $product->id,
  146. 'factory_number' => 'FN-999999',
  147. 'manufacture_date' => '2026-04-04',
  148. 'statement_number' => 'STAT-9',
  149. ]);
  150. $factoryResponse = $this->actingAs($this->adminUser)
  151. ->get(route('product_sku.index', [
  152. 'filters' => ['factory_number' => '-пусто-'],
  153. ]));
  154. $factoryResponse->assertOk();
  155. $factoryAddresses = $factoryResponse->viewData('products_sku')->pluck('object_address')->all();
  156. $this->assertContains('ул. Пустая фабрика', $factoryAddresses);
  157. $this->assertContains('ул. Пробелы фабрика', $factoryAddresses);
  158. $this->assertNotContains('ул. Заполненная', $factoryAddresses);
  159. $dateResponse = $this->actingAs($this->adminUser)
  160. ->get(route('product_sku.index', [
  161. 'filters' => ['manufacture_date' => '-пусто-'],
  162. ]));
  163. $dateResponse->assertOk();
  164. $dateAddresses = $dateResponse->viewData('products_sku')->pluck('object_address')->all();
  165. $this->assertContains('ул. Пустая дата', $dateAddresses);
  166. $this->assertNotContains('ул. Заполненная', $dateAddresses);
  167. $statementResponse = $this->actingAs($this->adminUser)
  168. ->get(route('product_sku.index', [
  169. 'filters' => ['statement_number' => '-пусто-'],
  170. ]));
  171. $statementResponse->assertOk();
  172. $statementAddresses = $statementResponse->viewData('products_sku')->pluck('object_address')->all();
  173. $this->assertContains('ул. Пустая ведомость', $statementAddresses);
  174. $this->assertNotContains('ул. Заполненная', $statementAddresses);
  175. }
  176. // ==================== Show ====================
  177. public function test_admin_can_view_product_sku(): void
  178. {
  179. $sku = ProductSKU::factory()->create();
  180. $response = $this->actingAs($this->adminUser)
  181. ->get(route('product_sku.show', $sku));
  182. $response->assertStatus(200);
  183. $response->assertViewIs('products_sku.edit');
  184. }
  185. public function test_manager_can_view_product_sku(): void
  186. {
  187. $sku = ProductSKU::factory()->create();
  188. $response = $this->actingAs($this->managerUser)
  189. ->get(route('product_sku.show', $sku));
  190. $response->assertStatus(200);
  191. $response->assertViewIs('products_sku.edit');
  192. }
  193. public function test_product_sku_show_uses_nav_context_back_url(): void
  194. {
  195. $sku = ProductSKU::factory()->create();
  196. $reclamation = \App\Models\Reclamation::factory()->create();
  197. $indexResponse = $this->actingAs($this->adminUser)
  198. ->get(route('reclamations.index'));
  199. $nav = $indexResponse->viewData('nav');
  200. $this->actingAs($this->adminUser)
  201. ->get(route('reclamations.show', [
  202. 'reclamation' => $reclamation,
  203. 'nav' => $nav,
  204. ]))
  205. ->assertOk();
  206. $response = $this->actingAs($this->adminUser)
  207. ->get(route('product_sku.show', [
  208. 'product_sku' => $sku,
  209. 'nav' => $nav,
  210. ]));
  211. $response->assertOk();
  212. $response->assertViewHas('nav', $nav);
  213. $response->assertViewHas('back_url', route('reclamations.show', [
  214. 'reclamation' => $reclamation,
  215. 'nav' => $nav,
  216. ]));
  217. }
  218. // ==================== Update ====================
  219. public function test_admin_can_update_product_sku(): void
  220. {
  221. $product = Product::factory()->create();
  222. $order = Order::factory()->create();
  223. $sku = ProductSKU::factory()->create([
  224. 'product_id' => $product->id,
  225. 'order_id' => $order->id,
  226. ]);
  227. $response = $this->actingAs($this->adminUser)
  228. ->post(route('product_sku.update', $sku), [
  229. 'product_id' => $product->id,
  230. 'order_id' => $order->id,
  231. 'status' => 'shipped',
  232. 'factory_number' => 'FN-999999',
  233. 'comment' => 'Updated by admin',
  234. ]);
  235. $response->assertRedirect();
  236. $this->assertDatabaseHas('products_sku', [
  237. 'id' => $sku->id,
  238. 'factory_number' => 'FN-999999',
  239. 'comment' => 'Updated by admin',
  240. ]);
  241. }
  242. public function test_manager_can_update_product_sku(): void
  243. {
  244. $product = Product::factory()->create();
  245. $order = Order::factory()->create();
  246. $sku = ProductSKU::factory()->create([
  247. 'product_id' => $product->id,
  248. 'order_id' => $order->id,
  249. ]);
  250. $response = $this->actingAs($this->managerUser)
  251. ->post(route('product_sku.update', $sku), [
  252. 'product_id' => $product->id,
  253. 'order_id' => $order->id,
  254. 'status' => 'needs',
  255. 'factory_number' => 'FN-777777',
  256. ]);
  257. $response->assertRedirect();
  258. }
  259. public function test_update_product_sku_redirects_to_parent_url_from_nav_context(): void
  260. {
  261. $product = Product::factory()->create();
  262. $order = Order::factory()->create();
  263. $sku = ProductSKU::factory()->create([
  264. 'product_id' => $product->id,
  265. 'order_id' => $order->id,
  266. ]);
  267. $reclamation = \App\Models\Reclamation::factory()->create();
  268. $nav = 'product-sku-nav-token';
  269. $response = $this->actingAs($this->adminUser)
  270. ->withSession([
  271. 'navigation' => [
  272. $nav => [
  273. 'updated_at' => time(),
  274. 'stack' => [
  275. route('reclamations.index'),
  276. route('reclamations.show', $reclamation),
  277. route('product_sku.show', $sku),
  278. ],
  279. ],
  280. ],
  281. ])
  282. ->post(route('product_sku.update', $sku), [
  283. 'nav' => $nav,
  284. 'product_id' => $product->id,
  285. 'order_id' => $order->id,
  286. 'status' => 'shipped',
  287. 'factory_number' => 'FN-NAV-123',
  288. 'comment' => 'Updated through nav',
  289. ]);
  290. $response->assertRedirect(route('reclamations.show', [
  291. 'reclamation' => $reclamation,
  292. 'nav' => 'product-sku-nav-token',
  293. ]));
  294. }
  295. public function test_admin_can_inline_update_product_sku_field(): void
  296. {
  297. $sku = ProductSKU::factory()->create(['rfid' => 'OLD-RFID']);
  298. $response = $this->actingAs($this->adminUser)
  299. ->postJson(route('product_sku.inline-update', $sku), [
  300. 'field' => 'rfid',
  301. 'value' => 'NEW-RFID',
  302. ]);
  303. $response->assertOk()
  304. ->assertJson([
  305. 'field' => 'rfid',
  306. 'value' => 'NEW-RFID',
  307. ]);
  308. $this->assertDatabaseHas('products_sku', [
  309. 'id' => $sku->id,
  310. 'rfid' => 'NEW-RFID',
  311. ]);
  312. }
  313. public function test_assistant_head_can_inline_update_product_sku_date_field(): void
  314. {
  315. $sku = ProductSKU::factory()->create(['manufacture_date' => null]);
  316. $response = $this->actingAs($this->assistantHeadUser)
  317. ->postJson(route('product_sku.inline-update', $sku), [
  318. 'field' => 'manufacture_date',
  319. 'value' => '2026-05-19',
  320. ]);
  321. $response->assertOk()
  322. ->assertJson([
  323. 'field' => 'manufacture_date',
  324. 'value' => '2026-05-19',
  325. ]);
  326. $this->assertDatabaseHas('products_sku', [
  327. 'id' => $sku->id,
  328. 'manufacture_date' => '2026-05-19',
  329. ]);
  330. }
  331. public function test_user_with_maf_field_update_permission_can_inline_update_product_sku_field(): void
  332. {
  333. $role = Role::query()->create([
  334. 'slug' => 'maf_inline_editor',
  335. 'name' => 'Редактор МАФ в таблице',
  336. 'is_system' => false,
  337. 'is_active' => true,
  338. 'sort' => 100,
  339. ]);
  340. $permission = Permission::query()->where('slug', 'maf.fields.statement_number.update')->firstOrFail();
  341. $role->permissions()->sync([
  342. $permission->id => ['effect' => 'allow'],
  343. ]);
  344. $user = User::factory()->create([
  345. 'role' => $role->slug,
  346. 'role_id' => $role->id,
  347. ]);
  348. $sku = ProductSKU::factory()->create(['statement_number' => 'OLD-STAT']);
  349. $response = $this->actingAs($user)
  350. ->postJson(route('product_sku.inline-update', $sku), [
  351. 'field' => 'statement_number',
  352. 'value' => 'NEW-STAT',
  353. ]);
  354. $response->assertOk()
  355. ->assertJson([
  356. 'field' => 'statement_number',
  357. 'value' => 'NEW-STAT',
  358. ]);
  359. $this->assertDatabaseHas('products_sku', [
  360. 'id' => $sku->id,
  361. 'statement_number' => 'NEW-STAT',
  362. ]);
  363. }
  364. public function test_manager_cannot_inline_update_product_sku_field(): void
  365. {
  366. $sku = ProductSKU::factory()->create(['upd_number' => 'UPD-OLD']);
  367. $response = $this->actingAs($this->managerUser)
  368. ->postJson(route('product_sku.inline-update', $sku), [
  369. 'field' => 'upd_number',
  370. 'value' => 'UPD-NEW',
  371. ]);
  372. $response->assertForbidden();
  373. $this->assertDatabaseHas('products_sku', [
  374. 'id' => $sku->id,
  375. 'upd_number' => 'UPD-OLD',
  376. ]);
  377. }
  378. public function test_inline_update_rejects_unapproved_product_sku_field(): void
  379. {
  380. $sku = ProductSKU::factory()->create(['comment' => 'Old comment']);
  381. $response = $this->actingAs($this->adminUser)
  382. ->postJson(route('product_sku.inline-update', $sku), [
  383. 'field' => 'comment',
  384. 'value' => 'New comment',
  385. ]);
  386. $response->assertUnprocessable();
  387. $this->assertDatabaseHas('products_sku', [
  388. 'id' => $sku->id,
  389. 'comment' => 'Old comment',
  390. ]);
  391. }
  392. public function test_user_with_passport_upload_permission_can_upload_product_sku_passport(): void
  393. {
  394. Storage::fake('public');
  395. $role = Role::query()->create([
  396. 'slug' => 'maf_passport_loader',
  397. 'name' => 'Загрузка паспортов МАФ',
  398. 'is_system' => false,
  399. 'is_active' => true,
  400. 'sort' => 100,
  401. ]);
  402. $permission = Permission::query()->where('slug', 'maf.passports.upload')->firstOrFail();
  403. $role->permissions()->sync([
  404. $permission->id => ['effect' => 'allow'],
  405. ]);
  406. $user = User::factory()->create([
  407. 'role' => $role->slug,
  408. 'role_id' => $role->id,
  409. ]);
  410. $sku = ProductSKU::factory()->create(['passport_id' => null]);
  411. $response = $this->actingAs($user)
  412. ->from(route('order.show', $sku->order))
  413. ->post(route('product-sku.upload-passport', $sku), [
  414. 'passport' => UploadedFile::fake()->create('passport.pdf', 10, 'application/pdf'),
  415. ]);
  416. $response->assertRedirect(route('order.show', $sku->order));
  417. $this->assertNotNull($sku->fresh()->passport_id);
  418. }
  419. public function test_user_without_passport_upload_permission_cannot_upload_product_sku_passport(): void
  420. {
  421. Storage::fake('public');
  422. $sku = ProductSKU::factory()->create(['passport_id' => null]);
  423. $response = $this->actingAs($this->brigadierUser)
  424. ->post(route('product-sku.upload-passport', $sku), [
  425. 'passport' => UploadedFile::fake()->create('passport.pdf', 10, 'application/pdf'),
  426. ]);
  427. $response->assertForbidden();
  428. $this->assertNull($sku->fresh()->passport_id);
  429. }
  430. // ==================== Export ====================
  431. public function test_admin_can_export_mafs(): void
  432. {
  433. Bus::fake();
  434. $response = $this->actingAs($this->adminUser)
  435. ->post(route('mafs.export'));
  436. $response->assertRedirect(route('product_sku.index'));
  437. $response->assertSessionHas('success');
  438. Bus::assertDispatched(ExportMafJob::class);
  439. }
  440. public function test_admin_can_export_maf_registry(): void
  441. {
  442. Bus::fake();
  443. $response = $this->actingAs($this->adminUser)
  444. ->post(route('mafs.registry'), ['upd_number' => 'UPD-001']);
  445. $response->assertRedirect(route('product_sku.index'));
  446. $response->assertSessionHas('success');
  447. Bus::assertDispatched(ExportMafRegistryJob::class);
  448. }
  449. public function test_assistant_head_can_export_maf_registry(): void
  450. {
  451. Bus::fake();
  452. $response = $this->actingAs($this->assistantHeadUser)
  453. ->post(route('mafs.registry'), ['upd_number' => 'UPD-002']);
  454. $response->assertRedirect(route('product_sku.index'));
  455. $response->assertSessionHas('success');
  456. Bus::assertDispatched(ExportMafRegistryJob::class);
  457. }
  458. }