]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Updated styles to use logical properties/values
[bookstack] / tests / Api / BooksApiTest.php
1 <?php namespace Tests\Api;
2
3 use BookStack\Entities\Book;
4 use Tests\TestCase;
5
6 class BooksApiTest extends TestCase
7 {
8     use TestsApi;
9
10     protected $baseEndpoint = '/api/books';
11
12     public function test_index_endpoint_returns_expected_book()
13     {
14         $this->actingAsApiEditor();
15         $firstBook = Book::query()->orderBy('id', 'asc')->first();
16
17         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
18         $resp->assertJson(['data' => [
19             [
20                 'id' => $firstBook->id,
21                 'name' => $firstBook->name,
22                 'slug' => $firstBook->slug,
23             ]
24         ]]);
25     }
26
27     public function test_create_endpoint()
28     {
29         $this->actingAsApiEditor();
30         $details = [
31             'name' => 'My API book',
32             'description' => 'A book created via the API',
33         ];
34
35         $resp = $this->postJson($this->baseEndpoint, $details);
36         $resp->assertStatus(200);
37         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
38         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
39         $this->assertActivityExists('book_create', $newItem);
40     }
41
42     public function test_book_name_needed_to_create()
43     {
44         $this->actingAsApiEditor();
45         $details = [
46             'description' => 'A book created via the API',
47         ];
48
49         $resp = $this->postJson($this->baseEndpoint, $details);
50         $resp->assertStatus(422);
51         $resp->assertJson([
52             "error" => [
53                 "message" => "The given data was invalid.",
54                 "validation" => [
55                     "name" => ["The name field is required."]
56                 ],
57                 "code" => 422,
58             ],
59         ]);
60     }
61
62     public function test_read_endpoint()
63     {
64         $this->actingAsApiEditor();
65         $book = Book::visible()->first();
66
67         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
68
69         $resp->assertStatus(200);
70         $resp->assertJson([
71             'id' => $book->id,
72             'slug' => $book->slug,
73             'created_by' => [
74                 'name' => $book->createdBy->name,
75             ],
76             'updated_by' => [
77                 'name' => $book->createdBy->name,
78             ]
79         ]);
80     }
81
82     public function test_update_endpoint()
83     {
84         $this->actingAsApiEditor();
85         $book = Book::visible()->first();
86         $details = [
87             'name' => 'My updated API book',
88             'description' => 'A book created via the API',
89         ];
90
91         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
92         $book->refresh();
93
94         $resp->assertStatus(200);
95         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
96         $this->assertActivityExists('book_update', $book);
97     }
98
99     public function test_delete_endpoint()
100     {
101         $this->actingAsApiEditor();
102         $book = Book::visible()->first();
103         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
104
105         $resp->assertStatus(204);
106         $this->assertActivityExists('book_delete');
107     }
108 }