5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
9 class ShelvesApiTest extends TestCase
13 protected $baseEndpoint = '/api/shelves';
15 public function test_index_endpoint_returns_expected_shelf()
17 $this->actingAsApiEditor();
18 $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
20 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
21 $resp->assertJson(['data' => [
23 'id' => $firstBookshelf->id,
24 'name' => $firstBookshelf->name,
25 'slug' => $firstBookshelf->slug,
30 public function test_create_endpoint()
32 $this->actingAsApiEditor();
33 $books = Book::query()->take(2)->get();
36 'name' => 'My API shelf',
37 'description' => 'A shelf created via the API',
40 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
41 $resp->assertStatus(200);
42 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
43 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
44 $this->assertActivityExists('bookshelf_create', $newItem);
45 foreach ($books as $index => $book) {
46 $this->assertDatabaseHas('bookshelves_books', [
47 'bookshelf_id' => $newItem->id,
48 'book_id' => $book->id,
54 public function test_shelf_name_needed_to_create()
56 $this->actingAsApiEditor();
58 'description' => 'A shelf created via the API',
61 $resp = $this->postJson($this->baseEndpoint, $details);
62 $resp->assertStatus(422);
65 'message' => 'The given data was invalid.',
67 'name' => ['The name field is required.'],
74 public function test_read_endpoint()
76 $this->actingAsApiEditor();
77 $shelf = Bookshelf::visible()->first();
79 $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
81 $resp->assertStatus(200);
84 'slug' => $shelf->slug,
86 'name' => $shelf->createdBy->name,
89 'name' => $shelf->createdBy->name,
92 'name' => $shelf->ownedBy->name,
97 public function test_update_endpoint()
99 $this->actingAsApiEditor();
100 $shelf = Bookshelf::visible()->first();
102 'name' => 'My updated API shelf',
103 'description' => 'A shelf created via the API',
106 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
109 $resp->assertStatus(200);
110 $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
111 $this->assertActivityExists('bookshelf_update', $shelf);
114 public function test_update_only_assigns_books_if_param_provided()
116 $this->actingAsApiEditor();
117 $shelf = Bookshelf::visible()->first();
118 $this->assertTrue($shelf->books()->count() > 0);
120 'name' => 'My updated API shelf',
123 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
124 $resp->assertStatus(200);
125 $this->assertTrue($shelf->books()->count() > 0);
127 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
128 $resp->assertStatus(200);
129 $this->assertTrue($shelf->books()->count() === 0);
132 public function test_delete_endpoint()
134 $this->actingAsApiEditor();
135 $shelf = Bookshelf::visible()->first();
136 $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
138 $resp->assertStatus(204);
139 $this->assertActivityExists('bookshelf_delete');