5 use BookStack\Entities\Models\Book;
7 use Illuminate\Support\Facades\DB;
10 class BooksApiTest extends TestCase
14 protected string $baseEndpoint = '/api/books';
16 public function test_index_endpoint_returns_expected_book()
18 $this->actingAsApiEditor();
19 $firstBook = Book::query()->orderBy('id', 'asc')->first();
21 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
22 $resp->assertJson(['data' => [
24 'id' => $firstBook->id,
25 'name' => $firstBook->name,
26 'slug' => $firstBook->slug,
31 public function test_create_endpoint()
33 $this->actingAsApiEditor();
35 'name' => 'My API book',
36 'description' => 'A book created via the API',
39 $resp = $this->postJson($this->baseEndpoint, $details);
40 $resp->assertStatus(200);
41 $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
42 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
43 $this->assertActivityExists('book_create', $newItem);
46 public function test_book_name_needed_to_create()
48 $this->actingAsApiEditor();
50 'description' => 'A book created via the API',
53 $resp = $this->postJson($this->baseEndpoint, $details);
54 $resp->assertStatus(422);
57 'message' => 'The given data was invalid.',
59 'name' => ['The name field is required.'],
66 public function test_read_endpoint()
68 $this->actingAsApiEditor();
69 $book = $this->entities->book();
71 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
73 $resp->assertStatus(200);
76 'slug' => $book->slug,
78 'name' => $book->createdBy->name,
81 'name' => $book->createdBy->name,
84 'name' => $book->ownedBy->name,
89 public function test_read_endpoint_includes_chapter_and_page_contents()
91 $this->actingAsApiEditor();
92 $book = $this->entities->bookHasChaptersAndPages();
93 $chapter = $book->chapters()->first();
94 $chapterPage = $chapter->pages()->first();
96 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
98 $directChildCount = $book->directPages()->count() + $book->chapters()->count();
99 $resp->assertStatus(200);
100 $resp->assertJsonCount($directChildCount, 'contents');
105 'id' => $chapter->id,
106 'name' => $chapter->name,
107 'slug' => $chapter->slug,
110 'id' => $chapterPage->id,
111 'name' => $chapterPage->name,
112 'slug' => $chapterPage->slug,
120 public function test_update_endpoint()
122 $this->actingAsApiEditor();
123 $book = $this->entities->book();
125 'name' => 'My updated API book',
126 'description' => 'A book created via the API',
129 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
132 $resp->assertStatus(200);
133 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
134 $this->assertActivityExists('book_update', $book);
137 public function test_update_increments_updated_date_if_only_tags_are_sent()
139 $this->actingAsApiEditor();
140 $book = $this->entities->book();
141 DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
144 'tags' => [['name' => 'Category', 'value' => 'Testing']],
147 $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
149 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
152 public function test_update_cover_image_control()
154 $this->actingAsApiEditor();
155 /** @var Book $book */
156 $book = $this->entities->book();
157 $this->assertNull($book->cover);
158 $file = $this->files->uploadedImage('image.png');
160 // Ensure cover image can be set via API
161 $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
162 'name' => 'My updated API book with image',
163 ], [], ['image' => $file]);
166 $resp->assertStatus(200);
167 $this->assertNotNull($book->cover);
169 // Ensure further updates without image do not clear cover image
170 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
171 'name' => 'My updated book again',
175 $resp->assertStatus(200);
176 $this->assertNotNull($book->cover);
178 // Ensure update with null image property clears image
179 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
184 $resp->assertStatus(200);
185 $this->assertNull($book->cover);
188 public function test_delete_endpoint()
190 $this->actingAsApiEditor();
191 $book = $this->entities->book();
192 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
194 $resp->assertStatus(204);
195 $this->assertActivityExists('book_delete');
198 public function test_export_html_endpoint()
200 $this->actingAsApiEditor();
201 $book = $this->entities->book();
203 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
204 $resp->assertStatus(200);
205 $resp->assertSee($book->name);
206 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
209 public function test_export_plain_text_endpoint()
211 $this->actingAsApiEditor();
212 $book = $this->entities->book();
214 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
215 $resp->assertStatus(200);
216 $resp->assertSee($book->name);
217 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
220 public function test_export_pdf_endpoint()
222 $this->actingAsApiEditor();
223 $book = $this->entities->book();
225 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
226 $resp->assertStatus(200);
227 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
230 public function test_export_markdown_endpoint()
232 $this->actingAsApiEditor();
233 $book = Book::visible()->has('pages')->has('chapters')->first();
235 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
236 $resp->assertStatus(200);
237 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
238 $resp->assertSee('# ' . $book->name);
239 $resp->assertSee('# ' . $book->pages()->first()->name);
240 $resp->assertSee('# ' . $book->chapters()->first()->name);
243 public function test_cant_export_when_not_have_permission()
245 $types = ['html', 'plaintext', 'pdf', 'markdown'];
246 $this->actingAsApiEditor();
247 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
249 $book = $this->entities->book();
250 foreach ($types as $type) {
251 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
252 $this->assertPermissionError($resp);