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