]> BookStack Code Mirror - bookstack/blob - tests/Api/ChaptersApiTest.php
Cleaned up dark mode styles inc. setting browser color scheme
[bookstack] / tests / Api / ChaptersApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Chapter;
6 use Carbon\Carbon;
7 use Illuminate\Support\Facades\DB;
8 use Tests\TestCase;
9
10 class ChaptersApiTest extends TestCase
11 {
12     use TestsApi;
13
14     protected string $baseEndpoint = '/api/chapters';
15
16     public function test_index_endpoint_returns_expected_chapter()
17     {
18         $this->actingAsApiEditor();
19         $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
20
21         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
22         $resp->assertJson(['data' => [
23             [
24                 'id'       => $firstChapter->id,
25                 'name'     => $firstChapter->name,
26                 'slug'     => $firstChapter->slug,
27                 'book_id'  => $firstChapter->book->id,
28                 'priority' => $firstChapter->priority,
29             ],
30         ]]);
31     }
32
33     public function test_create_endpoint()
34     {
35         $this->actingAsApiEditor();
36         $book = $this->entities->book();
37         $details = [
38             'name'        => 'My API chapter',
39             'description' => 'A chapter created via the API',
40             'book_id'     => $book->id,
41             'tags'        => [
42                 [
43                     'name'  => 'tagname',
44                     'value' => 'tagvalue',
45                 ],
46             ],
47         ];
48
49         $resp = $this->postJson($this->baseEndpoint, $details);
50         $resp->assertStatus(200);
51         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
52         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
53         $this->assertDatabaseHas('tags', [
54             'entity_id'   => $newItem->id,
55             'entity_type' => $newItem->getMorphClass(),
56             'name'        => 'tagname',
57             'value'       => 'tagvalue',
58         ]);
59         $resp->assertJsonMissing(['pages' => []]);
60         $this->assertActivityExists('chapter_create', $newItem);
61     }
62
63     public function test_chapter_name_needed_to_create()
64     {
65         $this->actingAsApiEditor();
66         $book = $this->entities->book();
67         $details = [
68             'book_id'     => $book->id,
69             'description' => 'A chapter created via the API',
70         ];
71
72         $resp = $this->postJson($this->baseEndpoint, $details);
73         $resp->assertStatus(422);
74         $resp->assertJson($this->validationResponse([
75             'name' => ['The name field is required.'],
76         ]));
77     }
78
79     public function test_chapter_book_id_needed_to_create()
80     {
81         $this->actingAsApiEditor();
82         $details = [
83             'name'        => 'My api chapter',
84             'description' => 'A chapter created via the API',
85         ];
86
87         $resp = $this->postJson($this->baseEndpoint, $details);
88         $resp->assertStatus(422);
89         $resp->assertJson($this->validationResponse([
90             'book_id' => ['The book id field is required.'],
91         ]));
92     }
93
94     public function test_read_endpoint()
95     {
96         $this->actingAsApiEditor();
97         $chapter = $this->entities->chapter();
98         $page = $chapter->pages()->first();
99
100         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
101         $resp->assertStatus(200);
102         $resp->assertJson([
103             'id'         => $chapter->id,
104             'slug'       => $chapter->slug,
105             'created_by' => [
106                 'name' => $chapter->createdBy->name,
107             ],
108             'book_id'    => $chapter->book_id,
109             'updated_by' => [
110                 'name' => $chapter->createdBy->name,
111             ],
112             'owned_by' => [
113                 'name' => $chapter->ownedBy->name,
114             ],
115             'pages' => [
116                 [
117                     'id'   => $page->id,
118                     'slug' => $page->slug,
119                     'name' => $page->name,
120                 ],
121             ],
122         ]);
123         $resp->assertJsonCount($chapter->pages()->count(), 'pages');
124     }
125
126     public function test_update_endpoint()
127     {
128         $this->actingAsApiEditor();
129         $chapter = $this->entities->chapter();
130         $details = [
131             'name'        => 'My updated API chapter',
132             'description' => 'A chapter created via the API',
133             'tags'        => [
134                 [
135                     'name'  => 'freshtag',
136                     'value' => 'freshtagval',
137                 ],
138             ],
139         ];
140
141         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
142         $chapter->refresh();
143
144         $resp->assertStatus(200);
145         $resp->assertJson(array_merge($details, [
146             'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
147         ]));
148         $this->assertActivityExists('chapter_update', $chapter);
149     }
150
151     public function test_update_increments_updated_date_if_only_tags_are_sent()
152     {
153         $this->actingAsApiEditor();
154         $chapter = $this->entities->chapter();
155         DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
156
157         $details = [
158             'tags' => [['name' => 'Category', 'value' => 'Testing']],
159         ];
160
161         $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
162         $chapter->refresh();
163         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
164     }
165
166     public function test_delete_endpoint()
167     {
168         $this->actingAsApiEditor();
169         $chapter = $this->entities->chapter();
170         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
171
172         $resp->assertStatus(204);
173         $this->assertActivityExists('chapter_delete');
174     }
175
176     public function test_export_html_endpoint()
177     {
178         $this->actingAsApiEditor();
179         $chapter = $this->entities->chapter();
180
181         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
182         $resp->assertStatus(200);
183         $resp->assertSee($chapter->name);
184         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
185     }
186
187     public function test_export_plain_text_endpoint()
188     {
189         $this->actingAsApiEditor();
190         $chapter = $this->entities->chapter();
191
192         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
193         $resp->assertStatus(200);
194         $resp->assertSee($chapter->name);
195         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
196     }
197
198     public function test_export_pdf_endpoint()
199     {
200         $this->actingAsApiEditor();
201         $chapter = $this->entities->chapter();
202
203         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
204         $resp->assertStatus(200);
205         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
206     }
207
208     public function test_export_markdown_endpoint()
209     {
210         $this->actingAsApiEditor();
211         $chapter = Chapter::visible()->has('pages')->first();
212
213         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
214         $resp->assertStatus(200);
215         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
216         $resp->assertSee('# ' . $chapter->name);
217         $resp->assertSee('# ' . $chapter->pages()->first()->name);
218     }
219
220     public function test_cant_export_when_not_have_permission()
221     {
222         $types = ['html', 'plaintext', 'pdf', 'markdown'];
223         $this->actingAsApiEditor();
224         $this->removePermissionFromUser($this->getEditor(), 'content-export');
225
226         $chapter = Chapter::visible()->has('pages')->first();
227         foreach ($types as $type) {
228             $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
229             $this->assertPermissionError($resp);
230         }
231     }
232 }