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