5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
8 use Illuminate\Support\Facades\DB;
11 class ChaptersApiTest extends TestCase
15 protected string $baseEndpoint = '/api/chapters';
17 public function test_index_endpoint_returns_expected_chapter()
19 $this->actingAsApiEditor();
20 $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
22 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23 $resp->assertJson(['data' => [
25 'id' => $firstChapter->id,
26 'name' => $firstChapter->name,
27 'slug' => $firstChapter->slug,
28 'book_id' => $firstChapter->book->id,
29 'priority' => $firstChapter->priority,
34 public function test_create_endpoint()
36 $this->actingAsApiEditor();
37 $book = $this->entities->book();
39 'name' => 'My API chapter',
40 'description' => 'A chapter created via the API',
41 'book_id' => $book->id,
45 'value' => 'tagvalue',
50 $resp = $this->postJson($this->baseEndpoint, $details);
51 $resp->assertStatus(200);
52 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
53 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
54 $this->assertDatabaseHas('tags', [
55 'entity_id' => $newItem->id,
56 'entity_type' => $newItem->getMorphClass(),
58 'value' => 'tagvalue',
60 $resp->assertJsonMissing(['pages' => []]);
61 $this->assertActivityExists('chapter_create', $newItem);
64 public function test_chapter_name_needed_to_create()
66 $this->actingAsApiEditor();
67 $book = $this->entities->book();
69 'book_id' => $book->id,
70 'description' => 'A chapter created via the API',
73 $resp = $this->postJson($this->baseEndpoint, $details);
74 $resp->assertStatus(422);
75 $resp->assertJson($this->validationResponse([
76 'name' => ['The name field is required.'],
80 public function test_chapter_book_id_needed_to_create()
82 $this->actingAsApiEditor();
84 'name' => 'My api chapter',
85 'description' => 'A chapter created via the API',
88 $resp = $this->postJson($this->baseEndpoint, $details);
89 $resp->assertStatus(422);
90 $resp->assertJson($this->validationResponse([
91 'book_id' => ['The book id field is required.'],
95 public function test_read_endpoint()
97 $this->actingAsApiEditor();
98 $chapter = $this->entities->chapter();
99 $page = $chapter->pages()->first();
101 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
102 $resp->assertStatus(200);
104 'id' => $chapter->id,
105 'slug' => $chapter->slug,
107 'name' => $chapter->createdBy->name,
109 'book_id' => $chapter->book_id,
111 'name' => $chapter->createdBy->name,
114 'name' => $chapter->ownedBy->name,
119 'slug' => $page->slug,
120 'name' => $page->name,
124 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
127 public function test_update_endpoint()
129 $this->actingAsApiEditor();
130 $chapter = $this->entities->chapter();
132 'name' => 'My updated API chapter',
133 'description' => 'A chapter created via the API',
136 'name' => 'freshtag',
137 'value' => 'freshtagval',
142 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
145 $resp->assertStatus(200);
146 $resp->assertJson(array_merge($details, [
147 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
149 $this->assertActivityExists('chapter_update', $chapter);
152 public function test_update_increments_updated_date_if_only_tags_are_sent()
154 $this->actingAsApiEditor();
155 $chapter = $this->entities->chapter();
156 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
159 'tags' => [['name' => 'Category', 'value' => 'Testing']],
162 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
164 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
167 public function test_update_with_book_id_moves_chapter()
169 $this->actingAsApiEditor();
170 $chapter = $this->entities->chapterHasPages();
171 $page = $chapter->pages()->first();
172 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
174 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
178 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
179 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
182 public function test_update_with_new_book_id_requires_delete_permission()
184 $editor = $this->users->editor();
185 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
186 $this->actingAs($editor);
187 $chapter = $this->entities->chapterHasPages();
188 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
190 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
191 $this->assertPermissionError($resp);
194 public function test_delete_endpoint()
196 $this->actingAsApiEditor();
197 $chapter = $this->entities->chapter();
198 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
200 $resp->assertStatus(204);
201 $this->assertActivityExists('chapter_delete');
204 public function test_export_html_endpoint()
206 $this->actingAsApiEditor();
207 $chapter = $this->entities->chapter();
209 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
210 $resp->assertStatus(200);
211 $resp->assertSee($chapter->name);
212 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
215 public function test_export_plain_text_endpoint()
217 $this->actingAsApiEditor();
218 $chapter = $this->entities->chapter();
220 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
221 $resp->assertStatus(200);
222 $resp->assertSee($chapter->name);
223 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
226 public function test_export_pdf_endpoint()
228 $this->actingAsApiEditor();
229 $chapter = $this->entities->chapter();
231 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
232 $resp->assertStatus(200);
233 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
236 public function test_export_markdown_endpoint()
238 $this->actingAsApiEditor();
239 $chapter = Chapter::visible()->has('pages')->first();
241 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
242 $resp->assertStatus(200);
243 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
244 $resp->assertSee('# ' . $chapter->name);
245 $resp->assertSee('# ' . $chapter->pages()->first()->name);
248 public function test_cant_export_when_not_have_permission()
250 $types = ['html', 'plaintext', 'pdf', 'markdown'];
251 $this->actingAsApiEditor();
252 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
254 $chapter = Chapter::visible()->has('pages')->first();
255 foreach ($types as $type) {
256 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
257 $this->assertPermissionError($resp);