ProductSKUControllerTest.php 22 KB

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