-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathBooksApiTest.php
More file actions
320 lines (266 loc) · 10.6 KB
/
Copy pathBooksApiTest.php
File metadata and controls
320 lines (266 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
namespace Tests\Api;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Repos\BaseRepo;
use Carbon\Carbon;
use Tests\TestCase;
class BooksApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/books';
public function test_index_endpoint_returns_expected_book()
{
$this->actingAsApiEditor();
$firstBook = Book::query()->orderBy('id', 'asc')->first();
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [
[
'id' => $firstBook->id,
'name' => $firstBook->name,
'slug' => $firstBook->slug,
'owned_by' => $firstBook->owned_by,
'created_by' => $firstBook->created_by,
'updated_by' => $firstBook->updated_by,
'cover' => null,
],
]]);
}
public function test_index_endpoint_includes_cover_if_set()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$baseRepo = $this->app->make(BaseRepo::class);
$image = $this->files->uploadedImage('book_cover.png');
$baseRepo->updateCoverImage($book, $image);
$resp = $this->getJson($this->baseEndpoint . '?filter[id]=' . $book->id);
$resp->assertJson(['data' => [
[
'id' => $book->id,
'cover' => [
'id' => $book->coverInfo()->getImage()->id,
'url' => $book->coverInfo()->getImage()->url,
],
],
]]);
}
public function test_create_endpoint()
{
$this->actingAsApiEditor();
$templatePage = $this->entities->templatePage();
$details = [
'name' => 'My API book',
'description' => 'A book created via the API',
'default_template_id' => $templatePage->id,
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(200);
$newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$resp->assertJson(array_merge($details, [
'id' => $newItem->id,
'slug' => $newItem->slug,
'description_html' => '<p>A book created via the API</p>',
]));
$this->assertActivityExists('book_create', $newItem);
}
public function test_create_endpoint_with_html()
{
$this->actingAsApiEditor();
$details = [
'name' => 'My API book',
'description_html' => '<p>A book <em>created</em> <strong>via</strong> the API</p>',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(200);
$newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$expectedDetails = array_merge($details, [
'id' => $newItem->id,
'description' => 'A book created via the API',
]);
$resp->assertJson($expectedDetails);
$this->assertDatabaseHasEntityData('book', $expectedDetails);
}
public function test_book_name_needed_to_create()
{
$this->actingAsApiEditor();
$details = [
'description' => 'A book created via the API',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson([
'error' => [
'message' => 'The given data was invalid.',
'validation' => [
'name' => ['The name field is required.'],
],
'code' => 422,
],
]);
}
public function test_read_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
$resp->assertStatus(200);
$resp->assertJson([
'id' => $book->id,
'slug' => $book->slug,
'created_by' => [
'name' => $book->createdBy->name,
],
'updated_by' => [
'name' => $book->createdBy->name,
],
'owned_by' => [
'name' => $book->ownedBy->name,
],
'default_template_id' => null,
]);
}
public function test_read_endpoint_includes_chapter_and_page_contents()
{
$this->actingAsApiEditor();
$book = $this->entities->bookHasChaptersAndPages();
$chapter = $book->chapters()->first();
$chapterPage = $chapter->pages()->first();
$resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
$directChildCount = $book->directPages()->count() + $book->chapters()->count();
$resp->assertStatus(200);
$resp->assertJsonCount($directChildCount, 'contents');
$contents = $resp->json('contents');
$respChapter = array_values(array_filter($contents, fn ($item) => ($item['id'] === $chapter->id && $item['type'] === 'chapter')))[0];
$this->assertArrayMapIncludes([
'id' => $chapter->id,
'type' => 'chapter',
'name' => $chapter->name,
'slug' => $chapter->slug,
], $respChapter);
$respPage = array_values(array_filter($respChapter['pages'], fn ($item) => ($item['id'] === $chapterPage->id)))[0];
$this->assertArrayMapIncludes([
'id' => $chapterPage->id,
'name' => $chapterPage->name,
'slug' => $chapterPage->slug,
], $respPage);
}
public function test_read_endpoint_contents_nested_pages_has_permissions_applied()
{
$this->actingAsApiEditor();
$book = $this->entities->bookHasChaptersAndPages();
$chapter = $book->chapters()->first();
$chapterPage = $chapter->pages()->first();
$customName = 'MyNonVisiblePageWithinAChapter';
$chapterPage->name = $customName;
$chapterPage->save();
$this->permissions->disableEntityInheritedPermissions($chapterPage);
$resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
$resp->assertJsonMissing(['name' => $customName]);
}
public function test_read_endpoint_lists_visible_shelves_the_book_is_assigned_to()
{
$this->actingAsApiEditor();
$shelf = $this->entities->shelf();
$otherShelf = $this->entities->shelf();
$book = $this->entities->book();
$book->shelves()->detach();
$book->shelves()->attach($shelf);
$book->shelves()->attach($otherShelf);
$this->assertEquals(2, $book->shelves()->count());
$this->permissions->disableEntityInheritedPermissions($otherShelf);
$resp = $this->getJson("{$this->baseEndpoint}/{$book->id}");
$resp->assertOk();
$resp->assertJsonCount(1, 'shelves');
$resp->assertJson([
'shelves' => [
[
'id' => $shelf->id,
'name' => $shelf->name,
'slug' => $shelf->slug,
]
]
]);
$resp->assertJsonMissingPath('shelves.0.description');
$resp->assertJsonMissingPath('shelves.0.pivot');
}
public function test_update_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$templatePage = $this->entities->templatePage();
$details = [
'name' => 'My updated API book',
'description' => 'A book updated via the API',
'default_template_id' => $templatePage->id,
];
$resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
$book->refresh();
$resp->assertStatus(200);
$resp->assertJson(array_merge($details, [
'id' => $book->id,
'slug' => $book->slug,
'description_html' => '<p>A book updated via the API</p>',
]));
$this->assertActivityExists('book_update', $book);
}
public function test_update_endpoint_with_html()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'name' => 'My updated API book',
'description_html' => '<p>A book <strong>updated</strong> via the API</p>',
];
$resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
$resp->assertStatus(200);
$this->assertDatabaseHasEntityData('book', array_merge($details, ['id' => $book->id, 'description' => 'A book updated via the API']));
}
public function test_update_increments_updated_date_if_only_tags_are_sent()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
Book::query()->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
$details = [
'tags' => [['name' => 'Category', 'value' => 'Testing']],
];
$this->putJson($this->baseEndpoint . "/{$book->id}", $details);
$book->refresh();
$this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
}
public function test_update_cover_image_control()
{
$this->actingAsApiEditor();
/** @var Book $book */
$book = $this->entities->book();
$this->assertNull($book->coverInfo()->getImage());
$file = $this->files->uploadedImage('image.png');
// Ensure cover image can be set via API
$resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
'name' => 'My updated API book with image',
], [], ['image' => $file]);
$book->refresh();
$resp->assertStatus(200);
$this->assertNotNull($book->coverInfo()->getImage());
// Ensure further updates without image do not clear cover image
$resp = $this->put($this->baseEndpoint . "/{$book->id}", [
'name' => 'My updated book again',
]);
$book->refresh();
$resp->assertStatus(200);
$this->assertNotNull($book->coverInfo()->getImage());
// Ensure update with null image property clears image
$resp = $this->put($this->baseEndpoint . "/{$book->id}", [
'image' => null,
]);
$book->refresh();
$resp->assertStatus(200);
$this->assertNull($book->coverInfo()->getImage());
}
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
$resp->assertStatus(204);
$this->assertActivityExists('book_delete');
}
}