]> BookStack Code Mirror - bookstack/blobdiff - tests/SharedTestHelpers.php
Applied StyleCI changes
[bookstack] / tests / SharedTestHelpers.php
index cbf49bf71f424f747e97bc04b3e3ff55fa6bb50b..bc7b10266b06cfe9bc4fc0a78f2adefcfe920277 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Tests;
 
-use BookStack\Auth\Permissions\PermissionService;
+use BookStack\Auth\Permissions\JointPermissionBuilder;
 use BookStack\Auth\Permissions\PermissionsRepo;
 use BookStack\Auth\Permissions\RolePermission;
 use BookStack\Auth\Role;
@@ -176,7 +176,7 @@ trait SharedTestHelpers
 
         $entity->save();
         $entity->load('permissions');
-        $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
+        $this->app->make(JointPermissionBuilder::class)->rebuildForEntity($entity);
         $entity->load('jointPermissions');
     }
 
@@ -194,13 +194,23 @@ trait SharedTestHelpers
     /**
      * Completely remove the given permission name from the given user.
      */
-    protected function removePermissionFromUser(User $user, string $permission)
+    protected function removePermissionFromUser(User $user, string $permissionName)
     {
-        $permission = RolePermission::query()->where('name', '=', $permission)->first();
+        $permissionBuilder = app()->make(JointPermissionBuilder::class);
+
+        /** @var RolePermission $permission */
+        $permission = RolePermission::query()->where('name', '=', $permissionName)->firstOrFail();
+
+        $roles = $user->roles()->whereHas('permissions', function ($query) use ($permission) {
+            $query->where('id', '=', $permission->id);
+        })->get();
+
         /** @var Role $role */
-        foreach ($user->roles as $role) {
+        foreach ($roles as $role) {
             $role->detachPermission($permission);
+            $permissionBuilder->rebuildForRole($role);
         }
+
         $user->clearPermissionCache();
     }
 
@@ -231,8 +241,8 @@ trait SharedTestHelpers
         $book = Book::factory()->create($userAttrs);
         $chapter = Chapter::factory()->create(array_merge(['book_id' => $book->id], $userAttrs));
         $page = Page::factory()->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
-        $restrictionService = $this->app[PermissionService::class];
-        $restrictionService->buildJointPermissionsForEntity($book);
+
+        $this->app->make(JointPermissionBuilder::class)->rebuildForEntity($book);
 
         return compact('book', 'chapter', 'page');
     }