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