]> BookStack Code Mirror - bookstack/blobdiff - tests/Helpers/EntityProvider.php
respective book and chapter structure added.
[bookstack] / tests / Helpers / EntityProvider.php
index 4af6957a182a7d369a5078ccfe01f55296d01497..1897abefa28501009366f76661a925b1fb50214a 100644 (file)
@@ -2,9 +2,6 @@
 
 namespace Tests\Helpers;
 
-use BookStack\Auth\Permissions\EntityPermission;
-use BookStack\Auth\Role;
-use BookStack\Auth\User;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
 use BookStack\Entities\Models\Chapter;
@@ -14,6 +11,8 @@ use BookStack\Entities\Repos\BookRepo;
 use BookStack\Entities\Repos\BookshelfRepo;
 use BookStack\Entities\Repos\ChapterRepo;
 use BookStack\Entities\Repos\PageRepo;
+use BookStack\Entities\Tools\TrashCan;
+use BookStack\Users\Models\User;
 use Illuminate\Database\Eloquent\Builder;
 
 /**
@@ -54,6 +53,15 @@ class EntityProvider
         return $this->page(fn(Builder $query) => $query->where('chapter_id', '=', 0));
     }
 
+    public function templatePage(): Page
+    {
+        $page = $this->page();
+        $page->template = true;
+        $page->save();
+
+        return $page;
+    }
+
     /**
      * Get an un-fetched chapter from the system.
      */
@@ -187,37 +195,49 @@ class EntityProvider
     }
 
     /**
-     * Regenerate the permission for an entity.
-     * Centralised to manage clearing of cached elements between requests.
+     * Create and return a new test draft page.
      */
-    public function regenPermissions(Entity $entity): void
+    public function newDraftPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page
     {
-        $entity->rebuildPermissions();
-        $entity->load('jointPermissions');
+        $book = $this->book();
+        $pageRepo = app(PageRepo::class);
+        $draftPage = $pageRepo->getNewDraftPage($book);
+        $pageRepo->updatePageDraft($draftPage, $input);
+        $this->addToCache($draftPage);
+        return $draftPage;
     }
 
     /**
-     * Set the given entity as having restricted permissions, and apply the given
-     * permissions for the given roles.
-     * @param string[] $actions
-     * @param Role[] $roles
+     * Send an entity to the recycle bin.
      */
-    public function setPermissions(Entity $entity, array $actions = [], array $roles = []): void
+    public function sendToRecycleBin(Entity $entity)
     {
-        $entity->permissions()->delete();
+        $trash = app()->make(TrashCan::class);
+
+        if ($entity instanceof Page) {
+            $trash->softDestroyPage($entity);
+        } elseif ($entity instanceof Chapter) {
+            $trash->softDestroyChapter($entity);
+        } elseif ($entity instanceof Book) {
+            $trash->softDestroyBook($entity);
+        } elseif ($entity instanceof Bookshelf) {
+            $trash->softDestroyBookshelf($entity);
+        }
 
-        $permissions = [];
-        foreach ($roles as $role) {
-            $permission = ['role_id' => $role->id];
-            foreach (EntityPermission::PERMISSIONS as $possibleAction) {
-                $permission[$possibleAction] = in_array($possibleAction, $actions);
-            }
-            $permissions[] = $permission;
+        $entity->refresh();
+        if (is_null($entity->deleted_at)) {
+            throw new \Exception("Could not send entity type [{$entity->getMorphClass()}] to the recycle bin");
         }
+    }
 
-        $entity->permissions()->createMany($permissions);
-        $entity->load('permissions');
-        $this->regenPermissions($entity);
+    /**
+     * Fully destroy the given entity from the system, bypassing the recycle bin
+     * stage. Still runs through main app deletion logic.
+     */
+    public function destroy(Entity $entity)
+    {
+        $trash = app()->make(TrashCan::class);
+        $trash->destroyEntity($entity);
     }
 
     /**