Skip to content

FOUR-19377 #7418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/Feature/Api/PermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,55 @@ public function testCategoryPermission()
$context('script', ScriptCategory::class);
$context('screen', ScreenCategory::class);
}

/**
* Test if the created event in UserObserver assigns the correct permissions.
*/
public function testSetPermissionsViewMyRequestForUser()
{
$permissionName = 'view-my_requests';
$permissionTitle = 'View My Requests';
// Ensure permission is created without duplicates
Permission::firstOrCreate(['name' => $permissionName, 'title' => $permissionTitle]);

// Create a user (this should trigger the observer)
$user = User::factory()->create();

// Assert the user has been assigned the correct permissions
$this->assertTrue($user->permissions()->where('name', $permissionName)->exists());
}

/**
* Test that the permissions are seeded and assigned to users and groups.
*/
public function testSetPermissionsViewMyRequestForUsersAndGroupCreated()
{
//Set up the users and groups
$users = User::factory()->count(5)->create();
$groups = Group::factory()->count(3)->create();

//Run the seeder
$this->seed(PermissionSeeder::class);
$permissionName = 'view-my_requests';
$permissionTitle = 'View My Requests';

//Verify that the permission exists
$this->assertDatabaseHas('permissions', [
'name' => $permissionName,
'group' => 'Cases and Requests',
'title' => $permissionTitle,
]);

//Verify that the permission is assigned to users
$permission = Permission::where('name', $permissionName)->first();
$this->assertNotNull($permission);
foreach ($users as $user) {
$this->assertTrue($user->hasPermission($permissionName));
}

//Verify that the permission is assigned to groups
foreach ($groups as $group) {
$this->assertTrue($permission->groups->contains($group));
}
}
}
Loading