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,
30 'book_slug' => $firstChapter->book->slug,
35 public function test_create_endpoint()
37 $this->actingAsApiEditor();
38 $book = $this->entities->book();
39 $templatePage = $this->entities->templatePage();
41 'name' => 'My API chapter',
42 'description' => 'A chapter created via the API',
43 'book_id' => $book->id,
47 'value' => 'tagvalue',
51 'default_template_id' => $templatePage->id,
54 $resp = $this->postJson($this->baseEndpoint, $details);
55 $resp->assertStatus(200);
56 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
57 $resp->assertJson(array_merge($details, [
59 'slug' => $newItem->slug,
60 'description_html' => '<p>A chapter created via the API</p>',
62 $this->assertDatabaseHas('tags', [
63 'entity_id' => $newItem->id,
64 'entity_type' => $newItem->getMorphClass(),
66 'value' => 'tagvalue',
68 $resp->assertJsonMissing(['pages' => []]);
69 $this->assertActivityExists('chapter_create', $newItem);
72 public function test_create_endpoint_with_html()
74 $this->actingAsApiEditor();
75 $book = $this->entities->book();
77 'name' => 'My API chapter',
78 'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
79 'book_id' => $book->id,
82 $resp = $this->postJson($this->baseEndpoint, $details);
83 $resp->assertStatus(200);
84 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
86 $expectedDetails = array_merge($details, [
88 'description' => 'A chapter created via the API',
90 $resp->assertJson($expectedDetails);
91 $this->assertDatabaseHas('chapters', $expectedDetails);
94 public function test_chapter_name_needed_to_create()
96 $this->actingAsApiEditor();
97 $book = $this->entities->book();
99 'book_id' => $book->id,
100 'description' => 'A chapter created via the API',
103 $resp = $this->postJson($this->baseEndpoint, $details);
104 $resp->assertStatus(422);
105 $resp->assertJson($this->validationResponse([
106 'name' => ['The name field is required.'],
110 public function test_chapter_book_id_needed_to_create()
112 $this->actingAsApiEditor();
114 'name' => 'My api chapter',
115 'description' => 'A chapter created via the API',
118 $resp = $this->postJson($this->baseEndpoint, $details);
119 $resp->assertStatus(422);
120 $resp->assertJson($this->validationResponse([
121 'book_id' => ['The book id field is required.'],
125 public function test_read_endpoint()
127 $this->actingAsApiEditor();
128 $chapter = $this->entities->chapter();
129 $page = $chapter->pages()->first();
131 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
132 $resp->assertStatus(200);
134 'id' => $chapter->id,
135 'slug' => $chapter->slug,
136 'book_slug' => $chapter->book->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,
154 'default_template_id' => null,
156 $resp->assertJsonMissingPath('book');
157 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
160 public function test_update_endpoint()
162 $this->actingAsApiEditor();
163 $chapter = $this->entities->chapter();
164 $templatePage = $this->entities->templatePage();
166 'name' => 'My updated API chapter',
167 'description' => 'A chapter updated via the API',
170 'name' => 'freshtag',
171 'value' => 'freshtagval',
175 'default_template_id' => $templatePage->id,
178 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
181 $resp->assertStatus(200);
182 $resp->assertJson(array_merge($details, [
183 'id' => $chapter->id,
184 'slug' => $chapter->slug,
185 'book_id' => $chapter->book_id,
186 'description_html' => '<p>A chapter updated via the API</p>',
188 $this->assertActivityExists('chapter_update', $chapter);
191 public function test_update_endpoint_with_html()
193 $this->actingAsApiEditor();
194 $chapter = $this->entities->chapter();
196 'name' => 'My updated API chapter',
197 'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
200 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
201 $resp->assertStatus(200);
203 $this->assertDatabaseHas('chapters', array_merge($details, [
204 'id' => $chapter->id, 'description' => 'A chapter updated via the API'
208 public function test_update_increments_updated_date_if_only_tags_are_sent()
210 $this->actingAsApiEditor();
211 $chapter = $this->entities->chapter();
212 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
215 'tags' => [['name' => 'Category', 'value' => 'Testing']],
218 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
220 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
223 public function test_update_with_book_id_moves_chapter()
225 $this->actingAsApiEditor();
226 $chapter = $this->entities->chapterHasPages();
227 $page = $chapter->pages()->first();
228 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
230 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
234 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
235 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
238 public function test_update_with_new_book_id_requires_delete_permission()
240 $editor = $this->users->editor();
241 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
242 $this->actingAs($editor);
243 $chapter = $this->entities->chapterHasPages();
244 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
246 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
247 $this->assertPermissionError($resp);
250 public function test_delete_endpoint()
252 $this->actingAsApiEditor();
253 $chapter = $this->entities->chapter();
254 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
256 $resp->assertStatus(204);
257 $this->assertActivityExists('chapter_delete');
260 public function test_export_html_endpoint()
262 $this->actingAsApiEditor();
263 $chapter = $this->entities->chapter();
265 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
266 $resp->assertStatus(200);
267 $resp->assertSee($chapter->name);
268 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
271 public function test_export_plain_text_endpoint()
273 $this->actingAsApiEditor();
274 $chapter = $this->entities->chapter();
276 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
277 $resp->assertStatus(200);
278 $resp->assertSee($chapter->name);
279 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
282 public function test_export_pdf_endpoint()
284 $this->actingAsApiEditor();
285 $chapter = $this->entities->chapter();
287 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
288 $resp->assertStatus(200);
289 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
292 public function test_export_markdown_endpoint()
294 $this->actingAsApiEditor();
295 $chapter = Chapter::visible()->has('pages')->first();
297 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
298 $resp->assertStatus(200);
299 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
300 $resp->assertSee('# ' . $chapter->name);
301 $resp->assertSee('# ' . $chapter->pages()->first()->name);
304 public function test_cant_export_when_not_have_permission()
306 $types = ['html', 'plaintext', 'pdf', 'markdown'];
307 $this->actingAsApiEditor();
308 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
310 $chapter = Chapter::visible()->has('pages')->first();
311 foreach ($types as $type) {
312 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
313 $this->assertPermissionError($resp);