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