]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Merge branch 'BookStackApp:development' into add-priority
[bookstack] / tests / Api / BooksApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use Carbon\Carbon;
7 use Illuminate\Support\Facades\DB;
8 use Tests\TestCase;
9
10 class BooksApiTest extends TestCase
11 {
12     use TestsApi;
13
14     protected string $baseEndpoint = '/api/books';
15
16     public function test_index_endpoint_returns_expected_book()
17     {
18         $this->actingAsApiEditor();
19         $firstBook = Book::query()->orderBy('id', 'asc')->first();
20
21         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
22         $resp->assertJson(['data' => [
23             [
24                 'id'   => $firstBook->id,
25                 'name' => $firstBook->name,
26                 'slug' => $firstBook->slug,
27             ],
28         ]]);
29     }
30
31     public function test_create_endpoint()
32     {
33         $this->actingAsApiEditor();
34         $details = [
35             'name'        => 'My API book',
36             'description' => 'A book created via the API',
37         ];
38
39         $resp = $this->postJson($this->baseEndpoint, $details);
40         $resp->assertStatus(200);
41         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
42         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
43         $this->assertActivityExists('book_create', $newItem);
44     }
45
46     public function test_book_name_needed_to_create()
47     {
48         $this->actingAsApiEditor();
49         $details = [
50             'description' => 'A book created via the API',
51         ];
52
53         $resp = $this->postJson($this->baseEndpoint, $details);
54         $resp->assertStatus(422);
55         $resp->assertJson([
56             'error' => [
57                 'message'    => 'The given data was invalid.',
58                 'validation' => [
59                     'name' => ['The name field is required.'],
60                 ],
61                 'code' => 422,
62             ],
63         ]);
64     }
65
66     public function test_read_endpoint()
67     {
68         $this->actingAsApiEditor();
69         $book = $this->entities->book();
70
71         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
72
73         $resp->assertStatus(200);
74         $resp->assertJson([
75             'id'         => $book->id,
76             'slug'       => $book->slug,
77             'created_by' => [
78                 'name' => $book->createdBy->name,
79             ],
80             'updated_by' => [
81                 'name' => $book->createdBy->name,
82             ],
83             'owned_by' => [
84                 'name' => $book->ownedBy->name,
85             ],
86         ]);
87     }
88
89     public function test_read_endpoint_includes_chapter_and_page_contents()
90     {
91         $this->actingAsApiEditor();
92         $book = $this->entities->bookHasChaptersAndPages();
93         $chapter = $book->chapters()->first();
94         $chapterPage = $chapter->pages()->first();
95
96         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
97
98         $directChildCount = $book->directPages()->count() + $book->chapters()->count();
99         $resp->assertStatus(200);
100         $resp->assertJsonCount($directChildCount, 'contents');
101         $resp->assertJson([
102             'contents' => [
103                 [
104                     'type' => 'chapter',
105                     'id' => $chapter->id,
106                     'name' => $chapter->name,
107                     'slug' => $chapter->slug,
108                     'pages' => [
109                         [
110                             'id' => $chapterPage->id,
111                             'name' => $chapterPage->name,
112                             'slug' => $chapterPage->slug,
113                         ]
114                     ]
115                 ]
116             ]
117         ]);
118     }
119
120     public function test_update_endpoint()
121     {
122         $this->actingAsApiEditor();
123         $book = $this->entities->book();
124         $details = [
125             'name'        => 'My updated API book',
126             'description' => 'A book created via the API',
127         ];
128
129         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
130         $book->refresh();
131
132         $resp->assertStatus(200);
133         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
134         $this->assertActivityExists('book_update', $book);
135     }
136
137     public function test_update_increments_updated_date_if_only_tags_are_sent()
138     {
139         $this->actingAsApiEditor();
140         $book = $this->entities->book();
141         DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
142
143         $details = [
144             'tags' => [['name' => 'Category', 'value' => 'Testing']],
145         ];
146
147         $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
148         $book->refresh();
149         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
150     }
151
152     public function test_update_cover_image_control()
153     {
154         $this->actingAsApiEditor();
155         /** @var Book $book */
156         $book = $this->entities->book();
157         $this->assertNull($book->cover);
158         $file = $this->files->uploadedImage('image.png');
159
160         // Ensure cover image can be set via API
161         $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
162             'name'  => 'My updated API book with image',
163         ], [], ['image' => $file]);
164         $book->refresh();
165
166         $resp->assertStatus(200);
167         $this->assertNotNull($book->cover);
168
169         // Ensure further updates without image do not clear cover image
170         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
171             'name' => 'My updated book again',
172         ]);
173         $book->refresh();
174
175         $resp->assertStatus(200);
176         $this->assertNotNull($book->cover);
177
178         // Ensure update with null image property clears image
179         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
180             'image' => null,
181         ]);
182         $book->refresh();
183
184         $resp->assertStatus(200);
185         $this->assertNull($book->cover);
186     }
187
188     public function test_delete_endpoint()
189     {
190         $this->actingAsApiEditor();
191         $book = $this->entities->book();
192         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
193
194         $resp->assertStatus(204);
195         $this->assertActivityExists('book_delete');
196     }
197
198     public function test_export_html_endpoint()
199     {
200         $this->actingAsApiEditor();
201         $book = $this->entities->book();
202
203         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
204         $resp->assertStatus(200);
205         $resp->assertSee($book->name);
206         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
207     }
208
209     public function test_export_plain_text_endpoint()
210     {
211         $this->actingAsApiEditor();
212         $book = $this->entities->book();
213
214         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
215         $resp->assertStatus(200);
216         $resp->assertSee($book->name);
217         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
218     }
219
220     public function test_export_pdf_endpoint()
221     {
222         $this->actingAsApiEditor();
223         $book = $this->entities->book();
224
225         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
226         $resp->assertStatus(200);
227         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
228     }
229
230     public function test_export_markdown_endpoint()
231     {
232         $this->actingAsApiEditor();
233         $book = Book::visible()->has('pages')->has('chapters')->first();
234
235         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
236         $resp->assertStatus(200);
237         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
238         $resp->assertSee('# ' . $book->name);
239         $resp->assertSee('# ' . $book->pages()->first()->name);
240         $resp->assertSee('# ' . $book->chapters()->first()->name);
241     }
242
243     public function test_cant_export_when_not_have_permission()
244     {
245         $types = ['html', 'plaintext', 'pdf', 'markdown'];
246         $this->actingAsApiEditor();
247         $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
248
249         $book = $this->entities->book();
250         foreach ($types as $type) {
251             $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
252             $this->assertPermissionError($resp);
253         }
254     }
255 }