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