X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a6633642232efd164d4708967ab59e498fbff896..HEAD:/tests/Api/ChaptersApiTest.php diff --git a/tests/Api/ChaptersApiTest.php b/tests/Api/ChaptersApiTest.php index c7368eaee..9698d4dd9 100644 --- a/tests/Api/ChaptersApiTest.php +++ b/tests/Api/ChaptersApiTest.php @@ -1,14 +1,18 @@ -getJson($this->baseEndpoint . '?count=1&sort=+id'); $resp->assertJson(['data' => [ [ - 'id' => $firstChapter->id, - 'name' => $firstChapter->name, - 'slug' => $firstChapter->slug, - 'book_id' => $firstChapter->book->id, - 'priority' => $firstChapter->priority, - ] + 'id' => $firstChapter->id, + 'name' => $firstChapter->name, + 'slug' => $firstChapter->slug, + 'book_id' => $firstChapter->book->id, + 'priority' => $firstChapter->priority, + 'book_slug' => $firstChapter->book->slug, + 'owned_by' => $firstChapter->owned_by, + 'created_by' => $firstChapter->created_by, + 'updated_by' => $firstChapter->updated_by, + ], ]]); } public function test_create_endpoint() { $this->actingAsApiEditor(); - $book = Book::query()->first(); + $book = $this->entities->book(); + $templatePage = $this->entities->templatePage(); $details = [ - 'name' => 'My API chapter', + 'name' => 'My API chapter', 'description' => 'A chapter created via the API', - 'book_id' => $book->id, - 'tags' => [ + 'book_id' => $book->id, + 'tags' => [ [ - 'name' => 'tagname', + 'name' => 'tagname', 'value' => 'tagvalue', - ] - ] + ], + ], + 'priority' => 15, + 'default_template_id' => $templatePage->id, ]; $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])); + $resp->assertJson(array_merge($details, [ + 'id' => $newItem->id, + 'slug' => $newItem->slug, + 'description_html' => '
A chapter created via the API
', + ])); $this->assertDatabaseHas('tags', [ - 'entity_id' => $newItem->id, + 'entity_id' => $newItem->id, 'entity_type' => $newItem->getMorphClass(), - 'name' => 'tagname', - 'value' => 'tagvalue', + 'name' => 'tagname', + 'value' => 'tagvalue', ]); $resp->assertJsonMissing(['pages' => []]); $this->assertActivityExists('chapter_create', $newItem); } + public function test_create_endpoint_with_html() + { + $this->actingAsApiEditor(); + $book = $this->entities->book(); + $details = [ + 'name' => 'My API chapter', + 'description_html' => 'A chapter created via the API
', + 'book_id' => $book->id, + ]; + + $resp = $this->postJson($this->baseEndpoint, $details); + $resp->assertStatus(200); + $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first(); + + $expectedDetails = array_merge($details, [ + 'id' => $newItem->id, + 'description' => 'A chapter created via the API', + ]); + $resp->assertJson($expectedDetails); + $this->assertDatabaseHas('chapters', $expectedDetails); + } + public function test_chapter_name_needed_to_create() { $this->actingAsApiEditor(); - $book = Book::query()->first(); + $book = $this->entities->book(); $details = [ - 'book_id' => $book->id, + 'book_id' => $book->id, 'description' => 'A chapter created via the API', ]; $resp = $this->postJson($this->baseEndpoint, $details); $resp->assertStatus(422); $resp->assertJson($this->validationResponse([ - "name" => ["The name field is required."] + 'name' => ['The name field is required.'], ])); } @@ -77,62 +114,78 @@ class ChaptersApiTest extends TestCase { $this->actingAsApiEditor(); $details = [ - 'name' => 'My api chapter', + 'name' => 'My api chapter', 'description' => 'A chapter created via the API', ]; $resp = $this->postJson($this->baseEndpoint, $details); $resp->assertStatus(422); $resp->assertJson($this->validationResponse([ - "book_id" => ["The book id field is required."] + 'book_id' => ['The book id field is required.'], ])); } 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}"); $resp->assertStatus(200); $resp->assertJson([ - 'id' => $chapter->id, - 'slug' => $chapter->slug, + 'id' => $chapter->id, + 'slug' => $chapter->slug, + 'book_slug' => $chapter->book->slug, 'created_by' => [ 'name' => $chapter->createdBy->name, ], - 'book_id' => $chapter->book_id, + 'book_id' => $chapter->book_id, 'updated_by' => [ 'name' => $chapter->createdBy->name, ], 'owned_by' => [ - 'name' => $chapter->ownedBy->name + 'name' => $chapter->ownedBy->name, ], 'pages' => [ [ - 'id' => $page->id, + 'id' => $page->id, 'slug' => $page->slug, 'name' => $page->name, - ] + 'owned_by' => $page->owned_by, + 'created_by' => $page->created_by, + 'updated_by' => $page->updated_by, + 'book_id' => $page->id, + 'chapter_id' => $chapter->id, + 'priority' => $page->priority, + 'book_slug' => $chapter->book->slug, + 'draft' => $page->draft, + 'template' => $page->template, + 'editor' => $page->editor, + ], ], + 'default_template_id' => null, ]); + $resp->assertJsonMissingPath('book'); $resp->assertJsonCount($chapter->pages()->count(), 'pages'); } public function test_update_endpoint() { $this->actingAsApiEditor(); - $chapter = Chapter::visible()->first(); + $chapter = $this->entities->chapter(); + $templatePage = $this->entities->templatePage(); $details = [ - 'name' => 'My updated API chapter', - 'description' => 'A chapter created via the API', - 'tags' => [ + 'name' => 'My updated API chapter', + 'description' => 'A chapter updated via the API', + 'tags' => [ [ - 'name' => 'freshtag', + 'name' => 'freshtag', 'value' => 'freshtagval', - ] + ], ], + 'priority' => 15, + 'default_template_id' => $templatePage->id, ]; $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); @@ -140,15 +193,77 @@ class ChaptersApiTest extends TestCase $resp->assertStatus(200); $resp->assertJson(array_merge($details, [ - 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id + 'id' => $chapter->id, + 'slug' => $chapter->slug, + 'book_id' => $chapter->book_id, + 'description_html' => 'A chapter updated via the API
', ])); $this->assertActivityExists('chapter_update', $chapter); } + public function test_update_endpoint_with_html() + { + $this->actingAsApiEditor(); + $chapter = $this->entities->chapter(); + $details = [ + 'name' => 'My updated API chapter', + 'description_html' => 'A chapter updated via the API
', + ]; + + $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); + $resp->assertStatus(200); + + $this->assertDatabaseHas('chapters', array_merge($details, [ + 'id' => $chapter->id, 'description' => 'A chapter updated via the API' + ])); + } + + public function test_update_increments_updated_date_if_only_tags_are_sent() + { + $this->actingAsApiEditor(); + $chapter = $this->entities->chapter(); + DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]); + + $details = [ + 'tags' => [['name' => 'Category', 'value' => 'Testing']], + ]; + + $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); + $chapter->refresh(); + $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); @@ -158,7 +273,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); @@ -169,7 +284,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); @@ -180,10 +295,35 @@ 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); $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"'); } -} \ No newline at end of file + + public function test_export_markdown_endpoint() + { + $this->actingAsApiEditor(); + $chapter = Chapter::visible()->has('pages')->first(); + + $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown"); + $resp->assertStatus(200); + $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"'); + $resp->assertSee('# ' . $chapter->name); + $resp->assertSee('# ' . $chapter->pages()->first()->name); + } + + public function test_cant_export_when_not_have_permission() + { + $types = ['html', 'plaintext', 'pdf', 'markdown']; + $this->actingAsApiEditor(); + $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']); + + $chapter = Chapter::visible()->has('pages')->first(); + foreach ($types as $type) { + $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}"); + $this->assertPermissionError($resp); + } + } +}