5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
8 use Illuminate\Support\Facades\DB;
11 class ShelvesApiTest extends TestCase
15 protected string $baseEndpoint = '/api/shelves';
17 public function test_index_endpoint_returns_expected_shelf()
19 $this->actingAsApiEditor();
20 $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
22 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23 $resp->assertJson(['data' => [
25 'id' => $firstBookshelf->id,
26 'name' => $firstBookshelf->name,
27 'slug' => $firstBookshelf->slug,
32 public function test_create_endpoint()
34 $this->actingAsApiEditor();
35 $books = Book::query()->take(2)->get();
38 'name' => 'My API shelf',
39 'description' => 'A shelf created via the API',
42 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
43 $resp->assertStatus(200);
44 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
45 $resp->assertJson(array_merge($details, [
47 'slug' => $newItem->slug,
48 'description_html' => '<p>A shelf created via the API</p>',
50 $this->assertActivityExists('bookshelf_create', $newItem);
51 foreach ($books as $index => $book) {
52 $this->assertDatabaseHas('bookshelves_books', [
53 'bookshelf_id' => $newItem->id,
54 'book_id' => $book->id,
60 public function test_create_endpoint_with_html()
62 $this->actingAsApiEditor();
65 'name' => 'My API shelf',
66 'description_html' => '<p>A <strong>shelf</strong> created via the API</p>',
69 $resp = $this->postJson($this->baseEndpoint, $details);
70 $resp->assertStatus(200);
71 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
73 $expectedDetails = array_merge($details, [
75 'description' => 'A shelf created via the API',
78 $resp->assertJson($expectedDetails);
79 $this->assertDatabaseHas('bookshelves', $expectedDetails);
82 public function test_shelf_name_needed_to_create()
84 $this->actingAsApiEditor();
86 'description' => 'A shelf created via the API',
89 $resp = $this->postJson($this->baseEndpoint, $details);
90 $resp->assertStatus(422);
93 'message' => 'The given data was invalid.',
95 'name' => ['The name field is required.'],
102 public function test_read_endpoint()
104 $this->actingAsApiEditor();
105 $shelf = Bookshelf::visible()->first();
107 $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
109 $resp->assertStatus(200);
112 'slug' => $shelf->slug,
114 'name' => $shelf->createdBy->name,
117 'name' => $shelf->createdBy->name,
120 'name' => $shelf->ownedBy->name,
125 public function test_update_endpoint()
127 $this->actingAsApiEditor();
128 $shelf = Bookshelf::visible()->first();
130 'name' => 'My updated API shelf',
131 'description' => 'A shelf updated via the API',
134 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
137 $resp->assertStatus(200);
138 $resp->assertJson(array_merge($details, [
140 'slug' => $shelf->slug,
141 'description_html' => '<p>A shelf updated via the API</p>',
143 $this->assertActivityExists('bookshelf_update', $shelf);
146 public function test_update_endpoint_with_html()
148 $this->actingAsApiEditor();
149 $shelf = Bookshelf::visible()->first();
151 'name' => 'My updated API shelf',
152 'description_html' => '<p>A shelf <em>updated</em> via the API</p>',
155 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
156 $resp->assertStatus(200);
158 $this->assertDatabaseHas('bookshelves', array_merge($details, ['id' => $shelf->id, 'description' => 'A shelf updated via the API']));
161 public function test_update_increments_updated_date_if_only_tags_are_sent()
163 $this->actingAsApiEditor();
164 $shelf = Bookshelf::visible()->first();
165 DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
168 'tags' => [['name' => 'Category', 'value' => 'Testing']],
171 $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
173 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
176 public function test_update_only_assigns_books_if_param_provided()
178 $this->actingAsApiEditor();
179 $shelf = Bookshelf::visible()->first();
180 $this->assertTrue($shelf->books()->count() > 0);
182 'name' => 'My updated API shelf',
185 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
186 $resp->assertStatus(200);
187 $this->assertTrue($shelf->books()->count() > 0);
189 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
190 $resp->assertStatus(200);
191 $this->assertTrue($shelf->books()->count() === 0);
194 public function test_update_cover_image_control()
196 $this->actingAsApiEditor();
197 /** @var Book $shelf */
198 $shelf = Bookshelf::visible()->first();
199 $this->assertNull($shelf->cover);
200 $file = $this->files->uploadedImage('image.png');
202 // Ensure cover image can be set via API
203 $resp = $this->call('PUT', $this->baseEndpoint . "/{$shelf->id}", [
204 'name' => 'My updated API shelf with image',
205 ], [], ['image' => $file]);
208 $resp->assertStatus(200);
209 $this->assertNotNull($shelf->cover);
211 // Ensure further updates without image do not clear cover image
212 $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
213 'name' => 'My updated shelf again',
217 $resp->assertStatus(200);
218 $this->assertNotNull($shelf->cover);
220 // Ensure update with null image property clears image
221 $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
226 $resp->assertStatus(200);
227 $this->assertNull($shelf->cover);
230 public function test_delete_endpoint()
232 $this->actingAsApiEditor();
233 $shelf = Bookshelf::visible()->first();
234 $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
236 $resp->assertStatus(204);
237 $this->assertActivityExists('bookshelf_delete');