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, ['id' => $newItem->id, 'slug' => $newItem->slug]));
55 $this->assertDatabaseHas('tags', [
56 'entity_id' => $newItem->id,
57 'entity_type' => $newItem->getMorphClass(),
59 'value' => 'tagvalue',
61 $resp->assertJsonMissing(['pages' => []]);
62 $this->assertActivityExists('chapter_create', $newItem);
65 public function test_chapter_name_needed_to_create()
67 $this->actingAsApiEditor();
68 $book = $this->entities->book();
70 'book_id' => $book->id,
71 'description' => 'A chapter created via the API',
74 $resp = $this->postJson($this->baseEndpoint, $details);
75 $resp->assertStatus(422);
76 $resp->assertJson($this->validationResponse([
77 'name' => ['The name field is required.'],
81 public function test_chapter_book_id_needed_to_create()
83 $this->actingAsApiEditor();
85 'name' => 'My api chapter',
86 'description' => 'A chapter created via the API',
89 $resp = $this->postJson($this->baseEndpoint, $details);
90 $resp->assertStatus(422);
91 $resp->assertJson($this->validationResponse([
92 'book_id' => ['The book id field is required.'],
96 public function test_read_endpoint()
98 $this->actingAsApiEditor();
99 $chapter = $this->entities->chapter();
100 $page = $chapter->pages()->first();
102 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
103 $resp->assertStatus(200);
105 'id' => $chapter->id,
106 'slug' => $chapter->slug,
108 'name' => $chapter->createdBy->name,
110 'book_id' => $chapter->book_id,
112 'name' => $chapter->createdBy->name,
115 'name' => $chapter->ownedBy->name,
120 'slug' => $page->slug,
121 'name' => $page->name,
125 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
128 public function test_update_endpoint()
130 $this->actingAsApiEditor();
131 $chapter = $this->entities->chapter();
133 'name' => 'My updated API chapter',
134 'description' => 'A chapter created via the API',
137 'name' => 'freshtag',
138 'value' => 'freshtagval',
144 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
147 $resp->assertStatus(200);
148 $resp->assertJson(array_merge($details, [
149 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
151 $this->assertActivityExists('chapter_update', $chapter);
154 public function test_update_increments_updated_date_if_only_tags_are_sent()
156 $this->actingAsApiEditor();
157 $chapter = $this->entities->chapter();
158 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
161 'tags' => [['name' => 'Category', 'value' => 'Testing']],
164 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
166 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
169 public function test_update_with_book_id_moves_chapter()
171 $this->actingAsApiEditor();
172 $chapter = $this->entities->chapterHasPages();
173 $page = $chapter->pages()->first();
174 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
176 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
180 $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
181 $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
184 public function test_update_with_new_book_id_requires_delete_permission()
186 $editor = $this->users->editor();
187 $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
188 $this->actingAs($editor);
189 $chapter = $this->entities->chapterHasPages();
190 $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
192 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
193 $this->assertPermissionError($resp);
196 public function test_delete_endpoint()
198 $this->actingAsApiEditor();
199 $chapter = $this->entities->chapter();
200 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
202 $resp->assertStatus(204);
203 $this->assertActivityExists('chapter_delete');
206 public function test_export_html_endpoint()
208 $this->actingAsApiEditor();
209 $chapter = $this->entities->chapter();
211 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
212 $resp->assertStatus(200);
213 $resp->assertSee($chapter->name);
214 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
217 public function test_export_plain_text_endpoint()
219 $this->actingAsApiEditor();
220 $chapter = $this->entities->chapter();
222 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
223 $resp->assertStatus(200);
224 $resp->assertSee($chapter->name);
225 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
228 public function test_export_pdf_endpoint()
230 $this->actingAsApiEditor();
231 $chapter = $this->entities->chapter();
233 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
234 $resp->assertStatus(200);
235 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
238 public function test_export_markdown_endpoint()
240 $this->actingAsApiEditor();
241 $chapter = Chapter::visible()->has('pages')->first();
243 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
244 $resp->assertStatus(200);
245 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
246 $resp->assertSee('# ' . $chapter->name);
247 $resp->assertSee('# ' . $chapter->pages()->first()->name);
250 public function test_cant_export_when_not_have_permission()
252 $types = ['html', 'plaintext', 'pdf', 'markdown'];
253 $this->actingAsApiEditor();
254 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
256 $chapter = Chapter::visible()->has('pages')->first();
257 foreach ($types as $type) {
258 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
259 $this->assertPermissionError($resp);