ManagerMenuTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Permission;
  4. use App\Models\PricingCode;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\TestCase;
  9. class ManagerMenuTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. protected $seed = true;
  13. public function test_admin_sees_existing_sections_in_new_menu_groups(): void
  14. {
  15. $user = $this->createSystemRoleUser(Role::ADMIN);
  16. $response = $this->actingAs($user)->get(route('common-catalog.index'));
  17. $response->assertOk()
  18. ->assertSeeText('ДКР')
  19. ->assertSee(route('order.index'), false)
  20. ->assertSee(route('product_sku.index'), false)
  21. ->assertSee(route('catalog.index'), false)
  22. ->assertSee(route('reports.index'), false)
  23. ->assertSee(route('responsible.index'), false)
  24. ->assertSee(route('maf_order.index'), false)
  25. ->assertSeeText('Графики')
  26. ->assertSee(route('schedule.index'), false)
  27. ->assertSeeText('Запчасти')
  28. ->assertSee(route('spare_parts.index'), false)
  29. ->assertSee(route('spare_part_orders.index'), false)
  30. ->assertSee(route('spare_part_inventory.index'), false)
  31. ->assertSee(route('pricing_codes.index'), false)
  32. ->assertSeeText('Администратор')
  33. ->assertSee(route('contractors.index'), false)
  34. ->assertSee(route('contract.index'), false)
  35. ->assertSee(route('user.index'), false)
  36. ->assertSee(route('admin.roles.index'), false)
  37. ->assertSee(route('admin.settings.index'), false)
  38. ->assertSee(route('admin.district.index'), false)
  39. ->assertSee(route('admin.area.index'), false)
  40. ->assertSee(route('admin.notifications.log'), false)
  41. ->assertSee(route('import.index'), false)
  42. ->assertSee(route('year-data.index'), false)
  43. ->assertSee(route('clear-data.index'), false)
  44. ->assertDontSeeText('Администрирование');
  45. $this->assertSame(1, substr_count($response->getContent(), '>Подрядчики</a>'));
  46. }
  47. public function test_contractor_viewer_sees_contractors_only_inside_admin_group(): void
  48. {
  49. $user = $this->createUserWithPermissions('contractor_viewer', ['contractors.view']);
  50. $response = $this->actingAs($user)->get(route('common-catalog.index'));
  51. $response->assertOk()
  52. ->assertSeeText('Администратор')
  53. ->assertSee(route('contractors.index'), false);
  54. $this->assertSame(1, substr_count($response->getContent(), '>Подрядчики</a>'));
  55. }
  56. public function test_spare_parts_viewer_can_read_pricing_codes_without_edit_controls(): void
  57. {
  58. $user = $this->createUserWithPermissions('spare_parts_reader', ['spare_parts.view']);
  59. $pricingCode = PricingCode::query()->create([
  60. 'type' => PricingCode::TYPE_PRICING_CODE,
  61. 'code' => 'TEST-01',
  62. 'description' => 'Тестовая расшифровка',
  63. ]);
  64. $response = $this->actingAs($user)->get(route('pricing_codes.index'));
  65. $response->assertOk()
  66. ->assertSeeText('Справочник расшифровок')
  67. ->assertSeeText('TEST-01')
  68. ->assertSeeText('Тестовая расшифровка')
  69. ->assertSee(route('spare_parts.index'), false)
  70. ->assertSee(route('spare_parts.help'), false)
  71. ->assertDontSee(route('spare_part_orders.index'), false)
  72. ->assertDontSee(route('spare_part_inventory.index'), false)
  73. ->assertDontSee('data-bs-target="#addCodeModal"', false)
  74. ->assertDontSee('action="'.route('pricing_codes.update', $pricingCode).'"', false)
  75. ->assertDontSee('action="'.route('pricing_codes.destroy', $pricingCode).'"', false)
  76. ->assertDontSee('data-confirm-message=', false);
  77. $this->actingAs($user)
  78. ->post(route('pricing_codes.store'), [
  79. 'type' => PricingCode::TYPE_PRICING_CODE,
  80. 'code' => 'TEST-02',
  81. ])
  82. ->assertForbidden();
  83. }
  84. public function test_spare_parts_subsections_set_their_own_active_menu_item(): void
  85. {
  86. $user = $this->createSystemRoleUser(Role::ADMIN);
  87. $this->actingAs($user)
  88. ->get(route('spare_part_orders.index'))
  89. ->assertOk()
  90. ->assertViewHas('active', 'spare_part_orders')
  91. ->assertSeeText('Заказы запчастей');
  92. $this->actingAs($user)
  93. ->get(route('spare_part_inventory.index'))
  94. ->assertOk()
  95. ->assertViewHas('active', 'spare_part_inventory');
  96. $this->actingAs($user)
  97. ->get(route('pricing_codes.index'))
  98. ->assertOk()
  99. ->assertViewHas('active', 'pricing_codes');
  100. $this->actingAs($user)
  101. ->get(route('spare_parts.help'))
  102. ->assertOk()
  103. ->assertViewHas('active', 'spare_parts_help');
  104. }
  105. private function createSystemRoleUser(string $slug): User
  106. {
  107. $role = Role::query()->where('slug', $slug)->firstOrFail();
  108. return User::factory()->create([
  109. 'role' => $role->slug,
  110. 'role_id' => $role->id,
  111. ]);
  112. }
  113. private function createUserWithPermissions(string $slug, array $permissionSlugs): User
  114. {
  115. $role = Role::query()->create([
  116. 'slug' => $slug,
  117. 'name' => $slug,
  118. 'is_system' => false,
  119. 'is_active' => true,
  120. ]);
  121. $permissions = Permission::query()
  122. ->whereIn('slug', $permissionSlugs)
  123. ->pluck('id')
  124. ->mapWithKeys(fn (int $id): array => [$id => ['effect' => 'allow']]);
  125. $role->permissions()->sync($permissions);
  126. return User::factory()->create([
  127. 'role' => $role->slug,
  128. 'role_id' => $role->id,
  129. ]);
  130. }
  131. }