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();
38 $templatePage = $this->entities->templatePage();
40 'name' => 'My API chapter',
41 'description' => 'A chapter created via the API',
42 'book_id' => $book->id,
46 'value' => 'tagvalue',
50 'default_template_id' => $templatePage->id,
53 $resp = $this->postJson($this->baseEndpoint, $details);
54 $resp->assertStatus(200);
55 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
56 $resp->assertJson(array_merge($details, [
58 'slug' => $newItem->slug,
59 'description_html' => '<p>A chapter created via the API</p>',
61 $this->assertDatabaseHas('tags', [
62 'entity_id' => $newItem->id,
63 'entity_type' => $newItem->getMorphClass(),
65 'value' => 'tagvalue',
67 $resp->assertJsonMissing(['pages' => []]);
68 $this->assertActivityExists('chapter_create', $newItem);
71 public function test_create_endpoint_with_html()
73 $this->actingAsApiEditor();
74 $book = $this->entities->book();
76 'name' => 'My API chapter',
77 'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
78 'book_id' => $book->id,
81 $resp = $this->postJson($this->baseEndpoint, $details);
82 $resp->assertStatus(200);
83 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
85 $expectedDetails = array_merge($details, [
87 'description' => 'A chapter created via the API',
89 $resp->assertJson($expectedDetails);
90 $this->assertDatabaseHas('chapters', $expectedDetails);
93 public function test_chapter_name_needed_to_create()
95 $this->actingAsApiEditor();
96 $book = $this->entities->book();
98 'book_id' => $book->id,
99 'description' => 'A chapter created via the API',
102 $resp = $this->postJson($this->baseEndpoint, $details);
103 $resp->assertStatus(422);
104 $resp->assertJson($this->validationResponse([
105 'name' => ['The name field is required.'],
109 public function test_chapter_book_id_needed_to_create()
111 $this->actingAsApiEditor();
113 'name' => 'My api chapter',
114 'description' => 'A chapter created via the API',
117 $resp = $this->postJson($this->baseEndpoint, $details);
118 $resp->assertStatus(422);
119 $resp->assertJson($this->validationResponse([
120 'book_id' => ['The book id field is required.'],
124 public function test_read_endpoint()
126 $this->actingAsApiEditor();
127 $chapter = $this->entities->chapter();
128 $page = $chapter->pages()->first();
130 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
131 $resp->assertStatus(200);
133 'id' => $chapter->id,
134 'slug' => $chapter->slug,
136 'name' => $chapter->createdBy->name,
138 'book_id' => $chapter->book_id,
140 'name' => $chapter->createdBy->name,
143 'name' => $chapter->ownedBy->name,
148 'slug' => $page->slug,
149 'name' => $page->name,
152 'default_template_id' => null,
154 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
157 public function test_update_endpoint()
159 $this->actingAsApiEditor();
160 $chapter = $this->entities->chapter();
161 $templatePage = $this->entities->templatePage();
163 'name' => 'My updated API chapter',
164 'description' => 'A chapter updated via the API',
167 'name' => 'freshtag',
168 'value' => 'freshtagval',
172 'default_template_id' => $templatePage->id,
175 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
178 $resp->assertStatus(200);
179 $resp->assertJson(array_merge($details, [
180 'id' => $chapter->id,
181 'slug' => $chapter->slug,
182 'book_id' => $chapter->book_id,
183 'description_html' => '<p>A chapter updated via the API</p>',
185 $this->assertActivityExists('chapter_update', $chapter);
188 public function test_update_endpoint_with_html()
190 $this->actingAsApiEditor();
191 $chapter = $this->entities->chapter();
193 'name' => 'My updated API chapter',
194 'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
197 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
198 $resp->assertStatus(200);
200 $this->assertDatabaseHas('chapters', array_merge($details, [
201 'id' => $chapter->id, 'description' => 'A chapter updated via the API'
205 public function test_update_increments_updated_date_if_only_tags_are_sent()
207 $this->actingAsApiEditor();
208 $chapter = $this->entities->chapter();
209 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
212 'tags' => [['name' => 'Category', 'value' => 'Testing']],
215 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
217 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
220 public function test_update_with_book_id_moves_chapter()
222 $this->actingAsApiEditor();
223 $chapter = $this->entities->chapterHasPages();
224 $page = $chapter->pages()->first();
225 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
227 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
231 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
232 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
235 public function test_update_with_new_book_id_requires_delete_permission()
237 $editor = $this->users->editor();
238 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
239 $this->actingAs($editor);
240 $chapter = $this->entities->chapterHasPages();
241 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
243 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
244 $this->assertPermissionError($resp);
247 public function test_delete_endpoint()
249 $this->actingAsApiEditor();
250 $chapter = $this->entities->chapter();
251 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
253 $resp->assertStatus(204);
254 $this->assertActivityExists('chapter_delete');
257 public function test_export_html_endpoint()
259 $this->actingAsApiEditor();
260 $chapter = $this->entities->chapter();
262 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
263 $resp->assertStatus(200);
264 $resp->assertSee($chapter->name);
265 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
268 public function test_export_plain_text_endpoint()
270 $this->actingAsApiEditor();
271 $chapter = $this->entities->chapter();
273 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
274 $resp->assertStatus(200);
275 $resp->assertSee($chapter->name);
276 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
279 public function test_export_pdf_endpoint()
281 $this->actingAsApiEditor();
282 $chapter = $this->entities->chapter();
284 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
285 $resp->assertStatus(200);
286 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
289 public function test_export_markdown_endpoint()
291 $this->actingAsApiEditor();
292 $chapter = Chapter::visible()->has('pages')->first();
294 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
295 $resp->assertStatus(200);
296 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
297 $resp->assertSee('# ' . $chapter->name);
298 $resp->assertSee('# ' . $chapter->pages()->first()->name);
301 public function test_cant_export_when_not_have_permission()
303 $types = ['html', 'plaintext', 'pdf', 'markdown'];
304 $this->actingAsApiEditor();
305 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
307 $chapter = Chapter::visible()->has('pages')->first();
308 foreach ($types as $type) {
309 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
310 $this->assertPermissionError($resp);