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_read_endpoint_provides_raw_html()
164 $html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
166 $this->actingAsApiEditor();
167 $page = $this->entities->page();
171 $resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
172 $this->assertEquals($html, $resp->json('raw_html'));
173 $this->assertNotEquals($html, $resp->json('html'));
176 public function test_read_endpoint_returns_not_found()
178 $this->actingAsApiEditor();
179 // get an id that is not used
180 $id = Page::orderBy('id', 'desc')->first()->id + 1;
181 $this->assertNull(Page::find($id));
183 $resp = $this->getJson($this->baseEndpoint . "/$id");
185 $resp->assertNotFound();
186 $this->assertNull($resp->json('id'));
187 $resp->assertJsonIsObject('error');
188 $resp->assertJsonStructure([
194 $this->assertSame(404, $resp->json('error')['code']);
197 public function test_update_endpoint()
199 $this->actingAsApiEditor();
200 $page = $this->entities->page();
202 'name' => 'My updated API page',
203 'html' => '<p>A page created via the API</p>',
206 'name' => 'freshtag',
207 'value' => 'freshtagval',
212 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
215 $resp->assertStatus(200);
216 unset($details['html']);
217 $resp->assertJson(array_merge($details, [
218 'id' => $page->id, 'slug' => $page->slug, 'book_id' => $page->book_id,
220 $this->assertActivityExists('page_update', $page);
223 public function test_providing_new_chapter_id_on_update_will_move_page()
225 $this->actingAsApiEditor();
226 $page = $this->entities->page();
227 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
229 'name' => 'My updated API page',
230 'chapter_id' => $chapter->id,
231 'html' => '<p>A page created via the API</p>',
234 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
235 $resp->assertStatus(200);
237 'chapter_id' => $chapter->id,
238 'book_id' => $chapter->book_id,
242 public function test_providing_move_via_update_requires_page_create_permission_on_new_parent()
244 $this->actingAsApiEditor();
245 $page = $this->entities->page();
246 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
247 $this->permissions->setEntityPermissions($chapter, ['view'], [$this->users->editor()->roles()->first()]);
249 'name' => 'My updated API page',
250 'chapter_id' => $chapter->id,
251 'html' => '<p>A page created via the API</p>',
254 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
255 $resp->assertStatus(403);
258 public function test_update_endpoint_does_not_wipe_content_if_no_html_or_md_provided()
260 $this->actingAsApiEditor();
261 $page = $this->entities->page();
262 $originalContent = $page->html;
264 'name' => 'My updated API page',
267 'name' => 'freshtag',
268 'value' => 'freshtagval',
273 $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
276 $this->assertEquals($originalContent, $page->html);
279 public function test_update_increments_updated_date_if_only_tags_are_sent()
281 $this->actingAsApiEditor();
282 $page = $this->entities->page();
283 DB::table('pages')->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
286 'tags' => [['name' => 'Category', 'value' => 'Testing']],
289 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
293 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
296 public function test_delete_endpoint()
298 $this->actingAsApiEditor();
299 $page = $this->entities->page();
300 $resp = $this->deleteJson($this->baseEndpoint . "/{$page->id}");
302 $resp->assertStatus(204);
303 $this->assertActivityExists('page_delete', $page);
306 public function test_export_html_endpoint()
308 $this->actingAsApiEditor();
309 $page = $this->entities->page();
311 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
312 $resp->assertStatus(200);
313 $resp->assertSee($page->name);
314 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
317 public function test_export_plain_text_endpoint()
319 $this->actingAsApiEditor();
320 $page = $this->entities->page();
322 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
323 $resp->assertStatus(200);
324 $resp->assertSee($page->name);
325 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
328 public function test_export_pdf_endpoint()
330 $this->actingAsApiEditor();
331 $page = $this->entities->page();
333 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
334 $resp->assertStatus(200);
335 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
338 public function test_export_markdown_endpoint()
340 $this->actingAsApiEditor();
341 $page = $this->entities->page();
343 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/markdown");
344 $resp->assertStatus(200);
345 $resp->assertSee('# ' . $page->name);
346 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
349 public function test_cant_export_when_not_have_permission()
351 $types = ['html', 'plaintext', 'pdf', 'markdown'];
352 $this->actingAsApiEditor();
353 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
355 $page = $this->entities->page();
356 foreach ($types as $type) {
357 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/{$type}");
358 $this->assertPermissionError($resp);