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,
28 'owned_by' => $firstBookshelf->owned_by,
29 'created_by' => $firstBookshelf->created_by,
30 'updated_by' => $firstBookshelf->updated_by,
35 public function test_create_endpoint()
37 $this->actingAsApiEditor();
38 $books = Book::query()->take(2)->get();
41 'name' => 'My API shelf',
42 'description' => 'A shelf created via the API',
45 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
46 $resp->assertStatus(200);
47 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
48 $resp->assertJson(array_merge($details, [
50 'slug' => $newItem->slug,
51 'description_html' => '<p>A shelf created via the API</p>',
53 $this->assertActivityExists('bookshelf_create', $newItem);
54 foreach ($books as $index => $book) {
55 $this->assertDatabaseHas('bookshelves_books', [
56 'bookshelf_id' => $newItem->id,
57 'book_id' => $book->id,
63 public function test_create_endpoint_with_html()
65 $this->actingAsApiEditor();
68 'name' => 'My API shelf',
69 'description_html' => '<p>A <strong>shelf</strong> created via the API</p>',
72 $resp = $this->postJson($this->baseEndpoint, $details);
73 $resp->assertStatus(200);
74 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
76 $expectedDetails = array_merge($details, [
78 'description' => 'A shelf created via the API',
81 $resp->assertJson($expectedDetails);
82 $this->assertDatabaseHas('bookshelves', $expectedDetails);
85 public function test_shelf_name_needed_to_create()
87 $this->actingAsApiEditor();
89 'description' => 'A shelf created via the API',
92 $resp = $this->postJson($this->baseEndpoint, $details);
93 $resp->assertStatus(422);
96 'message' => 'The given data was invalid.',
98 'name' => ['The name field is required.'],
105 public function test_read_endpoint()
107 $this->actingAsApiEditor();
108 $shelf = Bookshelf::visible()->first();
110 $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
112 $resp->assertStatus(200);
115 'slug' => $shelf->slug,
117 'name' => $shelf->createdBy->name,
120 'name' => $shelf->createdBy->name,
123 'name' => $shelf->ownedBy->name,
128 public function test_update_endpoint()
130 $this->actingAsApiEditor();
131 $shelf = Bookshelf::visible()->first();
133 'name' => 'My updated API shelf',
134 'description' => 'A shelf updated via the API',
137 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
140 $resp->assertStatus(200);
141 $resp->assertJson(array_merge($details, [
143 'slug' => $shelf->slug,
144 'description_html' => '<p>A shelf updated via the API</p>',
146 $this->assertActivityExists('bookshelf_update', $shelf);
149 public function test_update_endpoint_with_html()
151 $this->actingAsApiEditor();
152 $shelf = Bookshelf::visible()->first();
154 'name' => 'My updated API shelf',
155 'description_html' => '<p>A shelf <em>updated</em> via the API</p>',
158 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
159 $resp->assertStatus(200);
161 $this->assertDatabaseHas('bookshelves', array_merge($details, ['id' => $shelf->id, 'description' => 'A shelf updated via the API']));
164 public function test_update_increments_updated_date_if_only_tags_are_sent()
166 $this->actingAsApiEditor();
167 $shelf = Bookshelf::visible()->first();
168 DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
171 'tags' => [['name' => 'Category', 'value' => 'Testing']],
174 $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
176 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
179 public function test_update_only_assigns_books_if_param_provided()
181 $this->actingAsApiEditor();
182 $shelf = Bookshelf::visible()->first();
183 $this->assertTrue($shelf->books()->count() > 0);
185 'name' => 'My updated API shelf',
188 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
189 $resp->assertStatus(200);
190 $this->assertTrue($shelf->books()->count() > 0);
192 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
193 $resp->assertStatus(200);
194 $this->assertTrue($shelf->books()->count() === 0);
197 public function test_update_cover_image_control()
199 $this->actingAsApiEditor();
200 /** @var Book $shelf */
201 $shelf = Bookshelf::visible()->first();
202 $this->assertNull($shelf->cover);
203 $file = $this->files->uploadedImage('image.png');
205 // Ensure cover image can be set via API
206 $resp = $this->call('PUT', $this->baseEndpoint . "/{$shelf->id}", [
207 'name' => 'My updated API shelf with image',
208 ], [], ['image' => $file]);
211 $resp->assertStatus(200);
212 $this->assertNotNull($shelf->cover);
214 // Ensure further updates without image do not clear cover image
215 $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
216 'name' => 'My updated shelf again',
220 $resp->assertStatus(200);
221 $this->assertNotNull($shelf->cover);
223 // Ensure update with null image property clears image
224 $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
229 $resp->assertStatus(200);
230 $this->assertNull($shelf->cover);
233 public function test_delete_endpoint()
235 $this->actingAsApiEditor();
236 $shelf = Bookshelf::visible()->first();
237 $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
239 $resp->assertStatus(204);
240 $this->assertActivityExists('bookshelf_delete');