RolesHelperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Tests\Unit\Helpers;
  3. use App\Models\Role;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Tests\TestCase;
  6. class RolesHelperTest extends TestCase
  7. {
  8. use RefreshDatabase;
  9. protected $seed = true;
  10. public function test_get_roles_returns_all_roles(): void
  11. {
  12. $roles = getRoles();
  13. $this->assertIsArray($roles);
  14. $this->assertArrayHasKey(Role::ADMIN, $roles);
  15. $this->assertArrayHasKey(Role::MANAGER, $roles);
  16. $this->assertArrayHasKey(Role::BRIGADIER, $roles);
  17. $this->assertArrayHasKey(Role::WAREHOUSE_HEAD, $roles);
  18. $this->assertArrayHasKey(Role::ASSISTANT_HEAD, $roles);
  19. }
  20. public function test_get_roles_returns_name_by_key(): void
  21. {
  22. $this->assertEquals('Админ', getRoles(Role::ADMIN));
  23. $this->assertEquals('Менеджер', getRoles(Role::MANAGER));
  24. $this->assertEquals('Бригадир', getRoles(Role::BRIGADIER));
  25. $this->assertEquals('Рук. Склада', getRoles(Role::WAREHOUSE_HEAD));
  26. $this->assertEquals('Помощник рук.', getRoles(Role::ASSISTANT_HEAD));
  27. }
  28. public function test_get_roles_returns_all_when_key_not_found(): void
  29. {
  30. $result = getRoles('nonexistent');
  31. $this->assertIsArray($result);
  32. $this->assertCount(5, $result);
  33. }
  34. public function test_has_role_returns_true_for_correct_role(): void
  35. {
  36. $user = new \App\Models\User();
  37. $user->role = Role::ADMIN;
  38. $this->assertTrue(hasRole(Role::ADMIN, $user));
  39. }
  40. public function test_has_role_returns_false_for_wrong_role(): void
  41. {
  42. $user = new \App\Models\User();
  43. $user->role = Role::MANAGER;
  44. $this->assertFalse(hasRole(Role::ADMIN, $user));
  45. }
  46. public function test_has_role_supports_multiple_roles(): void
  47. {
  48. $user = new \App\Models\User();
  49. $user->role = Role::BRIGADIER;
  50. $this->assertTrue(hasRole(Role::MANAGER . ',' . Role::BRIGADIER, $user));
  51. }
  52. public function test_assistant_head_has_admin_permissions_in_helper(): void
  53. {
  54. $user = new \App\Models\User();
  55. $user->role = Role::ASSISTANT_HEAD;
  56. $this->assertTrue(hasRole(Role::ADMIN, $user));
  57. }
  58. public function test_has_role_returns_false_when_user_null(): void
  59. {
  60. $this->assertFalse(hasRole(Role::ADMIN, null));
  61. }
  62. public function test_role_name_returns_correct_name(): void
  63. {
  64. $this->assertEquals('Админ', roleName(Role::ADMIN));
  65. $this->assertEquals('Менеджер', roleName(Role::MANAGER));
  66. $this->assertEquals('Бригадир', roleName(Role::BRIGADIER));
  67. $this->assertEquals('Рук. Склада', roleName(Role::WAREHOUSE_HEAD));
  68. $this->assertEquals('Помощник рук.', roleName(Role::ASSISTANT_HEAD));
  69. }
  70. public function test_file_name_replaces_special_chars(): void
  71. {
  72. $result = fileName('file*name:test\\path/to?file<more>');
  73. $this->assertStringNotContainsString('*', $result);
  74. $this->assertStringNotContainsString(':', $result);
  75. $this->assertStringNotContainsString('\\', $result);
  76. $this->assertStringNotContainsString('/', $result);
  77. $this->assertStringNotContainsString('?', $result);
  78. $this->assertStringNotContainsString('<', $result);
  79. $this->assertStringNotContainsString('>', $result);
  80. }
  81. public function test_file_name_keeps_safe_chars(): void
  82. {
  83. $result = fileName('normal-file_name.xlsx');
  84. $this->assertEquals('normal-file_name.xlsx', $result);
  85. }
  86. public function test_year_returns_integer(): void
  87. {
  88. $this->assertIsInt(year());
  89. }
  90. public function test_year_returns_current_year_by_default(): void
  91. {
  92. $this->assertEquals((int) date('Y'), year());
  93. }
  94. }