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_create_applies_correct_priority()
66 $this->actingAsApiEditor();
67 $book = $this->entities->book();
69 'name' => 'My API chapter',
70 'description' => 'A chapter created via the API',
71 'book_id' => $book->id,
75 'value' => 'tagvalue',
81 $resp = $this->postJson($this->baseEndpoint, $details);
82 $resp->assertStatus(200);
83 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
84 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
85 $this->assertDatabaseHas('tags', [
86 'entity_id' => $newItem->id,
87 'entity_type' => $newItem->getMorphClass(),
89 'value' => 'tagvalue',
91 $resp->assertJsonMissing(['pages' => []]);
92 $this->assertActivityExists('chapter_create', $newItem);
95 public function test_chapter_name_needed_to_create()
97 $this->actingAsApiEditor();
98 $book = $this->entities->book();
100 'book_id' => $book->id,
101 'description' => 'A chapter created via the API',
104 $resp = $this->postJson($this->baseEndpoint, $details);
105 $resp->assertStatus(422);
106 $resp->assertJson($this->validationResponse([
107 'name' => ['The name field is required.'],
111 public function test_chapter_book_id_needed_to_create()
113 $this->actingAsApiEditor();
115 'name' => 'My api chapter',
116 'description' => 'A chapter created via the API',
119 $resp = $this->postJson($this->baseEndpoint, $details);
120 $resp->assertStatus(422);
121 $resp->assertJson($this->validationResponse([
122 'book_id' => ['The book id field is required.'],
126 public function test_read_endpoint()
128 $this->actingAsApiEditor();
129 $chapter = $this->entities->chapter();
130 $page = $chapter->pages()->first();
132 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
133 $resp->assertStatus(200);
135 'id' => $chapter->id,
136 'slug' => $chapter->slug,
138 'name' => $chapter->createdBy->name,
140 'book_id' => $chapter->book_id,
142 'name' => $chapter->createdBy->name,
145 'name' => $chapter->ownedBy->name,
150 'slug' => $page->slug,
151 'name' => $page->name,
155 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
158 public function test_update_endpoint()
160 $this->actingAsApiEditor();
161 $chapter = $this->entities->chapter();
163 'name' => 'My updated API chapter',
164 'description' => 'A chapter created via the API',
167 'name' => 'freshtag',
168 'value' => 'freshtagval',
174 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
177 $resp->assertStatus(200);
178 $resp->assertJson(array_merge($details, [
179 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
181 $this->assertActivityExists('chapter_update', $chapter);
184 public function test_update_increments_updated_date_if_only_tags_are_sent()
186 $this->actingAsApiEditor();
187 $chapter = $this->entities->chapter();
188 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
191 'tags' => [['name' => 'Category', 'value' => 'Testing']],
194 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
196 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
199 public function test_update_with_book_id_moves_chapter()
201 $this->actingAsApiEditor();
202 $chapter = $this->entities->chapterHasPages();
203 $page = $chapter->pages()->first();
204 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
206 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
210 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
211 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
214 public function test_update_with_new_book_id_requires_delete_permission()
216 $editor = $this->users->editor();
217 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
218 $this->actingAs($editor);
219 $chapter = $this->entities->chapterHasPages();
220 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
222 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
223 $this->assertPermissionError($resp);
226 public function test_delete_endpoint()
228 $this->actingAsApiEditor();
229 $chapter = $this->entities->chapter();
230 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
232 $resp->assertStatus(204);
233 $this->assertActivityExists('chapter_delete');
236 public function test_export_html_endpoint()
238 $this->actingAsApiEditor();
239 $chapter = $this->entities->chapter();
241 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
242 $resp->assertStatus(200);
243 $resp->assertSee($chapter->name);
244 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
247 public function test_export_plain_text_endpoint()
249 $this->actingAsApiEditor();
250 $chapter = $this->entities->chapter();
252 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
253 $resp->assertStatus(200);
254 $resp->assertSee($chapter->name);
255 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
258 public function test_export_pdf_endpoint()
260 $this->actingAsApiEditor();
261 $chapter = $this->entities->chapter();
263 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
264 $resp->assertStatus(200);
265 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
268 public function test_export_markdown_endpoint()
270 $this->actingAsApiEditor();
271 $chapter = Chapter::visible()->has('pages')->first();
273 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
274 $resp->assertStatus(200);
275 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
276 $resp->assertSee('# ' . $chapter->name);
277 $resp->assertSee('# ' . $chapter->pages()->first()->name);
280 public function test_cant_export_when_not_have_permission()
282 $types = ['html', 'plaintext', 'pdf', 'markdown'];
283 $this->actingAsApiEditor();
284 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
286 $chapter = Chapter::visible()->has('pages')->first();
287 foreach ($types as $type) {
288 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
289 $this->assertPermissionError($resp);