5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
9 class ChaptersApiTest extends TestCase
13 protected $baseEndpoint = '/api/chapters';
15 public function test_index_endpoint_returns_expected_chapter()
17 $this->actingAsApiEditor();
18 $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
20 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
21 $resp->assertJson(['data' => [
23 'id' => $firstChapter->id,
24 'name' => $firstChapter->name,
25 'slug' => $firstChapter->slug,
26 'book_id' => $firstChapter->book->id,
27 'priority' => $firstChapter->priority,
32 public function test_create_endpoint()
34 $this->actingAsApiEditor();
35 $book = Book::query()->first();
37 'name' => 'My API chapter',
38 'description' => 'A chapter created via the API',
39 'book_id' => $book->id,
43 'value' => 'tagvalue',
48 $resp = $this->postJson($this->baseEndpoint, $details);
49 $resp->assertStatus(200);
50 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
51 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
52 $this->assertDatabaseHas('tags', [
53 'entity_id' => $newItem->id,
54 'entity_type' => $newItem->getMorphClass(),
56 'value' => 'tagvalue',
58 $resp->assertJsonMissing(['pages' => []]);
59 $this->assertActivityExists('chapter_create', $newItem);
62 public function test_chapter_name_needed_to_create()
64 $this->actingAsApiEditor();
65 $book = Book::query()->first();
67 'book_id' => $book->id,
68 'description' => 'A chapter created via the API',
71 $resp = $this->postJson($this->baseEndpoint, $details);
72 $resp->assertStatus(422);
73 $resp->assertJson($this->validationResponse([
74 'name' => ['The name field is required.'],
78 public function test_chapter_book_id_needed_to_create()
80 $this->actingAsApiEditor();
82 'name' => 'My api chapter',
83 'description' => 'A chapter created via the API',
86 $resp = $this->postJson($this->baseEndpoint, $details);
87 $resp->assertStatus(422);
88 $resp->assertJson($this->validationResponse([
89 'book_id' => ['The book id field is required.'],
93 public function test_read_endpoint()
95 $this->actingAsApiEditor();
96 $chapter = Chapter::visible()->first();
97 $page = $chapter->pages()->first();
99 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
100 $resp->assertStatus(200);
102 'id' => $chapter->id,
103 'slug' => $chapter->slug,
105 'name' => $chapter->createdBy->name,
107 'book_id' => $chapter->book_id,
109 'name' => $chapter->createdBy->name,
112 'name' => $chapter->ownedBy->name,
117 'slug' => $page->slug,
118 'name' => $page->name,
122 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
125 public function test_update_endpoint()
127 $this->actingAsApiEditor();
128 $chapter = Chapter::visible()->first();
130 'name' => 'My updated API chapter',
131 'description' => 'A chapter created via the API',
134 'name' => 'freshtag',
135 'value' => 'freshtagval',
140 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
143 $resp->assertStatus(200);
144 $resp->assertJson(array_merge($details, [
145 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
147 $this->assertActivityExists('chapter_update', $chapter);
150 public function test_delete_endpoint()
152 $this->actingAsApiEditor();
153 $chapter = Chapter::visible()->first();
154 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
156 $resp->assertStatus(204);
157 $this->assertActivityExists('chapter_delete');
160 public function test_export_html_endpoint()
162 $this->actingAsApiEditor();
163 $chapter = Chapter::visible()->first();
165 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
166 $resp->assertStatus(200);
167 $resp->assertSee($chapter->name);
168 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
171 public function test_export_plain_text_endpoint()
173 $this->actingAsApiEditor();
174 $chapter = Chapter::visible()->first();
176 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
177 $resp->assertStatus(200);
178 $resp->assertSee($chapter->name);
179 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
182 public function test_export_pdf_endpoint()
184 $this->actingAsApiEditor();
185 $chapter = Chapter::visible()->first();
187 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
188 $resp->assertStatus(200);
189 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
192 public function test_export_markdown_endpoint()
194 $this->actingAsApiEditor();
195 $chapter = Chapter::visible()->has('pages')->first();
197 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
198 $resp->assertStatus(200);
199 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
200 $resp->assertSee('# ' . $chapter->name);
201 $resp->assertSee('# ' . $chapter->pages()->first()->name);
204 public function test_cant_export_when_not_have_permission()
206 $types = ['html', 'plaintext', 'pdf', 'markdown'];
207 $this->actingAsApiEditor();
208 $this->removePermissionFromUser($this->getEditor(), 'content-export');
210 $chapter = Chapter::visible()->has('pages')->first();
211 foreach ($types as $type) {
212 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
213 $this->assertPermissionError($resp);