]> BookStack Code Mirror - bookstack/blob - tests/Api/ChaptersApiTest.php
Input WYSIWYG: Fixed up some dark mode elements
[bookstack] / tests / Api / ChaptersApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use Carbon\Carbon;
8 use Illuminate\Support\Facades\DB;
9 use Tests\TestCase;
10
11 class ChaptersApiTest extends TestCase
12 {
13     use TestsApi;
14
15     protected string $baseEndpoint = '/api/chapters';
16
17     public function test_index_endpoint_returns_expected_chapter()
18     {
19         $this->actingAsApiEditor();
20         $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
21
22         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23         $resp->assertJson(['data' => [
24             [
25                 'id'       => $firstChapter->id,
26                 'name'     => $firstChapter->name,
27                 'slug'     => $firstChapter->slug,
28                 'book_id'  => $firstChapter->book->id,
29                 'priority' => $firstChapter->priority,
30             ],
31         ]]);
32     }
33
34     public function test_create_endpoint()
35     {
36         $this->actingAsApiEditor();
37         $book = $this->entities->book();
38         $details = [
39             'name'        => 'My API chapter',
40             'description' => 'A chapter created via the API',
41             'book_id'     => $book->id,
42             'tags'        => [
43                 [
44                     'name'  => 'tagname',
45                     'value' => 'tagvalue',
46                 ],
47             ],
48             'priority' => 15,
49         ];
50
51         $resp = $this->postJson($this->baseEndpoint, $details);
52         $resp->assertStatus(200);
53         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
54         $resp->assertJson(array_merge($details, [
55             'id' => $newItem->id,
56             'slug' => $newItem->slug,
57             'description_html' => '<p>A chapter created via the API</p>',
58         ]));
59         $this->assertDatabaseHas('tags', [
60             'entity_id'   => $newItem->id,
61             'entity_type' => $newItem->getMorphClass(),
62             'name'        => 'tagname',
63             'value'       => 'tagvalue',
64         ]);
65         $resp->assertJsonMissing(['pages' => []]);
66         $this->assertActivityExists('chapter_create', $newItem);
67     }
68
69     public function test_create_endpoint_with_html()
70     {
71         $this->actingAsApiEditor();
72         $book = $this->entities->book();
73         $details = [
74             'name'             => 'My API chapter',
75             'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
76             'book_id'          => $book->id,
77         ];
78
79         $resp = $this->postJson($this->baseEndpoint, $details);
80         $resp->assertStatus(200);
81         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
82
83         $expectedDetails = array_merge($details, [
84             'id'          => $newItem->id,
85             'description' => 'A chapter created via the API',
86         ]);
87         $resp->assertJson($expectedDetails);
88         $this->assertDatabaseHas('chapters', $expectedDetails);
89     }
90
91     public function test_chapter_name_needed_to_create()
92     {
93         $this->actingAsApiEditor();
94         $book = $this->entities->book();
95         $details = [
96             'book_id'     => $book->id,
97             'description' => 'A chapter created via the API',
98         ];
99
100         $resp = $this->postJson($this->baseEndpoint, $details);
101         $resp->assertStatus(422);
102         $resp->assertJson($this->validationResponse([
103             'name' => ['The name field is required.'],
104         ]));
105     }
106
107     public function test_chapter_book_id_needed_to_create()
108     {
109         $this->actingAsApiEditor();
110         $details = [
111             'name'        => 'My api chapter',
112             'description' => 'A chapter created via the API',
113         ];
114
115         $resp = $this->postJson($this->baseEndpoint, $details);
116         $resp->assertStatus(422);
117         $resp->assertJson($this->validationResponse([
118             'book_id' => ['The book id field is required.'],
119         ]));
120     }
121
122     public function test_read_endpoint()
123     {
124         $this->actingAsApiEditor();
125         $chapter = $this->entities->chapter();
126         $page = $chapter->pages()->first();
127
128         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
129         $resp->assertStatus(200);
130         $resp->assertJson([
131             'id'         => $chapter->id,
132             'slug'       => $chapter->slug,
133             'created_by' => [
134                 'name' => $chapter->createdBy->name,
135             ],
136             'book_id'    => $chapter->book_id,
137             'updated_by' => [
138                 'name' => $chapter->createdBy->name,
139             ],
140             'owned_by' => [
141                 'name' => $chapter->ownedBy->name,
142             ],
143             'pages' => [
144                 [
145                     'id'   => $page->id,
146                     'slug' => $page->slug,
147                     'name' => $page->name,
148                 ],
149             ],
150         ]);
151         $resp->assertJsonCount($chapter->pages()->count(), 'pages');
152     }
153
154     public function test_update_endpoint()
155     {
156         $this->actingAsApiEditor();
157         $chapter = $this->entities->chapter();
158         $details = [
159             'name'        => 'My updated API chapter',
160             'description' => 'A chapter updated via the API',
161             'tags'        => [
162                 [
163                     'name'  => 'freshtag',
164                     'value' => 'freshtagval',
165                 ],
166             ],
167             'priority'    => 15,
168         ];
169
170         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
171         $chapter->refresh();
172
173         $resp->assertStatus(200);
174         $resp->assertJson(array_merge($details, [
175             'id' => $chapter->id,
176             'slug' => $chapter->slug,
177             'book_id' => $chapter->book_id,
178             'description_html' => '<p>A chapter updated via the API</p>',
179         ]));
180         $this->assertActivityExists('chapter_update', $chapter);
181     }
182
183     public function test_update_endpoint_with_html()
184     {
185         $this->actingAsApiEditor();
186         $chapter = $this->entities->chapter();
187         $details = [
188             'name'             => 'My updated API chapter',
189             'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
190         ];
191
192         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
193         $resp->assertStatus(200);
194
195         $this->assertDatabaseHas('chapters', array_merge($details, [
196             'id' => $chapter->id, 'description' => 'A chapter updated via the API'
197         ]));
198     }
199
200     public function test_update_increments_updated_date_if_only_tags_are_sent()
201     {
202         $this->actingAsApiEditor();
203         $chapter = $this->entities->chapter();
204         DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
205
206         $details = [
207             'tags' => [['name' => 'Category', 'value' => 'Testing']],
208         ];
209
210         $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
211         $chapter->refresh();
212         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
213     }
214
215     public function test_update_with_book_id_moves_chapter()
216     {
217         $this->actingAsApiEditor();
218         $chapter = $this->entities->chapterHasPages();
219         $page = $chapter->pages()->first();
220         $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
221
222         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
223         $resp->assertOk();
224         $chapter->refresh();
225
226         $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]);
227         $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
228     }
229
230     public function test_update_with_new_book_id_requires_delete_permission()
231     {
232         $editor = $this->users->editor();
233         $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
234         $this->actingAs($editor);
235         $chapter = $this->entities->chapterHasPages();
236         $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
237
238         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
239         $this->assertPermissionError($resp);
240     }
241
242     public function test_delete_endpoint()
243     {
244         $this->actingAsApiEditor();
245         $chapter = $this->entities->chapter();
246         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
247
248         $resp->assertStatus(204);
249         $this->assertActivityExists('chapter_delete');
250     }
251
252     public function test_export_html_endpoint()
253     {
254         $this->actingAsApiEditor();
255         $chapter = $this->entities->chapter();
256
257         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
258         $resp->assertStatus(200);
259         $resp->assertSee($chapter->name);
260         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
261     }
262
263     public function test_export_plain_text_endpoint()
264     {
265         $this->actingAsApiEditor();
266         $chapter = $this->entities->chapter();
267
268         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
269         $resp->assertStatus(200);
270         $resp->assertSee($chapter->name);
271         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
272     }
273
274     public function test_export_pdf_endpoint()
275     {
276         $this->actingAsApiEditor();
277         $chapter = $this->entities->chapter();
278
279         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
280         $resp->assertStatus(200);
281         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
282     }
283
284     public function test_export_markdown_endpoint()
285     {
286         $this->actingAsApiEditor();
287         $chapter = Chapter::visible()->has('pages')->first();
288
289         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
290         $resp->assertStatus(200);
291         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
292         $resp->assertSee('# ' . $chapter->name);
293         $resp->assertSee('# ' . $chapter->pages()->first()->name);
294     }
295
296     public function test_cant_export_when_not_have_permission()
297     {
298         $types = ['html', 'plaintext', 'pdf', 'markdown'];
299         $this->actingAsApiEditor();
300         $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
301
302         $chapter = Chapter::visible()->has('pages')->first();
303         foreach ($types as $type) {
304             $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
305             $this->assertPermissionError($resp);
306         }
307     }
308 }