]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Cleaned up dark mode styles inc. setting browser color scheme
[bookstack] / tests / Api / BooksApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use Carbon\Carbon;
7 use Illuminate\Support\Facades\DB;
8 use Tests\TestCase;
9 use Tests\Uploads\UsesImages;
10
11 class BooksApiTest extends TestCase
12 {
13     use TestsApi;
14     use UsesImages;
15
16     protected string $baseEndpoint = '/api/books';
17
18     public function test_index_endpoint_returns_expected_book()
19     {
20         $this->actingAsApiEditor();
21         $firstBook = Book::query()->orderBy('id', 'asc')->first();
22
23         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
24         $resp->assertJson(['data' => [
25             [
26                 'id'   => $firstBook->id,
27                 'name' => $firstBook->name,
28                 'slug' => $firstBook->slug,
29             ],
30         ]]);
31     }
32
33     public function test_create_endpoint()
34     {
35         $this->actingAsApiEditor();
36         $details = [
37             'name'        => 'My API book',
38             'description' => 'A book created via the API',
39         ];
40
41         $resp = $this->postJson($this->baseEndpoint, $details);
42         $resp->assertStatus(200);
43         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
44         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
45         $this->assertActivityExists('book_create', $newItem);
46     }
47
48     public function test_book_name_needed_to_create()
49     {
50         $this->actingAsApiEditor();
51         $details = [
52             'description' => 'A book created via the API',
53         ];
54
55         $resp = $this->postJson($this->baseEndpoint, $details);
56         $resp->assertStatus(422);
57         $resp->assertJson([
58             'error' => [
59                 'message'    => 'The given data was invalid.',
60                 'validation' => [
61                     'name' => ['The name field is required.'],
62                 ],
63                 'code' => 422,
64             ],
65         ]);
66     }
67
68     public function test_read_endpoint()
69     {
70         $this->actingAsApiEditor();
71         $book = $this->entities->book();
72
73         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
74
75         $resp->assertStatus(200);
76         $resp->assertJson([
77             'id'         => $book->id,
78             'slug'       => $book->slug,
79             'created_by' => [
80                 'name' => $book->createdBy->name,
81             ],
82             'updated_by' => [
83                 'name' => $book->createdBy->name,
84             ],
85             'owned_by' => [
86                 'name' => $book->ownedBy->name,
87             ],
88         ]);
89     }
90
91     public function test_read_endpoint_includes_chapter_and_page_contents()
92     {
93         $this->actingAsApiEditor();
94         $book = $this->entities->bookHasChaptersAndPages();
95         $chapter = $book->chapters()->first();
96         $chapterPage = $chapter->pages()->first();
97
98         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
99
100         $directChildCount = $book->directPages()->count() + $book->chapters()->count();
101         $resp->assertStatus(200);
102         $resp->assertJsonCount($directChildCount, 'contents');
103         $resp->assertJson([
104             'contents' => [
105                 [
106                     'type' => 'chapter',
107                     'id' => $chapter->id,
108                     'name' => $chapter->name,
109                     'slug' => $chapter->slug,
110                     'pages' => [
111                         [
112                             'id' => $chapterPage->id,
113                             'name' => $chapterPage->name,
114                             'slug' => $chapterPage->slug,
115                         ]
116                     ]
117                 ]
118             ]
119         ]);
120     }
121
122     public function test_update_endpoint()
123     {
124         $this->actingAsApiEditor();
125         $book = $this->entities->book();
126         $details = [
127             'name'        => 'My updated API book',
128             'description' => 'A book created via the API',
129         ];
130
131         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
132         $book->refresh();
133
134         $resp->assertStatus(200);
135         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
136         $this->assertActivityExists('book_update', $book);
137     }
138
139     public function test_update_increments_updated_date_if_only_tags_are_sent()
140     {
141         $this->actingAsApiEditor();
142         $book = $this->entities->book();
143         DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
144
145         $details = [
146             'tags' => [['name' => 'Category', 'value' => 'Testing']],
147         ];
148
149         $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
150         $book->refresh();
151         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
152     }
153
154     public function test_update_cover_image_control()
155     {
156         $this->actingAsApiEditor();
157         /** @var Book $book */
158         $book = $this->entities->book();
159         $this->assertNull($book->cover);
160         $file = $this->getTestImage('image.png');
161
162         // Ensure cover image can be set via API
163         $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
164             'name'  => 'My updated API book with image',
165         ], [], ['image' => $file]);
166         $book->refresh();
167
168         $resp->assertStatus(200);
169         $this->assertNotNull($book->cover);
170
171         // Ensure further updates without image do not clear cover image
172         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
173             'name' => 'My updated book again',
174         ]);
175         $book->refresh();
176
177         $resp->assertStatus(200);
178         $this->assertNotNull($book->cover);
179
180         // Ensure update with null image property clears image
181         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
182             'image' => null,
183         ]);
184         $book->refresh();
185
186         $resp->assertStatus(200);
187         $this->assertNull($book->cover);
188     }
189
190     public function test_delete_endpoint()
191     {
192         $this->actingAsApiEditor();
193         $book = $this->entities->book();
194         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
195
196         $resp->assertStatus(204);
197         $this->assertActivityExists('book_delete');
198     }
199
200     public function test_export_html_endpoint()
201     {
202         $this->actingAsApiEditor();
203         $book = $this->entities->book();
204
205         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
206         $resp->assertStatus(200);
207         $resp->assertSee($book->name);
208         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
209     }
210
211     public function test_export_plain_text_endpoint()
212     {
213         $this->actingAsApiEditor();
214         $book = $this->entities->book();
215
216         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
217         $resp->assertStatus(200);
218         $resp->assertSee($book->name);
219         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
220     }
221
222     public function test_export_pdf_endpoint()
223     {
224         $this->actingAsApiEditor();
225         $book = $this->entities->book();
226
227         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
228         $resp->assertStatus(200);
229         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
230     }
231
232     public function test_export_markdown_endpoint()
233     {
234         $this->actingAsApiEditor();
235         $book = Book::visible()->has('pages')->has('chapters')->first();
236
237         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
238         $resp->assertStatus(200);
239         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
240         $resp->assertSee('# ' . $book->name);
241         $resp->assertSee('# ' . $book->pages()->first()->name);
242         $resp->assertSee('# ' . $book->chapters()->first()->name);
243     }
244
245     public function test_cant_export_when_not_have_permission()
246     {
247         $types = ['html', 'plaintext', 'pdf', 'markdown'];
248         $this->actingAsApiEditor();
249         $this->removePermissionFromUser($this->getEditor(), 'content-export');
250
251         $book = $this->entities->book();
252         foreach ($types as $type) {
253             $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
254             $this->assertPermissionError($resp);
255         }
256     }
257 }