]> BookStack Code Mirror - bookstack/blob - tests/Helpers/UserRoleProvider.php
Lexical: Added tests to cover recent changes
[bookstack] / tests / Helpers / UserRoleProvider.php
1 <?php
2
3 namespace Tests\Helpers;
4
5 use BookStack\Permissions\PermissionsRepo;
6 use BookStack\Users\Models\Role;
7 use BookStack\Users\Models\User;
8
9 class UserRoleProvider
10 {
11     protected ?User $admin = null;
12     protected ?User $editor = null;
13
14     /**
15      * Get a typical "Admin" user.
16      */
17     public function admin(): User
18     {
19         if (is_null($this->admin)) {
20             $adminRole = Role::getSystemRole('admin');
21             $this->admin = $adminRole->users()->first();
22         }
23
24         return $this->admin;
25     }
26
27     /**
28      * Get a typical "Editor" user.
29      */
30     public function editor(): User
31     {
32         if ($this->editor === null) {
33             $editorRole = Role::getRole('editor');
34             $this->editor = $editorRole->users->first();
35         }
36
37         return $this->editor;
38     }
39
40     /**
41      * Get a typical "Viewer" user.
42      */
43     public function viewer(array $attributes = []): User
44     {
45         $user = Role::getRole('viewer')->users()->first();
46         if (!empty($attributes)) {
47             $user->forceFill($attributes)->save();
48         }
49
50         return $user;
51     }
52
53     /**
54      * Get the system "guest" user.
55      */
56     public function guest(): User
57     {
58         return User::getGuest();
59     }
60
61     /**
62      * Create a new fresh user without any relations.
63      */
64     public function newUser(array $attrs = []): User
65     {
66         return User::factory()->create($attrs);
67     }
68
69     /**
70      * Create a new fresh user, with the given attrs, that has assigned a fresh role
71      * that has the given role permissions.
72      * Intended as a helper to create a blank slate baseline user and role.
73      * @return array{0: User, 1: Role}
74      */
75     public function newUserWithRole(array $userAttrs = [], array $rolePermissions = []): array
76     {
77         $user = $this->newUser($userAttrs);
78         $role = $this->attachNewRole($user, $rolePermissions);
79
80         return [$user, $role];
81     }
82
83     /**
84      * Attach a new role, with the given role permissions, to the given user
85      * and return that role.
86      */
87     public function attachNewRole(User $user, array $rolePermissions = []): Role
88     {
89         $role = $this->createRole($rolePermissions);
90         $user->attachRole($role);
91         return $role;
92     }
93
94     /**
95      * Create a new basic role with the given role permissions.
96      */
97     public function createRole(array $rolePermissions = []): Role
98     {
99         $permissionRepo = app(PermissionsRepo::class);
100         $roleData = Role::factory()->make()->toArray();
101         $roleData['permissions'] = $rolePermissions;
102
103         return $permissionRepo->saveNewRole($roleData);
104     }
105 }