5 use BookStack\Entities\Models\Book;
7 use Illuminate\Support\Facades\DB;
9 use Tests\Uploads\UsesImages;
11 class BooksApiTest extends TestCase
16 protected string $baseEndpoint = '/api/books';
18 public function test_index_endpoint_returns_expected_book()
20 $this->actingAsApiEditor();
21 $firstBook = Book::query()->orderBy('id', 'asc')->first();
23 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
24 $resp->assertJson(['data' => [
26 'id' => $firstBook->id,
27 'name' => $firstBook->name,
28 'slug' => $firstBook->slug,
33 public function test_create_endpoint()
35 $this->actingAsApiEditor();
37 'name' => 'My API book',
38 'description' => 'A book created via the API',
41 $resp = $this->postJson($this->baseEndpoint, $details);
42 $resp->assertStatus(200);
43 $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
44 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
45 $this->assertActivityExists('book_create', $newItem);
48 public function test_book_name_needed_to_create()
50 $this->actingAsApiEditor();
52 'description' => 'A book created via the API',
55 $resp = $this->postJson($this->baseEndpoint, $details);
56 $resp->assertStatus(422);
59 'message' => 'The given data was invalid.',
61 'name' => ['The name field is required.'],
68 public function test_read_endpoint()
70 $this->actingAsApiEditor();
71 $book = $this->entities->book();
73 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
75 $resp->assertStatus(200);
78 'slug' => $book->slug,
80 'name' => $book->createdBy->name,
83 'name' => $book->createdBy->name,
86 'name' => $book->ownedBy->name,
91 public function test_read_endpoint_includes_chapter_and_page_contents()
93 $this->actingAsApiEditor();
94 $book = $this->entities->bookHasChaptersAndPages();
95 $chapter = $book->chapters()->first();
96 $chapterPage = $chapter->pages()->first();
98 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
100 $directChildCount = $book->directPages()->count() + $book->chapters()->count();
101 $resp->assertStatus(200);
102 $resp->assertJsonCount($directChildCount, 'contents');
107 'id' => $chapter->id,
108 'name' => $chapter->name,
109 'slug' => $chapter->slug,
112 'id' => $chapterPage->id,
113 'name' => $chapterPage->name,
114 'slug' => $chapterPage->slug,
122 public function test_update_endpoint()
124 $this->actingAsApiEditor();
125 $book = $this->entities->book();
127 'name' => 'My updated API book',
128 'description' => 'A book created via the API',
131 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
134 $resp->assertStatus(200);
135 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
136 $this->assertActivityExists('book_update', $book);
139 public function test_update_increments_updated_date_if_only_tags_are_sent()
141 $this->actingAsApiEditor();
142 $book = $this->entities->book();
143 DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
146 'tags' => [['name' => 'Category', 'value' => 'Testing']],
149 $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
151 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
154 public function test_update_cover_image_control()
156 $this->actingAsApiEditor();
157 /** @var Book $book */
158 $book = $this->entities->book();
159 $this->assertNull($book->cover);
160 $file = $this->getTestImage('image.png');
162 // Ensure cover image can be set via API
163 $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
164 'name' => 'My updated API book with image',
165 ], [], ['image' => $file]);
168 $resp->assertStatus(200);
169 $this->assertNotNull($book->cover);
171 // Ensure further updates without image do not clear cover image
172 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
173 'name' => 'My updated book again',
177 $resp->assertStatus(200);
178 $this->assertNotNull($book->cover);
180 // Ensure update with null image property clears image
181 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
186 $resp->assertStatus(200);
187 $this->assertNull($book->cover);
190 public function test_delete_endpoint()
192 $this->actingAsApiEditor();
193 $book = $this->entities->book();
194 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
196 $resp->assertStatus(204);
197 $this->assertActivityExists('book_delete');
200 public function test_export_html_endpoint()
202 $this->actingAsApiEditor();
203 $book = $this->entities->book();
205 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
206 $resp->assertStatus(200);
207 $resp->assertSee($book->name);
208 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
211 public function test_export_plain_text_endpoint()
213 $this->actingAsApiEditor();
214 $book = $this->entities->book();
216 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
217 $resp->assertStatus(200);
218 $resp->assertSee($book->name);
219 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
222 public function test_export_pdf_endpoint()
224 $this->actingAsApiEditor();
225 $book = $this->entities->book();
227 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
228 $resp->assertStatus(200);
229 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
232 public function test_export_markdown_endpoint()
234 $this->actingAsApiEditor();
235 $book = Book::visible()->has('pages')->has('chapters')->first();
237 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
238 $resp->assertStatus(200);
239 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
240 $resp->assertSee('# ' . $book->name);
241 $resp->assertSee('# ' . $book->pages()->first()->name);
242 $resp->assertSee('# ' . $book->chapters()->first()->name);
245 public function test_cant_export_when_not_have_permission()
247 $types = ['html', 'plaintext', 'pdf', 'markdown'];
248 $this->actingAsApiEditor();
249 $this->removePermissionFromUser($this->getEditor(), 'content-export');
251 $book = $this->entities->book();
252 foreach ($types as $type) {
253 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
254 $this->assertPermissionError($resp);