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,
31 'owned_by' => $firstChapter->owned_by,
32 'created_by' => $firstChapter->created_by,
33 'updated_by' => $firstChapter->updated_by,
38 public function test_create_endpoint()
40 $this->actingAsApiEditor();
41 $book = $this->entities->book();
42 $templatePage = $this->entities->templatePage();
44 'name' => 'My API chapter',
45 'description' => 'A chapter created via the API',
46 'book_id' => $book->id,
50 'value' => 'tagvalue',
54 'default_template_id' => $templatePage->id,
57 $resp = $this->postJson($this->baseEndpoint, $details);
58 $resp->assertStatus(200);
59 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
60 $resp->assertJson(array_merge($details, [
62 'slug' => $newItem->slug,
63 'description_html' => '<p>A chapter created via the API</p>',
65 $this->assertDatabaseHas('tags', [
66 'entity_id' => $newItem->id,
67 'entity_type' => $newItem->getMorphClass(),
69 'value' => 'tagvalue',
71 $resp->assertJsonMissing(['pages' => []]);
72 $this->assertActivityExists('chapter_create', $newItem);
75 public function test_create_endpoint_with_html()
77 $this->actingAsApiEditor();
78 $book = $this->entities->book();
80 'name' => 'My API chapter',
81 'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
82 'book_id' => $book->id,
85 $resp = $this->postJson($this->baseEndpoint, $details);
86 $resp->assertStatus(200);
87 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
89 $expectedDetails = array_merge($details, [
91 'description' => 'A chapter created via the API',
93 $resp->assertJson($expectedDetails);
94 $this->assertDatabaseHas('chapters', $expectedDetails);
97 public function test_chapter_name_needed_to_create()
99 $this->actingAsApiEditor();
100 $book = $this->entities->book();
102 'book_id' => $book->id,
103 'description' => 'A chapter created via the API',
106 $resp = $this->postJson($this->baseEndpoint, $details);
107 $resp->assertStatus(422);
108 $resp->assertJson($this->validationResponse([
109 'name' => ['The name field is required.'],
113 public function test_chapter_book_id_needed_to_create()
115 $this->actingAsApiEditor();
117 'name' => 'My api chapter',
118 'description' => 'A chapter created via the API',
121 $resp = $this->postJson($this->baseEndpoint, $details);
122 $resp->assertStatus(422);
123 $resp->assertJson($this->validationResponse([
124 'book_id' => ['The book id field is required.'],
128 public function test_read_endpoint()
130 $this->actingAsApiEditor();
131 $chapter = $this->entities->chapter();
132 $page = $chapter->pages()->first();
134 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
135 $resp->assertStatus(200);
137 'id' => $chapter->id,
138 'slug' => $chapter->slug,
139 'book_slug' => $chapter->book->slug,
141 'name' => $chapter->createdBy->name,
143 'book_id' => $chapter->book_id,
145 'name' => $chapter->createdBy->name,
148 'name' => $chapter->ownedBy->name,
153 'slug' => $page->slug,
154 'name' => $page->name,
155 'owned_by' => $page->owned_by,
156 'created_by' => $page->created_by,
157 'updated_by' => $page->updated_by,
158 'book_id' => $page->id,
159 'chapter_id' => $chapter->id,
160 'priority' => $page->priority,
161 'book_slug' => $chapter->book->slug,
162 'draft' => $page->draft,
163 'template' => $page->template,
164 'editor' => $page->editor,
167 'default_template_id' => null,
169 $resp->assertJsonMissingPath('book');
170 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
173 public function test_update_endpoint()
175 $this->actingAsApiEditor();
176 $chapter = $this->entities->chapter();
177 $templatePage = $this->entities->templatePage();
179 'name' => 'My updated API chapter',
180 'description' => 'A chapter updated via the API',
183 'name' => 'freshtag',
184 'value' => 'freshtagval',
188 'default_template_id' => $templatePage->id,
191 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
194 $resp->assertStatus(200);
195 $resp->assertJson(array_merge($details, [
196 'id' => $chapter->id,
197 'slug' => $chapter->slug,
198 'book_id' => $chapter->book_id,
199 'description_html' => '<p>A chapter updated via the API</p>',
201 $this->assertActivityExists('chapter_update', $chapter);
204 public function test_update_endpoint_with_html()
206 $this->actingAsApiEditor();
207 $chapter = $this->entities->chapter();
209 'name' => 'My updated API chapter',
210 'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
213 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
214 $resp->assertStatus(200);
216 $this->assertDatabaseHas('chapters', array_merge($details, [
217 'id' => $chapter->id, 'description' => 'A chapter updated via the API'
221 public function test_update_increments_updated_date_if_only_tags_are_sent()
223 $this->actingAsApiEditor();
224 $chapter = $this->entities->chapter();
225 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
228 'tags' => [['name' => 'Category', 'value' => 'Testing']],
231 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
233 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
236 public function test_update_with_book_id_moves_chapter()
238 $this->actingAsApiEditor();
239 $chapter = $this->entities->chapterHasPages();
240 $page = $chapter->pages()->first();
241 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
243 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
247 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
248 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
251 public function test_update_with_new_book_id_requires_delete_permission()
253 $editor = $this->users->editor();
254 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
255 $this->actingAs($editor);
256 $chapter = $this->entities->chapterHasPages();
257 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
259 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
260 $this->assertPermissionError($resp);
263 public function test_delete_endpoint()
265 $this->actingAsApiEditor();
266 $chapter = $this->entities->chapter();
267 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
269 $resp->assertStatus(204);
270 $this->assertActivityExists('chapter_delete');
273 public function test_export_html_endpoint()
275 $this->actingAsApiEditor();
276 $chapter = $this->entities->chapter();
278 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
279 $resp->assertStatus(200);
280 $resp->assertSee($chapter->name);
281 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
284 public function test_export_plain_text_endpoint()
286 $this->actingAsApiEditor();
287 $chapter = $this->entities->chapter();
289 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
290 $resp->assertStatus(200);
291 $resp->assertSee($chapter->name);
292 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
295 public function test_export_pdf_endpoint()
297 $this->actingAsApiEditor();
298 $chapter = $this->entities->chapter();
300 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
301 $resp->assertStatus(200);
302 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
305 public function test_export_markdown_endpoint()
307 $this->actingAsApiEditor();
308 $chapter = Chapter::visible()->has('pages')->first();
310 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
311 $resp->assertStatus(200);
312 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
313 $resp->assertSee('# ' . $chapter->name);
314 $resp->assertSee('# ' . $chapter->pages()->first()->name);
317 public function test_cant_export_when_not_have_permission()
319 $types = ['html', 'plaintext', 'pdf', 'markdown'];
320 $this->actingAsApiEditor();
321 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
323 $chapter = Chapter::visible()->has('pages')->first();
324 foreach ($types as $type) {
325 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
326 $this->assertPermissionError($resp);