1 <?php namespace Tests\Api;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
7 class ShelvesApiTest extends TestCase
11 protected $baseEndpoint = '/api/shelves';
13 public function test_index_endpoint_returns_expected_shelf()
15 $this->actingAsApiEditor();
16 $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
18 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
19 $resp->assertJson(['data' => [
21 'id' => $firstBookshelf->id,
22 'name' => $firstBookshelf->name,
23 'slug' => $firstBookshelf->slug,
28 public function test_create_endpoint()
30 $this->actingAsApiEditor();
31 $books = Book::query()->take(2)->get();
34 'name' => 'My API shelf',
35 'description' => 'A shelf created via the API',
38 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
39 $resp->assertStatus(200);
40 $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
41 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
42 $this->assertActivityExists('bookshelf_create', $newItem);
43 foreach ($books as $index => $book) {
44 $this->assertDatabaseHas('bookshelves_books', [
45 'bookshelf_id' => $newItem->id,
46 'book_id' => $book->id,
52 public function test_shelf_name_needed_to_create()
54 $this->actingAsApiEditor();
56 'description' => 'A shelf created via the API',
59 $resp = $this->postJson($this->baseEndpoint, $details);
60 $resp->assertStatus(422);
63 "message" => "The given data was invalid.",
65 "name" => ["The name field is required."]
72 public function test_read_endpoint()
74 $this->actingAsApiEditor();
75 $shelf = Bookshelf::visible()->first();
77 $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
79 $resp->assertStatus(200);
82 'slug' => $shelf->slug,
84 'name' => $shelf->createdBy->name,
87 'name' => $shelf->createdBy->name,
92 public function test_update_endpoint()
94 $this->actingAsApiEditor();
95 $shelf = Bookshelf::visible()->first();
97 'name' => 'My updated API shelf',
98 'description' => 'A shelf created via the API',
101 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
104 $resp->assertStatus(200);
105 $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
106 $this->assertActivityExists('bookshelf_update', $shelf);
109 public function test_update_only_assigns_books_if_param_provided()
111 $this->actingAsApiEditor();
112 $shelf = Bookshelf::visible()->first();
113 $this->assertTrue($shelf->books()->count() > 0);
115 'name' => 'My updated API shelf',
118 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
119 $resp->assertStatus(200);
120 $this->assertTrue($shelf->books()->count() > 0);
122 $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
123 $resp->assertStatus(200);
124 $this->assertTrue($shelf->books()->count() === 0);
127 public function test_delete_endpoint()
129 $this->actingAsApiEditor();
130 $shelf = Bookshelf::visible()->first();
131 $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
133 $resp->assertStatus(204);
134 $this->assertActivityExists('bookshelf_delete');