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 = Book::query()->first();
39 'name' => 'My API chapter',
40 'description' => 'A chapter created via the API',
41 'book_id' => $book->id,
45 'value' => 'tagvalue',
50 $resp = $this->postJson($this->baseEndpoint, $details);
51 $resp->assertStatus(200);
52 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
53 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
54 $this->assertDatabaseHas('tags', [
55 'entity_id' => $newItem->id,
56 'entity_type' => $newItem->getMorphClass(),
58 'value' => 'tagvalue',
60 $resp->assertJsonMissing(['pages' => []]);
61 $this->assertActivityExists('chapter_create', $newItem);
64 public function test_chapter_name_needed_to_create()
66 $this->actingAsApiEditor();
67 $book = Book::query()->first();
69 'book_id' => $book->id,
70 'description' => 'A chapter created via the API',
73 $resp = $this->postJson($this->baseEndpoint, $details);
74 $resp->assertStatus(422);
75 $resp->assertJson($this->validationResponse([
76 'name' => ['The name field is required.'],
80 public function test_chapter_book_id_needed_to_create()
82 $this->actingAsApiEditor();
84 'name' => 'My api chapter',
85 'description' => 'A chapter created via the API',
88 $resp = $this->postJson($this->baseEndpoint, $details);
89 $resp->assertStatus(422);
90 $resp->assertJson($this->validationResponse([
91 'book_id' => ['The book id field is required.'],
95 public function test_read_endpoint()
97 $this->actingAsApiEditor();
98 $chapter = Chapter::visible()->first();
99 $page = $chapter->pages()->first();
101 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
102 $resp->assertStatus(200);
104 'id' => $chapter->id,
105 'slug' => $chapter->slug,
107 'name' => $chapter->createdBy->name,
109 'book_id' => $chapter->book_id,
111 'name' => $chapter->createdBy->name,
114 'name' => $chapter->ownedBy->name,
119 'slug' => $page->slug,
120 'name' => $page->name,
124 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
127 public function test_update_endpoint()
129 $this->actingAsApiEditor();
130 $chapter = Chapter::visible()->first();
132 'name' => 'My updated API chapter',
133 'description' => 'A chapter created via the API',
136 'name' => 'freshtag',
137 'value' => 'freshtagval',
142 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
145 $resp->assertStatus(200);
146 $resp->assertJson(array_merge($details, [
147 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
149 $this->assertActivityExists('chapter_update', $chapter);
152 public function test_update_increments_updated_date_if_only_tags_are_sent()
154 $this->actingAsApiEditor();
155 $chapter = Chapter::visible()->first();
156 DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
159 'tags' => [['name' => 'Category', 'value' => 'Testing']],
162 $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
164 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
167 public function test_delete_endpoint()
169 $this->actingAsApiEditor();
170 $chapter = Chapter::visible()->first();
171 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
173 $resp->assertStatus(204);
174 $this->assertActivityExists('chapter_delete');
177 public function test_export_html_endpoint()
179 $this->actingAsApiEditor();
180 $chapter = Chapter::visible()->first();
182 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
183 $resp->assertStatus(200);
184 $resp->assertSee($chapter->name);
185 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
188 public function test_export_plain_text_endpoint()
190 $this->actingAsApiEditor();
191 $chapter = Chapter::visible()->first();
193 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
194 $resp->assertStatus(200);
195 $resp->assertSee($chapter->name);
196 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
199 public function test_export_pdf_endpoint()
201 $this->actingAsApiEditor();
202 $chapter = Chapter::visible()->first();
204 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
205 $resp->assertStatus(200);
206 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
209 public function test_export_markdown_endpoint()
211 $this->actingAsApiEditor();
212 $chapter = Chapter::visible()->has('pages')->first();
214 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
215 $resp->assertStatus(200);
216 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
217 $resp->assertSee('# ' . $chapter->name);
218 $resp->assertSee('# ' . $chapter->pages()->first()->name);
221 public function test_cant_export_when_not_have_permission()
223 $types = ['html', 'plaintext', 'pdf', 'markdown'];
224 $this->actingAsApiEditor();
225 $this->removePermissionFromUser($this->getEditor(), 'content-export');
227 $chapter = Chapter::visible()->has('pages')->first();
228 foreach ($types as $type) {
229 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
230 $this->assertPermissionError($resp);