]> BookStack Code Mirror - bookstack/blob - tests/Api/ShelvesApiTest.php
Deps: Updated composer packages
[bookstack] / tests / Api / ShelvesApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use Carbon\Carbon;
8 use Illuminate\Support\Facades\DB;
9 use Tests\TestCase;
10
11 class ShelvesApiTest extends TestCase
12 {
13     use TestsApi;
14
15     protected string $baseEndpoint = '/api/shelves';
16
17     public function test_index_endpoint_returns_expected_shelf()
18     {
19         $this->actingAsApiEditor();
20         $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
21
22         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23         $resp->assertJson(['data' => [
24             [
25                 'id'   => $firstBookshelf->id,
26                 'name' => $firstBookshelf->name,
27                 'slug' => $firstBookshelf->slug,
28             ],
29         ]]);
30     }
31
32     public function test_create_endpoint()
33     {
34         $this->actingAsApiEditor();
35         $books = Book::query()->take(2)->get();
36
37         $details = [
38             'name'        => 'My API shelf',
39             'description' => 'A shelf created via the API',
40         ];
41
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, [
46             'id' => $newItem->id,
47             'slug' => $newItem->slug,
48             'description_html' => '<p>A shelf created via the API</p>',
49         ]));
50         $this->assertActivityExists('bookshelf_create', $newItem);
51         foreach ($books as $index => $book) {
52             $this->assertDatabaseHas('bookshelves_books', [
53                 'bookshelf_id' => $newItem->id,
54                 'book_id'      => $book->id,
55                 'order'        => $index,
56             ]);
57         }
58     }
59
60     public function test_create_endpoint_with_html()
61     {
62         $this->actingAsApiEditor();
63
64         $details = [
65             'name'             => 'My API shelf',
66             'description_html' => '<p>A <strong>shelf</strong> created via the API</p>',
67         ];
68
69         $resp = $this->postJson($this->baseEndpoint, $details);
70         $resp->assertStatus(200);
71         $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
72
73         $expectedDetails = array_merge($details, [
74             'id'          => $newItem->id,
75             'description' => 'A shelf created via the API',
76         ]);
77
78         $resp->assertJson($expectedDetails);
79         $this->assertDatabaseHas('bookshelves', $expectedDetails);
80     }
81
82     public function test_shelf_name_needed_to_create()
83     {
84         $this->actingAsApiEditor();
85         $details = [
86             'description' => 'A shelf created via the API',
87         ];
88
89         $resp = $this->postJson($this->baseEndpoint, $details);
90         $resp->assertStatus(422);
91         $resp->assertJson([
92             'error' => [
93                 'message'    => 'The given data was invalid.',
94                 'validation' => [
95                     'name' => ['The name field is required.'],
96                 ],
97                 'code' => 422,
98             ],
99         ]);
100     }
101
102     public function test_read_endpoint()
103     {
104         $this->actingAsApiEditor();
105         $shelf = Bookshelf::visible()->first();
106
107         $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
108
109         $resp->assertStatus(200);
110         $resp->assertJson([
111             'id'         => $shelf->id,
112             'slug'       => $shelf->slug,
113             'created_by' => [
114                 'name' => $shelf->createdBy->name,
115             ],
116             'updated_by' => [
117                 'name' => $shelf->createdBy->name,
118             ],
119             'owned_by' => [
120                 'name' => $shelf->ownedBy->name,
121             ],
122         ]);
123     }
124
125     public function test_update_endpoint()
126     {
127         $this->actingAsApiEditor();
128         $shelf = Bookshelf::visible()->first();
129         $details = [
130             'name'        => 'My updated API shelf',
131             'description' => 'A shelf updated via the API',
132         ];
133
134         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
135         $shelf->refresh();
136
137         $resp->assertStatus(200);
138         $resp->assertJson(array_merge($details, [
139             'id' => $shelf->id,
140             'slug' => $shelf->slug,
141             'description_html' => '<p>A shelf updated via the API</p>',
142         ]));
143         $this->assertActivityExists('bookshelf_update', $shelf);
144     }
145
146     public function test_update_endpoint_with_html()
147     {
148         $this->actingAsApiEditor();
149         $shelf = Bookshelf::visible()->first();
150         $details = [
151             'name'             => 'My updated API shelf',
152             'description_html' => '<p>A shelf <em>updated</em> via the API</p>',
153         ];
154
155         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
156         $resp->assertStatus(200);
157
158         $this->assertDatabaseHas('bookshelves', array_merge($details, ['id' => $shelf->id, 'description' => 'A shelf updated via the API']));
159     }
160
161     public function test_update_increments_updated_date_if_only_tags_are_sent()
162     {
163         $this->actingAsApiEditor();
164         $shelf = Bookshelf::visible()->first();
165         DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
166
167         $details = [
168             'tags' => [['name' => 'Category', 'value' => 'Testing']],
169         ];
170
171         $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
172         $shelf->refresh();
173         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
174     }
175
176     public function test_update_only_assigns_books_if_param_provided()
177     {
178         $this->actingAsApiEditor();
179         $shelf = Bookshelf::visible()->first();
180         $this->assertTrue($shelf->books()->count() > 0);
181         $details = [
182             'name' => 'My updated API shelf',
183         ];
184
185         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
186         $resp->assertStatus(200);
187         $this->assertTrue($shelf->books()->count() > 0);
188
189         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
190         $resp->assertStatus(200);
191         $this->assertTrue($shelf->books()->count() === 0);
192     }
193
194     public function test_update_cover_image_control()
195     {
196         $this->actingAsApiEditor();
197         /** @var Book $shelf */
198         $shelf = Bookshelf::visible()->first();
199         $this->assertNull($shelf->cover);
200         $file = $this->files->uploadedImage('image.png');
201
202         // Ensure cover image can be set via API
203         $resp = $this->call('PUT', $this->baseEndpoint . "/{$shelf->id}", [
204             'name'  => 'My updated API shelf with image',
205         ], [], ['image' => $file]);
206         $shelf->refresh();
207
208         $resp->assertStatus(200);
209         $this->assertNotNull($shelf->cover);
210
211         // Ensure further updates without image do not clear cover image
212         $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
213             'name' => 'My updated shelf again',
214         ]);
215         $shelf->refresh();
216
217         $resp->assertStatus(200);
218         $this->assertNotNull($shelf->cover);
219
220         // Ensure update with null image property clears image
221         $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
222             'image' => null,
223         ]);
224         $shelf->refresh();
225
226         $resp->assertStatus(200);
227         $this->assertNull($shelf->cover);
228     }
229
230     public function test_delete_endpoint()
231     {
232         $this->actingAsApiEditor();
233         $shelf = Bookshelf::visible()->first();
234         $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
235
236         $resp->assertStatus(204);
237         $this->assertActivityExists('bookshelf_delete');
238     }
239 }