RolesHelperTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $this->assertArrayHasKey(Role::DRIVER, $roles);
  20. }
  21. public function test_get_roles_returns_name_by_key(): void
  22. {
  23. $this->assertEquals('Админ', getRoles(Role::ADMIN));
  24. $this->assertEquals('Менеджер', getRoles(Role::MANAGER));
  25. $this->assertEquals('Бригадир', getRoles(Role::BRIGADIER));
  26. $this->assertEquals('Рук. Склада', getRoles(Role::WAREHOUSE_HEAD));
  27. $this->assertEquals('Помощник рук.', getRoles(Role::ASSISTANT_HEAD));
  28. $this->assertEquals('Водитель', getRoles(Role::DRIVER));
  29. }
  30. public function test_get_roles_returns_all_when_key_not_found(): void
  31. {
  32. $result = getRoles('nonexistent');
  33. $this->assertIsArray($result);
  34. $this->assertCount(6, $result);
  35. }
  36. public function test_has_role_returns_true_for_correct_role(): void
  37. {
  38. $user = new \App\Models\User();
  39. $user->role = Role::ADMIN;
  40. $this->assertTrue(hasRole(Role::ADMIN, $user));
  41. }
  42. public function test_has_role_returns_false_for_wrong_role(): void
  43. {
  44. $user = new \App\Models\User();
  45. $user->role = Role::MANAGER;
  46. $this->assertFalse(hasRole(Role::ADMIN, $user));
  47. }
  48. public function test_has_role_supports_multiple_roles(): void
  49. {
  50. $user = new \App\Models\User();
  51. $user->role = Role::BRIGADIER;
  52. $this->assertTrue(hasRole(Role::MANAGER . ',' . Role::BRIGADIER, $user));
  53. }
  54. public function test_assistant_head_has_admin_permissions_in_helper(): void
  55. {
  56. $user = new \App\Models\User();
  57. $user->role = Role::ASSISTANT_HEAD;
  58. $this->assertTrue(hasRole(Role::ADMIN, $user));
  59. }
  60. public function test_has_role_returns_false_when_user_null(): void
  61. {
  62. $this->assertFalse(hasRole(Role::ADMIN, null));
  63. }
  64. public function test_role_name_returns_correct_name(): void
  65. {
  66. $this->assertEquals('Админ', roleName(Role::ADMIN));
  67. $this->assertEquals('Менеджер', roleName(Role::MANAGER));
  68. $this->assertEquals('Бригадир', roleName(Role::BRIGADIER));
  69. $this->assertEquals('Рук. Склада', roleName(Role::WAREHOUSE_HEAD));
  70. $this->assertEquals('Помощник рук.', roleName(Role::ASSISTANT_HEAD));
  71. $this->assertEquals('Водитель', roleName(Role::DRIVER));
  72. }
  73. public function test_file_name_replaces_special_chars(): void
  74. {
  75. $result = fileName('file*name:test\\path/to?file<more>');
  76. $this->assertStringNotContainsString('*', $result);
  77. $this->assertStringNotContainsString(':', $result);
  78. $this->assertStringNotContainsString('\\', $result);
  79. $this->assertStringNotContainsString('/', $result);
  80. $this->assertStringNotContainsString('?', $result);
  81. $this->assertStringNotContainsString('<', $result);
  82. $this->assertStringNotContainsString('>', $result);
  83. }
  84. public function test_file_name_keeps_safe_chars(): void
  85. {
  86. $result = fileName('normal-file_name.xlsx');
  87. $this->assertEquals('normal-file_name.xlsx', $result);
  88. }
  89. public function test_year_returns_integer(): void
  90. {
  91. $this->assertIsInt(year());
  92. }
  93. public function test_year_returns_current_year_by_default(): void
  94. {
  95. $this->assertEquals((int) date('Y'), year());
  96. }
  97. }