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