]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Applied styleci changes for conversion work
[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 = Book::visible()->first();
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_update_endpoint()
92     {
93         $this->actingAsApiEditor();
94         $book = Book::visible()->first();
95         $details = [
96             'name'        => 'My updated API book',
97             'description' => 'A book created via the API',
98         ];
99
100         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
101         $book->refresh();
102
103         $resp->assertStatus(200);
104         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
105         $this->assertActivityExists('book_update', $book);
106     }
107
108     public function test_update_increments_updated_date_if_only_tags_are_sent()
109     {
110         $this->actingAsApiEditor();
111         $book = Book::visible()->first();
112         DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
113
114         $details = [
115             'tags' => [['name' => 'Category', 'value' => 'Testing']],
116         ];
117
118         $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
119         $book->refresh();
120         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
121     }
122
123     public function test_update_cover_image_control()
124     {
125         $this->actingAsApiEditor();
126         /** @var Book $book */
127         $book = Book::visible()->first();
128         $this->assertNull($book->cover);
129         $file = $this->getTestImage('image.png');
130
131         // Ensure cover image can be set via API
132         $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
133             'name'  => 'My updated API book with image',
134         ], [], ['image' => $file]);
135         $book->refresh();
136
137         $resp->assertStatus(200);
138         $this->assertNotNull($book->cover);
139
140         // Ensure further updates without image do not clear cover image
141         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
142             'name' => 'My updated book again',
143         ]);
144         $book->refresh();
145
146         $resp->assertStatus(200);
147         $this->assertNotNull($book->cover);
148
149         // Ensure update with null image property clears image
150         $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
151             'image' => null,
152         ]);
153         $book->refresh();
154
155         $resp->assertStatus(200);
156         $this->assertNull($book->cover);
157     }
158
159     public function test_delete_endpoint()
160     {
161         $this->actingAsApiEditor();
162         $book = Book::visible()->first();
163         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
164
165         $resp->assertStatus(204);
166         $this->assertActivityExists('book_delete');
167     }
168
169     public function test_export_html_endpoint()
170     {
171         $this->actingAsApiEditor();
172         $book = Book::visible()->first();
173
174         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
175         $resp->assertStatus(200);
176         $resp->assertSee($book->name);
177         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
178     }
179
180     public function test_export_plain_text_endpoint()
181     {
182         $this->actingAsApiEditor();
183         $book = Book::visible()->first();
184
185         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
186         $resp->assertStatus(200);
187         $resp->assertSee($book->name);
188         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
189     }
190
191     public function test_export_pdf_endpoint()
192     {
193         $this->actingAsApiEditor();
194         $book = Book::visible()->first();
195
196         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
197         $resp->assertStatus(200);
198         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
199     }
200
201     public function test_export_markdown_endpoint()
202     {
203         $this->actingAsApiEditor();
204         $book = Book::visible()->has('pages')->has('chapters')->first();
205
206         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
207         $resp->assertStatus(200);
208         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
209         $resp->assertSee('# ' . $book->name);
210         $resp->assertSee('# ' . $book->pages()->first()->name);
211         $resp->assertSee('# ' . $book->chapters()->first()->name);
212     }
213
214     public function test_cant_export_when_not_have_permission()
215     {
216         $types = ['html', 'plaintext', 'pdf', 'markdown'];
217         $this->actingAsApiEditor();
218         $this->removePermissionFromUser($this->getEditor(), 'content-export');
219
220         $book = Book::visible()->first();
221         foreach ($types as $type) {
222             $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
223             $this->assertPermissionError($resp);
224         }
225     }
226 }