RolesHelperTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  18. public function test_get_roles_returns_name_by_key(): void
  19. {
  20. $this->assertEquals('Админ', getRoles(Role::ADMIN));
  21. $this->assertEquals('Менеджер', getRoles(Role::MANAGER));
  22. $this->assertEquals('Бригадир', getRoles(Role::BRIGADIER));
  23. }
  24. public function test_get_roles_returns_all_when_key_not_found(): void
  25. {
  26. $result = getRoles('nonexistent');
  27. $this->assertIsArray($result);
  28. $this->assertCount(3, $result);
  29. }
  30. public function test_has_role_returns_true_for_correct_role(): void
  31. {
  32. $user = new \App\Models\User();
  33. $user->role = Role::ADMIN;
  34. $this->assertTrue(hasRole(Role::ADMIN, $user));
  35. }
  36. public function test_has_role_returns_false_for_wrong_role(): void
  37. {
  38. $user = new \App\Models\User();
  39. $user->role = Role::MANAGER;
  40. $this->assertFalse(hasRole(Role::ADMIN, $user));
  41. }
  42. public function test_has_role_supports_multiple_roles(): void
  43. {
  44. $user = new \App\Models\User();
  45. $user->role = Role::BRIGADIER;
  46. $this->assertTrue(hasRole(Role::MANAGER . ',' . Role::BRIGADIER, $user));
  47. }
  48. public function test_has_role_returns_false_when_user_null(): void
  49. {
  50. $this->assertFalse(hasRole(Role::ADMIN, null));
  51. }
  52. public function test_role_name_returns_correct_name(): void
  53. {
  54. $this->assertEquals('Админ', roleName(Role::ADMIN));
  55. $this->assertEquals('Менеджер', roleName(Role::MANAGER));
  56. $this->assertEquals('Бригадир', roleName(Role::BRIGADIER));
  57. }
  58. public function test_file_name_replaces_special_chars(): void
  59. {
  60. $result = fileName('file*name:test\\path/to?file<more>');
  61. $this->assertStringNotContainsString('*', $result);
  62. $this->assertStringNotContainsString(':', $result);
  63. $this->assertStringNotContainsString('\\', $result);
  64. $this->assertStringNotContainsString('/', $result);
  65. $this->assertStringNotContainsString('?', $result);
  66. $this->assertStringNotContainsString('<', $result);
  67. $this->assertStringNotContainsString('>', $result);
  68. }
  69. public function test_file_name_keeps_safe_chars(): void
  70. {
  71. $result = fileName('normal-file_name.xlsx');
  72. $this->assertEquals('normal-file_name.xlsx', $result);
  73. }
  74. public function test_year_returns_integer(): void
  75. {
  76. $this->assertIsInt(year());
  77. }
  78. public function test_year_returns_current_year_by_default(): void
  79. {
  80. $this->assertEquals((int) date('Y'), year());
  81. }
  82. }