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',
51 $resp = $this->postJson($this->baseEndpoint, $details);
52 $resp->assertStatus(200);
53 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
54 $resp->assertJson(array_merge($details, [
56 'slug' => $newItem->slug,
57 'description_html' => '<p>A chapter created via the API</p>',
59 $this->assertDatabaseHas('tags', [
60 'entity_id' => $newItem->id,
61 'entity_type' => $newItem->getMorphClass(),
63 'value' => 'tagvalue',
65 $resp->assertJsonMissing(['pages' => []]);
66 $this->assertActivityExists('chapter_create', $newItem);
69 public function test_create_endpoint_with_html()
71 $this->actingAsApiEditor();
72 $book = $this->entities->book();
74 'name' => 'My API chapter',
75 'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
76 'book_id' => $book->id,
79 $resp = $this->postJson($this->baseEndpoint, $details);
80 $resp->assertStatus(200);
81 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
83 $expectedDetails = array_merge($details, [
85 'description' => 'A chapter created via the API',
87 $resp->assertJson($expectedDetails);
88 $this->assertDatabaseHas('chapters', $expectedDetails);
91 public function test_chapter_name_needed_to_create()
93 $this->actingAsApiEditor();
94 $book = $this->entities->book();
96 'book_id' => $book->id,
97 'description' => 'A chapter created via the API',
100 $resp = $this->postJson($this->baseEndpoint, $details);
101 $resp->assertStatus(422);
102 $resp->assertJson($this->validationResponse([
103 'name' => ['The name field is required.'],
107 public function test_chapter_book_id_needed_to_create()
109 $this->actingAsApiEditor();
111 'name' => 'My api chapter',
112 'description' => 'A chapter created via the API',
115 $resp = $this->postJson($this->baseEndpoint, $details);
116 $resp->assertStatus(422);
117 $resp->assertJson($this->validationResponse([
118 'book_id' => ['The book id field is required.'],
122 public function test_read_endpoint()
124 $this->actingAsApiEditor();
125 $chapter = $this->entities->chapter();
126 $page = $chapter->pages()->first();
128 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
129 $resp->assertStatus(200);
131 'id' => $chapter->id,
132 'slug' => $chapter->slug,
134 'name' => $chapter->createdBy->name,
136 'book_id' => $chapter->book_id,
138 'name' => $chapter->createdBy->name,
141 'name' => $chapter->ownedBy->name,
146 'slug' => $page->slug,
147 'name' => $page->name,
151 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
154 public function test_update_endpoint()
156 $this->actingAsApiEditor();
157 $chapter = $this->entities->chapter();
159 'name' => 'My updated API chapter',
160 'description' => 'A chapter updated via the API',
163 'name' => 'freshtag',
164 'value' => 'freshtagval',
170 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
173 $resp->assertStatus(200);
174 $resp->assertJson(array_merge($details, [
175 'id' => $chapter->id,
176 'slug' => $chapter->slug,
177 'book_id' => $chapter->book_id,
178 'description_html' => '<p>A chapter updated via the API</p>',
180 $this->assertActivityExists('chapter_update', $chapter);
183 public function test_update_endpoint_with_html()
185 $this->actingAsApiEditor();
186 $chapter = $this->entities->chapter();
188 'name' => 'My updated API chapter',
189 'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
192 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
193 $resp->assertStatus(200);
195 $this->assertDatabaseHas('chapters', array_merge($details, [
196 'id' => $chapter->id, 'description' => 'A chapter updated via the API'
200 public function test_update_increments_updated_date_if_only_tags_are_sent()
202 $this->actingAsApiEditor();
203 $chapter = $this->entities->chapter();
204 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
207 'tags' => [['name' => 'Category', 'value' => 'Testing']],
210 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
212 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
215 public function test_update_with_book_id_moves_chapter()
217 $this->actingAsApiEditor();
218 $chapter = $this->entities->chapterHasPages();
219 $page = $chapter->pages()->first();
220 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
222 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
226 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
227 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
230 public function test_update_with_new_book_id_requires_delete_permission()
232 $editor = $this->users->editor();
233 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
234 $this->actingAs($editor);
235 $chapter = $this->entities->chapterHasPages();
236 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
238 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
239 $this->assertPermissionError($resp);
242 public function test_delete_endpoint()
244 $this->actingAsApiEditor();
245 $chapter = $this->entities->chapter();
246 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
248 $resp->assertStatus(204);
249 $this->assertActivityExists('chapter_delete');
252 public function test_export_html_endpoint()
254 $this->actingAsApiEditor();
255 $chapter = $this->entities->chapter();
257 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
258 $resp->assertStatus(200);
259 $resp->assertSee($chapter->name);
260 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
263 public function test_export_plain_text_endpoint()
265 $this->actingAsApiEditor();
266 $chapter = $this->entities->chapter();
268 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
269 $resp->assertStatus(200);
270 $resp->assertSee($chapter->name);
271 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
274 public function test_export_pdf_endpoint()
276 $this->actingAsApiEditor();
277 $chapter = $this->entities->chapter();
279 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
280 $resp->assertStatus(200);
281 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
284 public function test_export_markdown_endpoint()
286 $this->actingAsApiEditor();
287 $chapter = Chapter::visible()->has('pages')->first();
289 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
290 $resp->assertStatus(200);
291 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
292 $resp->assertSee('# ' . $chapter->name);
293 $resp->assertSee('# ' . $chapter->pages()->first()->name);
296 public function test_cant_export_when_not_have_permission()
298 $types = ['html', 'plaintext', 'pdf', 'markdown'];
299 $this->actingAsApiEditor();
300 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
302 $chapter = Chapter::visible()->has('pages')->first();
303 foreach ($types as $type) {
304 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
305 $this->assertPermissionError($resp);