CatalogFieldAccessTest.php 3.1 KB

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