]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Fix Crowdin name in the language_request issue template
[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
10 class BooksApiTest extends TestCase
11 {
12     use TestsApi;
13
14     protected string $baseEndpoint = '/api/books';
15
16     public function test_index_endpoint_returns_expected_book()
17     {
18         $this->actingAsApiEditor();
19         $firstBook = Book::query()->orderBy('id', 'asc')->first();
20
21         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
22         $resp->assertJson(['data' => [
23             [
24                 'id'   => $firstBook->id,
25                 'name' => $firstBook->name,
26                 'slug' => $firstBook->slug,
27             ],
28         ]]);
29     }
30
31     public function test_create_endpoint()
32     {
33         $this->actingAsApiEditor();
34         $details = [
35             'name'        => 'My API book',
36             'description' => 'A book created via the API',
37         ];
38
39         $resp = $this->postJson($this->baseEndpoint, $details);
40         $resp->assertStatus(200);
41         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
42         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
43         $this->assertActivityExists('book_create', $newItem);
44     }
45
46     public function test_book_name_needed_to_create()
47     {
48         $this->actingAsApiEditor();
49         $details = [
50             'description' => 'A book created via the API',
51         ];
52
53         $resp = $this->postJson($this->baseEndpoint, $details);
54         $resp->assertStatus(422);
55         $resp->assertJson([
56             'error' => [
57                 'message'    => 'The given data was invalid.',
58                 'validation' => [
59                     'name' => ['The name field is required.'],
60                 ],
61                 'code' => 422,
62             ],
63         ]);
64     }
65
66     public function test_read_endpoint()
67     {
68         $this->actingAsApiEditor();
69         $book = Book::visible()->first();
70
71         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
72
73         $resp->assertStatus(200);
74         $resp->assertJson([
75             'id'         => $book->id,
76             'slug'       => $book->slug,
77             'created_by' => [
78                 'name' => $book->createdBy->name,
79             ],
80             'updated_by' => [
81                 'name' => $book->createdBy->name,
82             ],
83             'owned_by' => [
84                 'name' => $book->ownedBy->name,
85             ],
86         ]);
87     }
88
89     public function test_update_endpoint()
90     {
91         $this->actingAsApiEditor();
92         $book = Book::visible()->first();
93         $details = [
94             'name'        => 'My updated API book',
95             'description' => 'A book created via the API',
96         ];
97
98         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
99         $book->refresh();
100
101         $resp->assertStatus(200);
102         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
103         $this->assertActivityExists('book_update', $book);
104     }
105
106     public function test_update_increments_updated_date_if_only_tags_are_sent()
107     {
108         $this->actingAsApiEditor();
109         $book = Book::visible()->first();
110         DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
111
112         $details = [
113             'tags' => [['name' => 'Category', 'value' => 'Testing']],
114         ];
115
116         $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
117         $book->refresh();
118         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
119     }
120
121     public function test_delete_endpoint()
122     {
123         $this->actingAsApiEditor();
124         $book = Book::visible()->first();
125         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
126
127         $resp->assertStatus(204);
128         $this->assertActivityExists('book_delete');
129     }
130
131     public function test_export_html_endpoint()
132     {
133         $this->actingAsApiEditor();
134         $book = Book::visible()->first();
135
136         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
137         $resp->assertStatus(200);
138         $resp->assertSee($book->name);
139         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
140     }
141
142     public function test_export_plain_text_endpoint()
143     {
144         $this->actingAsApiEditor();
145         $book = Book::visible()->first();
146
147         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
148         $resp->assertStatus(200);
149         $resp->assertSee($book->name);
150         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
151     }
152
153     public function test_export_pdf_endpoint()
154     {
155         $this->actingAsApiEditor();
156         $book = Book::visible()->first();
157
158         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
159         $resp->assertStatus(200);
160         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
161     }
162
163     public function test_export_markdown_endpoint()
164     {
165         $this->actingAsApiEditor();
166         $book = Book::visible()->has('pages')->has('chapters')->first();
167
168         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
169         $resp->assertStatus(200);
170         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
171         $resp->assertSee('# ' . $book->name);
172         $resp->assertSee('# ' . $book->pages()->first()->name);
173         $resp->assertSee('# ' . $book->chapters()->first()->name);
174     }
175
176     public function test_cant_export_when_not_have_permission()
177     {
178         $types = ['html', 'plaintext', 'pdf', 'markdown'];
179         $this->actingAsApiEditor();
180         $this->removePermissionFromUser($this->getEditor(), 'content-export');
181
182         $book = Book::visible()->first();
183         foreach ($types as $type) {
184             $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
185             $this->assertPermissionError($resp);
186         }
187     }
188 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.