5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
8 use Illuminate\Support\Facades\DB;
11 class PagesApiTest extends TestCase
15 protected string $baseEndpoint = '/api/pages';
17 public function test_index_endpoint_returns_expected_page()
19 $this->actingAsApiEditor();
20 $firstPage = Page::query()->orderBy('id', 'asc')->first();
22 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23 $resp->assertJson(['data' => [
25 'id' => $firstPage->id,
26 'name' => $firstPage->name,
27 'slug' => $firstPage->slug,
28 'book_id' => $firstPage->book->id,
29 'priority' => $firstPage->priority,
34 public function test_create_endpoint()
36 $this->actingAsApiEditor();
37 $book = $this->entities->book();
39 'name' => 'My API page',
40 'book_id' => $book->id,
41 'html' => '<p>My new page content</p>',
45 'value' => 'tagvalue',
50 $resp = $this->postJson($this->baseEndpoint, $details);
51 unset($details['html']);
52 $resp->assertStatus(200);
53 $newItem = Page::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
54 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
55 $this->assertDatabaseHas('tags', [
56 'entity_id' => $newItem->id,
57 'entity_type' => $newItem->getMorphClass(),
59 'value' => 'tagvalue',
61 $resp->assertSeeText('My new page content');
62 $resp->assertJsonMissing(['book' => []]);
63 $this->assertActivityExists('page_create', $newItem);
66 public function test_page_name_needed_to_create()
68 $this->actingAsApiEditor();
69 $book = $this->entities->book();
71 'book_id' => $book->id,
72 'html' => '<p>A page created via the API</p>',
75 $resp = $this->postJson($this->baseEndpoint, $details);
76 $resp->assertStatus(422);
77 $resp->assertJson($this->validationResponse([
78 'name' => ['The name field is required.'],
82 public function test_book_id_or_chapter_id_needed_to_create()
84 $this->actingAsApiEditor();
86 'name' => 'My api page',
87 'html' => '<p>A page created via the API</p>',
90 $resp = $this->postJson($this->baseEndpoint, $details);
91 $resp->assertStatus(422);
92 $resp->assertJson($this->validationResponse([
93 'book_id' => ['The book id field is required when chapter id is not present.'],
94 'chapter_id' => ['The chapter id field is required when book id is not present.'],
97 $chapter = $this->entities->chapter();
98 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['chapter_id' => $chapter->id]));
99 $resp->assertStatus(200);
101 $book = $this->entities->book();
102 $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['book_id' => $book->id]));
103 $resp->assertStatus(200);
106 public function test_markdown_can_be_provided_for_create()
108 $this->actingAsApiEditor();
109 $book = $this->entities->book();
111 'book_id' => $book->id,
112 'name' => 'My api page',
113 'markdown' => "# A new API page \n[link](https://example.com)",
116 $resp = $this->postJson($this->baseEndpoint, $details);
117 $resp->assertJson(['markdown' => $details['markdown']]);
119 $respHtml = $resp->json('html');
120 $this->assertStringContainsString('new API page</h1>', $respHtml);
121 $this->assertStringContainsString('link</a>', $respHtml);
122 $this->assertStringContainsString('href="https://example.com"', $respHtml);
125 public function test_read_endpoint()
127 $this->actingAsApiEditor();
128 $page = $this->entities->page();
130 $resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
131 $resp->assertStatus(200);
134 'slug' => $page->slug,
136 'name' => $page->createdBy->name,
138 'book_id' => $page->book_id,
140 'name' => $page->createdBy->name,
143 'name' => $page->ownedBy->name,
148 public function test_read_endpoint_provides_rendered_html()
150 $this->actingAsApiEditor();
151 $page = $this->entities->page();
152 $page->html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
155 $resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
156 $html = $resp->json('html');
157 $this->assertStringNotContainsString('script', $html);
158 $this->assertStringContainsString('Hello', $html);
159 $this->assertStringContainsString('testing', $html);
162 public function test_update_endpoint()
164 $this->actingAsApiEditor();
165 $page = $this->entities->page();
167 'name' => 'My updated API page',
168 'html' => '<p>A page created via the API</p>',
171 'name' => 'freshtag',
172 'value' => 'freshtagval',
177 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
180 $resp->assertStatus(200);
181 unset($details['html']);
182 $resp->assertJson(array_merge($details, [
183 'id' => $page->id, 'slug' => $page->slug, 'book_id' => $page->book_id,
185 $this->assertActivityExists('page_update', $page);
188 public function test_providing_new_chapter_id_on_update_will_move_page()
190 $this->actingAsApiEditor();
191 $page = $this->entities->page();
192 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
194 'name' => 'My updated API page',
195 'chapter_id' => $chapter->id,
196 'html' => '<p>A page created via the API</p>',
199 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
200 $resp->assertStatus(200);
202 'chapter_id' => $chapter->id,
203 'book_id' => $chapter->book_id,
207 public function test_providing_move_via_update_requires_page_create_permission_on_new_parent()
209 $this->actingAsApiEditor();
210 $page = $this->entities->page();
211 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
212 $this->permissions->setEntityPermissions($chapter, ['view'], [$this->users->editor()->roles()->first()]);
214 'name' => 'My updated API page',
215 'chapter_id' => $chapter->id,
216 'html' => '<p>A page created via the API</p>',
219 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
220 $resp->assertStatus(403);
223 public function test_update_endpoint_does_not_wipe_content_if_no_html_or_md_provided()
225 $this->actingAsApiEditor();
226 $page = $this->entities->page();
227 $originalContent = $page->html;
229 'name' => 'My updated API page',
232 'name' => 'freshtag',
233 'value' => 'freshtagval',
238 $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
241 $this->assertEquals($originalContent, $page->html);
244 public function test_update_increments_updated_date_if_only_tags_are_sent()
246 $this->actingAsApiEditor();
247 $page = $this->entities->page();
248 DB::table('pages')->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
251 'tags' => [['name' => 'Category', 'value' => 'Testing']],
254 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
258 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
261 public function test_delete_endpoint()
263 $this->actingAsApiEditor();
264 $page = $this->entities->page();
265 $resp = $this->deleteJson($this->baseEndpoint . "/{$page->id}");
267 $resp->assertStatus(204);
268 $this->assertActivityExists('page_delete', $page);
271 public function test_export_html_endpoint()
273 $this->actingAsApiEditor();
274 $page = $this->entities->page();
276 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
277 $resp->assertStatus(200);
278 $resp->assertSee($page->name);
279 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
282 public function test_export_plain_text_endpoint()
284 $this->actingAsApiEditor();
285 $page = $this->entities->page();
287 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
288 $resp->assertStatus(200);
289 $resp->assertSee($page->name);
290 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
293 public function test_export_pdf_endpoint()
295 $this->actingAsApiEditor();
296 $page = $this->entities->page();
298 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
299 $resp->assertStatus(200);
300 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
303 public function test_export_markdown_endpoint()
305 $this->actingAsApiEditor();
306 $page = $this->entities->page();
308 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/markdown");
309 $resp->assertStatus(200);
310 $resp->assertSee('# ' . $page->name);
311 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
314 public function test_cant_export_when_not_have_permission()
316 $types = ['html', 'plaintext', 'pdf', 'markdown'];
317 $this->actingAsApiEditor();
318 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
320 $page = $this->entities->page();
321 foreach ($types as $type) {
322 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/{$type}");
323 $this->assertPermissionError($resp);