CatalogFieldAccessTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Permission;
  4. use App\Models\Product;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Database\Seeders\RbacSeeder;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Tests\TestCase;
  10. class CatalogFieldAccessTest extends TestCase
  11. {
  12. use RefreshDatabase;
  13. protected $seed = true;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->seed(RbacSeeder::class);
  18. }
  19. public function test_catalog_index_hides_denied_field_columns(): void
  20. {
  21. $role = $this->makeRoleWithPermissions([
  22. 'catalog.view' => 'allow',
  23. 'catalog.fields.article.view' => 'allow',
  24. 'catalog.fields.nomenclature_number.view' => 'allow',
  25. 'catalog.fields.name_tz.view' => 'allow',
  26. 'catalog.fields.product_price.view' => 'deny',
  27. 'catalog.fields.product_price.update' => 'deny',
  28. ]);
  29. $user = User::factory()->create(['role' => $role->slug, 'role_id' => $role->id]);
  30. Product::factory()->create([
  31. 'article' => 'ACL-001',
  32. 'name_tz' => 'Открытое название',
  33. 'product_price' => 12345,
  34. ]);
  35. $this->actingAs($user)
  36. ->get(route('catalog.index'))
  37. ->assertOk()
  38. ->assertSee('Открытое название')
  39. ->assertDontSee('Цена товара')
  40. ->assertDontSee('12 345', false);
  41. }
  42. public function test_catalog_update_strips_denied_fields_from_payload(): void
  43. {
  44. $role = $this->makeRoleWithPermissions([
  45. 'catalog.view' => 'allow',
  46. 'catalog.update' => 'allow',
  47. 'catalog.fields.name_tz.view' => 'allow',
  48. 'catalog.fields.name_tz.update' => 'allow',
  49. 'catalog.fields.product_price.view' => 'allow',
  50. 'catalog.fields.product_price.update' => 'deny',
  51. ]);
  52. $user = User::factory()->create(['role' => $role->slug, 'role_id' => $role->id]);
  53. $product = Product::factory()->create([
  54. 'name_tz' => 'Старое название',
  55. 'product_price' => 100,
  56. ]);
  57. $this->actingAs($user)
  58. ->post(route('catalog.update', $product), [
  59. 'name_tz' => 'Новое название',
  60. 'product_price' => 999999,
  61. ])
  62. ->assertRedirect();
  63. $product->refresh();
  64. $this->assertSame('Новое название', $product->name_tz);
  65. $this->assertSame(100.0, $product->product_price);
  66. }
  67. private function makeRoleWithPermissions(array $effects): Role
  68. {
  69. $role = Role::query()->create([
  70. 'slug' => 'catalog_acl_' . uniqid(),
  71. 'name' => 'Catalog ACL',
  72. 'is_system' => false,
  73. 'is_active' => true,
  74. ]);
  75. $permissions = Permission::query()
  76. ->whereIn('slug', array_keys($effects))
  77. ->get();
  78. $sync = [];
  79. foreach ($permissions as $permission) {
  80. $sync[$permission->id] = ['effect' => $effects[$permission->slug]];
  81. }
  82. $role->permissions()->sync($sync);
  83. return $role;
  84. }
  85. }