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, ['id' => $newItem->id, 'slug' => $newItem->slug]));
46 $this->assertActivityExists('bookshelf_create', $newItem);
47 foreach ($books as $index => $book) {
48 $this->assertDatabaseHas('bookshelves_books', [
49 'bookshelf_id' => $newItem->id,
50 'book_id' => $book->id,
56 public function test_shelf_name_needed_to_create()
58 $this->actingAsApiEditor();
60 'description' => 'A shelf created via the API',
63 $resp = $this->postJson($this->baseEndpoint, $details);
64 $resp->assertStatus(422);
67 'message' => 'The given data was invalid.',
69 'name' => ['The name field is required.'],
76 public function test_read_endpoint()
78 $this->actingAsApiEditor();
79 $shelf = Bookshelf::visible()->first();
81 $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
83 $resp->assertStatus(200);
86 'slug' => $shelf->slug,
88 'name' => $shelf->createdBy->name,
91 'name' => $shelf->createdBy->name,
94 'name' => $shelf->ownedBy->name,
99 public function test_update_endpoint()
101 $this->actingAsApiEditor();
102 $shelf = Bookshelf::visible()->first();
104 'name' => 'My updated API shelf',
105 'description' => 'A shelf created via the API',
108 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
111 $resp->assertStatus(200);
112 $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
113 $this->assertActivityExists('bookshelf_update', $shelf);
116 public function test_update_increments_updated_date_if_only_tags_are_sent()
118 $this->actingAsApiEditor();
119 $shelf = Bookshelf::visible()->first();
120 DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
123 'tags' => [['name' => 'Category', 'value' => 'Testing']],
126 $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
128 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
131 public function test_update_only_assigns_books_if_param_provided()
133 $this->actingAsApiEditor();
134 $shelf = Bookshelf::visible()->first();
135 $this->assertTrue($shelf->books()->count() > 0);
137 'name' => 'My updated API shelf',
140 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
141 $resp->assertStatus(200);
142 $this->assertTrue($shelf->books()->count() > 0);
144 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
145 $resp->assertStatus(200);
146 $this->assertTrue($shelf->books()->count() === 0);
149 public function test_delete_endpoint()
151 $this->actingAsApiEditor();
152 $shelf = Bookshelf::visible()->first();
153 $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
155 $resp->assertStatus(204);
156 $this->assertActivityExists('bookshelf_delete');