]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/ChaptersApiTest.php
add tests for priority
[bookstack] / tests / Api / ChaptersApiTest.php
index a48e3b02626f5a80a47031cb71acbf942f707696..a99a85af8167b9009e01718ea086b5b92408102d 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Tests\Api;
 
+use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Chapter;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
@@ -60,6 +61,37 @@ class ChaptersApiTest extends TestCase
         $this->assertActivityExists('chapter_create', $newItem);
     }
 
+    public function test_create_applies_correct_priority()
+    {
+        $this->actingAsApiEditor();
+        $book = $this->entities->book();
+        $details = [
+            'name'        => 'My API chapter',
+            'description' => 'A chapter created via the API',
+            'book_id'     => $book->id,
+            'tags'        => [
+                [
+                    'name'  => 'tagname',
+                    'value' => 'tagvalue',
+                ],
+            ],
+            'priority'     => 15,
+        ];
+
+        $resp = $this->postJson($this->baseEndpoint, $details);
+        $resp->assertStatus(200);
+        $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
+        $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
+        $this->assertDatabaseHas('tags', [
+            'entity_id'   => $newItem->id,
+            'entity_type' => $newItem->getMorphClass(),
+            'name'        => 'tagname',
+            'value'       => 'tagvalue',
+        ]);
+        $resp->assertJsonMissing(['pages' => []]);
+        $this->assertActivityExists('chapter_create', $newItem);
+    }
+
     public function test_chapter_name_needed_to_create()
     {
         $this->actingAsApiEditor();
@@ -136,6 +168,7 @@ class ChaptersApiTest extends TestCase
                     'value' => 'freshtagval',
                 ],
             ],
+            'priority'    => 15,
         ];
 
         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
@@ -163,6 +196,33 @@ 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();