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 = Book::visible()->first();
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_update_endpoint()
93 $this->actingAsApiEditor();
94 $book = Book::visible()->first();
96 'name' => 'My updated API book',
97 'description' => 'A book created via the API',
100 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
103 $resp->assertStatus(200);
104 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
105 $this->assertActivityExists('book_update', $book);
108 public function test_update_increments_updated_date_if_only_tags_are_sent()
110 $this->actingAsApiEditor();
111 $book = Book::visible()->first();
112 DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
115 'tags' => [['name' => 'Category', 'value' => 'Testing']],
118 $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
120 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
123 public function test_update_cover_image_control()
125 $this->actingAsApiEditor();
126 /** @var Book $book */
127 $book = Book::visible()->first();
128 $this->assertNull($book->cover);
129 $file = $this->getTestImage('image.png');
131 // Ensure cover image can be set via API
132 $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
133 'name' => 'My updated API book with image',
134 ], [], ['image' => $file]);
137 $resp->assertStatus(200);
138 $this->assertNotNull($book->cover);
140 // Ensure further updates without image do not clear cover image
141 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
142 'name' => 'My updated book again',
146 $resp->assertStatus(200);
147 $this->assertNotNull($book->cover);
149 // Ensure update with null image property clears image
150 $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
155 $resp->assertStatus(200);
156 $this->assertNull($book->cover);
159 public function test_delete_endpoint()
161 $this->actingAsApiEditor();
162 $book = Book::visible()->first();
163 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
165 $resp->assertStatus(204);
166 $this->assertActivityExists('book_delete');
169 public function test_export_html_endpoint()
171 $this->actingAsApiEditor();
172 $book = Book::visible()->first();
174 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
175 $resp->assertStatus(200);
176 $resp->assertSee($book->name);
177 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
180 public function test_export_plain_text_endpoint()
182 $this->actingAsApiEditor();
183 $book = Book::visible()->first();
185 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
186 $resp->assertStatus(200);
187 $resp->assertSee($book->name);
188 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
191 public function test_export_pdf_endpoint()
193 $this->actingAsApiEditor();
194 $book = Book::visible()->first();
196 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
197 $resp->assertStatus(200);
198 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
201 public function test_export_markdown_endpoint()
203 $this->actingAsApiEditor();
204 $book = Book::visible()->has('pages')->has('chapters')->first();
206 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
207 $resp->assertStatus(200);
208 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
209 $resp->assertSee('# ' . $book->name);
210 $resp->assertSee('# ' . $book->pages()->first()->name);
211 $resp->assertSee('# ' . $book->chapters()->first()->name);
214 public function test_cant_export_when_not_have_permission()
216 $types = ['html', 'plaintext', 'pdf', 'markdown'];
217 $this->actingAsApiEditor();
218 $this->removePermissionFromUser($this->getEditor(), 'content-export');
220 $book = Book::visible()->first();
221 foreach ($types as $type) {
222 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
223 $this->assertPermissionError($resp);