3 use BookStack\Entities\Book;
5 class BooksApiTest extends TestCase
9 protected $baseEndpoint = '/api/books';
11 public function test_index_endpoint_returns_expected_book()
13 $this->actingAsApiEditor();
14 $firstBook = Book::query()->orderBy('id', 'asc')->first();
16 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
17 $resp->assertJson(['data' => [
19 'id' => $firstBook->id,
20 'name' => $firstBook->name,
21 'slug' => $firstBook->slug,
26 public function test_create_endpoint()
28 $this->actingAsApiEditor();
30 'name' => 'My API book',
31 'description' => 'A book created via the API',
34 $resp = $this->postJson($this->baseEndpoint, $details);
35 $resp->assertStatus(200);
36 $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
37 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
38 $this->assertActivityExists('book_create', $newItem);
41 public function test_book_name_needed_to_create()
43 $this->actingAsApiEditor();
45 'description' => 'A book created via the API',
48 $resp = $this->postJson($this->baseEndpoint, $details);
49 $resp->assertStatus(422);
52 "message" => "The given data was invalid.",
54 "name" => ["The name field is required."]
61 public function test_read_endpoint()
63 $this->actingAsApiEditor();
64 $book = Book::visible()->first();
66 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
68 $resp->assertStatus(200);
71 'slug' => $book->slug,
73 'name' => $book->createdBy->name,
76 'name' => $book->createdBy->name,
81 public function test_update_endpoint()
83 $this->actingAsApiEditor();
84 $book = Book::visible()->first();
86 'name' => 'My updated API book',
87 'description' => 'A book created via the API',
90 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
93 $resp->assertStatus(200);
94 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
95 $this->assertActivityExists('book_update', $book);
98 public function test_delete_endpoint()
100 $this->actingAsApiEditor();
101 $book = Book::visible()->first();
102 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
104 $resp->assertStatus(204);
105 $this->assertActivityExists('book_delete');