]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/ChaptersApiTest.php
Removed redundant null check
[bookstack] / tests / Api / ChaptersApiTest.php
index 8d31500ebc19ce3360cddfd7d823eab03fe4cab4..0629f3aedae61fadd338fcc9dc20cfefc959da2d 100644 (file)
@@ -34,7 +34,7 @@ class ChaptersApiTest extends TestCase
     public function test_create_endpoint()
     {
         $this->actingAsApiEditor();
-        $book = Book::query()->first();
+        $book = $this->entities->book();
         $details = [
             'name'        => 'My API chapter',
             'description' => 'A chapter created via the API',
@@ -45,6 +45,7 @@ class ChaptersApiTest extends TestCase
                     'value' => 'tagvalue',
                 ],
             ],
+            'priority' => 15,
         ];
 
         $resp = $this->postJson($this->baseEndpoint, $details);
@@ -64,7 +65,7 @@ class ChaptersApiTest extends TestCase
     public function test_chapter_name_needed_to_create()
     {
         $this->actingAsApiEditor();
-        $book = Book::query()->first();
+        $book = $this->entities->book();
         $details = [
             'book_id'     => $book->id,
             'description' => 'A chapter created via the API',
@@ -95,7 +96,7 @@ class ChaptersApiTest extends TestCase
     public function test_read_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
         $page = $chapter->pages()->first();
 
         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
@@ -127,7 +128,7 @@ class ChaptersApiTest extends TestCase
     public function test_update_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
         $details = [
             'name'        => 'My updated API chapter',
             'description' => 'A chapter created via the API',
@@ -137,6 +138,7 @@ class ChaptersApiTest extends TestCase
                     'value' => 'freshtagval',
                 ],
             ],
+            'priority'    => 15,
         ];
 
         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
@@ -152,7 +154,7 @@ class ChaptersApiTest extends TestCase
     public function test_update_increments_updated_date_if_only_tags_are_sent()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
         DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
 
         $details = [
@@ -164,10 +166,37 @@ class ChaptersApiTest extends TestCase
         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
     }
 
+    public function test_update_with_book_id_moves_chapter()
+    {
+        $this->actingAsApiEditor();
+        $chapter = $this->entities->chapterHasPages();
+        $page = $chapter->pages()->first();
+        $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
+
+        $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
+        $resp->assertOk();
+        $chapter->refresh();
+
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
+        $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
+    }
+
+    public function test_update_with_new_book_id_requires_delete_permission()
+    {
+        $editor = $this->users->editor();
+        $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
+        $this->actingAs($editor);
+        $chapter = $this->entities->chapterHasPages();
+        $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
+
+        $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
+        $this->assertPermissionError($resp);
+    }
+
     public function test_delete_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
 
         $resp->assertStatus(204);
@@ -177,7 +206,7 @@ class ChaptersApiTest extends TestCase
     public function test_export_html_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
 
         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
         $resp->assertStatus(200);
@@ -188,7 +217,7 @@ class ChaptersApiTest extends TestCase
     public function test_export_plain_text_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
 
         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
         $resp->assertStatus(200);
@@ -199,7 +228,7 @@ class ChaptersApiTest extends TestCase
     public function test_export_pdf_endpoint()
     {
         $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->first();
+        $chapter = $this->entities->chapter();
 
         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
         $resp->assertStatus(200);
@@ -222,7 +251,7 @@ class ChaptersApiTest extends TestCase
     {
         $types = ['html', 'plaintext', 'pdf', 'markdown'];
         $this->actingAsApiEditor();
-        $this->removePermissionFromUser($this->getEditor(), 'content-export');
+        $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
 
         $chapter = Chapter::visible()->has('pages')->first();
         foreach ($types as $type) {