]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/ConvertTest.php
fix Sidebar scrolling at mid-range sceen
[bookstack] / tests / Entity / ConvertTest.php
index 4a949e76fe5dd2c38d78f150b21c5bb07d821e95..decda52243f7d39d4169a5b0d4fb2a13e08dc726 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Tests\Entity;
 
-use BookStack\Actions\ActivityType;
-use BookStack\Actions\Tag;
+use BookStack\Activity\ActivityType;
+use BookStack\Activity\Models\Tag;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
 use BookStack\Entities\Models\Chapter;
@@ -12,22 +12,19 @@ use Tests\TestCase;
 
 class ConvertTest extends TestCase
 {
-
     public function test_chapter_edit_view_shows_convert_option()
     {
-        /** @var Chapter $chapter */
-        $chapter = Chapter::query()->first();
+        $chapter = $this->entities->chapter();
 
         $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
         $resp->assertSee('Convert to Book');
         $resp->assertSee('Convert Chapter');
-        $resp->assertElementExists('form[action$="/convert-to-book"] button');
+        $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-book"] button');
     }
 
     public function test_convert_chapter_to_book()
     {
-        /** @var Chapter $chapter */
-        $chapter = Chapter::query()->whereHas('pages')->first();
+        $chapter = $this->entities->chapterHasPages();
         $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
         /** @var Page $childPage */
         $childPage = $chapter->pages()->first();
@@ -49,15 +46,35 @@ class ConvertTest extends TestCase
         $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
     }
 
+    public function test_convert_chapter_to_book_requires_permissions()
+    {
+        $chapter = $this->entities->chapter();
+        $user = $this->users->viewer();
+
+        $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
+        $this->permissions->grantUserRolePermissions($user, $permissions);
+
+        foreach ($permissions as $permission) {
+            $this->permissions->removeUserRolePermissions($user, [$permission]);
+            $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
+            $this->assertPermissionError($resp);
+            $this->permissions->grantUserRolePermissions($user, [$permission]);
+        }
+
+        $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
+        $this->assertNotPermissionError($resp);
+        $resp->assertRedirect();
+    }
+
     public function test_book_edit_view_shows_convert_option()
     {
-        $book = Book::query()->first();
+        $book = $this->entities->book();
 
         $resp = $this->asEditor()->get($book->getUrl('/edit'));
         $resp->assertSee('Convert to Shelf');
         $resp->assertSee('Convert Book');
         $resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
-        $resp->assertElementExists('form[action$="/convert-to-shelf"] button');
+        $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-shelf"] button');
     }
 
     public function test_book_convert_to_shelf()
@@ -102,4 +119,23 @@ class ConvertTest extends TestCase
         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
     }
 
-}
\ No newline at end of file
+    public function test_book_convert_to_shelf_requires_permissions()
+    {
+        $book = $this->entities->book();
+        $user = $this->users->viewer();
+
+        $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
+        $this->permissions->grantUserRolePermissions($user, $permissions);
+
+        foreach ($permissions as $permission) {
+            $this->permissions->removeUserRolePermissions($user, [$permission]);
+            $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
+            $this->assertPermissionError($resp);
+            $this->permissions->grantUserRolePermissions($user, [$permission]);
+        }
+
+        $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
+        $this->assertNotPermissionError($resp);
+        $resp->assertRedirect();
+    }
+}