X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/968bc8cdf354d9cbe29b88abdc747a7845031fab..refs/pull/5280/head:/tests/Api/ChaptersApiTest.php diff --git a/tests/Api/ChaptersApiTest.php b/tests/Api/ChaptersApiTest.php index 0629f3aed..9698d4dd9 100644 --- a/tests/Api/ChaptersApiTest.php +++ b/tests/Api/ChaptersApiTest.php @@ -22,11 +22,15 @@ class ChaptersApiTest extends TestCase $resp = $this->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, ], ]]); } @@ -35,6 +39,7 @@ class ChaptersApiTest extends TestCase { $this->actingAsApiEditor(); $book = $this->entities->book(); + $templatePage = $this->entities->templatePage(); $details = [ 'name' => 'My API chapter', 'description' => 'A chapter created via the API', @@ -46,12 +51,17 @@ class ChaptersApiTest extends TestCase ], ], '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_type' => $newItem->getMorphClass(), @@ -62,6 +72,28 @@ class ChaptersApiTest extends TestCase $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(); @@ -104,6 +136,7 @@ class ChaptersApiTest extends TestCase $resp->assertJson([ 'id' => $chapter->id, 'slug' => $chapter->slug, + 'book_slug' => $chapter->book->slug, 'created_by' => [ 'name' => $chapter->createdBy->name, ], @@ -119,9 +152,21 @@ class ChaptersApiTest extends TestCase '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'); } @@ -129,9 +174,10 @@ class ChaptersApiTest extends TestCase { $this->actingAsApiEditor(); $chapter = $this->entities->chapter(); + $templatePage = $this->entities->templatePage(); $details = [ 'name' => 'My updated API chapter', - 'description' => 'A chapter created via the API', + 'description' => 'A chapter updated via the API', 'tags' => [ [ 'name' => 'freshtag', @@ -139,6 +185,7 @@ class ChaptersApiTest extends TestCase ], ], 'priority' => 15, + 'default_template_id' => $templatePage->id, ]; $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); @@ -146,11 +193,31 @@ 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();