]> BookStack Code Mirror - bookstack/blob - tests/Api/ChaptersApiTest.php
Fixed phpstan wanring about usage of static
[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 = Book::query()->first();
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 = Book::query()->first();
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 = Chapter::visible()->first();
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 = Chapter::visible()->first();
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 = Chapter::visible()->first();
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_delete_endpoint()
168     {
169         $this->actingAsApiEditor();
170         $chapter = Chapter::visible()->first();
171         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
172
173         $resp->assertStatus(204);
174         $this->assertActivityExists('chapter_delete');
175     }
176
177     public function test_export_html_endpoint()
178     {
179         $this->actingAsApiEditor();
180         $chapter = Chapter::visible()->first();
181
182         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
183         $resp->assertStatus(200);
184         $resp->assertSee($chapter->name);
185         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
186     }
187
188     public function test_export_plain_text_endpoint()
189     {
190         $this->actingAsApiEditor();
191         $chapter = Chapter::visible()->first();
192
193         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
194         $resp->assertStatus(200);
195         $resp->assertSee($chapter->name);
196         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
197     }
198
199     public function test_export_pdf_endpoint()
200     {
201         $this->actingAsApiEditor();
202         $chapter = Chapter::visible()->first();
203
204         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
205         $resp->assertStatus(200);
206         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
207     }
208
209     public function test_export_markdown_endpoint()
210     {
211         $this->actingAsApiEditor();
212         $chapter = Chapter::visible()->has('pages')->first();
213
214         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
215         $resp->assertStatus(200);
216         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
217         $resp->assertSee('# ' . $chapter->name);
218         $resp->assertSee('# ' . $chapter->pages()->first()->name);
219     }
220
221     public function test_cant_export_when_not_have_permission()
222     {
223         $types = ['html', 'plaintext', 'pdf', 'markdown'];
224         $this->actingAsApiEditor();
225         $this->removePermissionFromUser($this->getEditor(), 'content-export');
226
227         $chapter = Chapter::visible()->has('pages')->first();
228         foreach ($types as $type) {
229             $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
230             $this->assertPermissionError($resp);
231         }
232     }
233 }