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