]> BookStack Code Mirror - bookstack/blobdiff - tests/BrowserKitTest.php
Adds test cases and fixes an issue with the permission checking.
[bookstack] / tests / BrowserKitTest.php
index 8e02cb5f106b60e780963bffd089f56e6831e8df..1eabc74170ab75067e1ff95c57057cc0d9e156d6 100644 (file)
@@ -1,6 +1,9 @@
 <?php namespace Tests;
 
+use BookStack\Entity;
 use BookStack\Role;
+use BookStack\Services\PermissionService;
+use BookStack\User;
 use Illuminate\Contracts\Console\Kernel;
 use Illuminate\Foundation\Testing\DatabaseTransactions;
 use Laravel\BrowserKitTesting\TestCase;
@@ -22,6 +25,12 @@ abstract class BrowserKitTest extends TestCase
     private $admin;
     private $editor;
 
+    public function tearDown()
+    {
+        \DB::disconnect();
+        parent::tearDown();
+    }
+
     /**
      * Creates the application.
      *
@@ -99,11 +108,9 @@ abstract class BrowserKitTest extends TestCase
     {
         if ($updaterUser === false) $updaterUser = $creatorUser;
         $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
-        $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
-        $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
-        $book->chapters()->saveMany([$chapter]);
-        $chapter->pages()->saveMany([$page]);
-        $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
+        $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
+        $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
+        $restrictionService = $this->app[PermissionService::class];
         $restrictionService->buildJointPermissionsForEntity($book);
         return [
             'book' => $book,
@@ -113,15 +120,35 @@ abstract class BrowserKitTest extends TestCase
     }
 
     /**
-     * Quick way to create a new user
+     * Helper for updating entity permissions.
+     * @param Entity $entity
+     */
+    protected function updateEntityPermissions(Entity $entity)
+    {
+        $restrictionService = $this->app[PermissionService::class];
+        $restrictionService->buildJointPermissionsForEntity($entity);
+    }
+
+    /**
+     * Get an instance of a user with 'editor' permissions
      * @param array $attributes
      * @return mixed
      */
     protected function getEditor($attributes = [])
     {
-        $user = factory(\BookStack\User::class)->create($attributes);
-        $role = Role::getRole('editor');
-        $user->attachRole($role);;
+        $user = \BookStack\Role::getRole('editor')->users()->first();
+        if (!empty($attributes)) $user->forceFill($attributes)->save();
+        return $user;
+    }
+
+    /**
+     * Get an instance of a user with 'viewer' permissions
+     * @return mixed
+     */
+    protected function getViewer()
+    {
+        $user = \BookStack\Role::getRole('viewer')->users()->first();
+        if (!empty($attributes)) $user->forceFill($attributes)->save();
         return $user;
     }