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_returns_not_found()
164 $this->actingAsApiEditor();
165 // get an id that is not used
166 $id = Page::orderBy('id', 'desc')->first()->id + 1;
167 $this->assertNull(Page::find($id));
169 $resp = $this->getJson($this->baseEndpoint . "/$id");
171 $resp->assertNotFound();
172 $this->assertNull($resp->json('id'));
173 $resp->assertJsonIsObject('error');
174 $resp->assertJsonStructure([
180 $this->assertSame(404, $resp->json('error')['code']);
183 public function test_update_endpoint()
185 $this->actingAsApiEditor();
186 $page = $this->entities->page();
188 'name' => 'My updated API page',
189 'html' => '<p>A page created via the API</p>',
192 'name' => 'freshtag',
193 'value' => 'freshtagval',
198 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
201 $resp->assertStatus(200);
202 unset($details['html']);
203 $resp->assertJson(array_merge($details, [
204 'id' => $page->id, 'slug' => $page->slug, 'book_id' => $page->book_id,
206 $this->assertActivityExists('page_update', $page);
209 public function test_providing_new_chapter_id_on_update_will_move_page()
211 $this->actingAsApiEditor();
212 $page = $this->entities->page();
213 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
215 'name' => 'My updated API page',
216 'chapter_id' => $chapter->id,
217 'html' => '<p>A page created via the API</p>',
220 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
221 $resp->assertStatus(200);
223 'chapter_id' => $chapter->id,
224 'book_id' => $chapter->book_id,
228 public function test_providing_move_via_update_requires_page_create_permission_on_new_parent()
230 $this->actingAsApiEditor();
231 $page = $this->entities->page();
232 $chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
233 $this->permissions->setEntityPermissions($chapter, ['view'], [$this->users->editor()->roles()->first()]);
235 'name' => 'My updated API page',
236 'chapter_id' => $chapter->id,
237 'html' => '<p>A page created via the API</p>',
240 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
241 $resp->assertStatus(403);
244 public function test_update_endpoint_does_not_wipe_content_if_no_html_or_md_provided()
246 $this->actingAsApiEditor();
247 $page = $this->entities->page();
248 $originalContent = $page->html;
250 'name' => 'My updated API page',
253 'name' => 'freshtag',
254 'value' => 'freshtagval',
259 $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
262 $this->assertEquals($originalContent, $page->html);
265 public function test_update_increments_updated_date_if_only_tags_are_sent()
267 $this->actingAsApiEditor();
268 $page = $this->entities->page();
269 DB::table('pages')->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
272 'tags' => [['name' => 'Category', 'value' => 'Testing']],
275 $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
279 $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
282 public function test_delete_endpoint()
284 $this->actingAsApiEditor();
285 $page = $this->entities->page();
286 $resp = $this->deleteJson($this->baseEndpoint . "/{$page->id}");
288 $resp->assertStatus(204);
289 $this->assertActivityExists('page_delete', $page);
292 public function test_export_html_endpoint()
294 $this->actingAsApiEditor();
295 $page = $this->entities->page();
297 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
298 $resp->assertStatus(200);
299 $resp->assertSee($page->name);
300 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
303 public function test_export_plain_text_endpoint()
305 $this->actingAsApiEditor();
306 $page = $this->entities->page();
308 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
309 $resp->assertStatus(200);
310 $resp->assertSee($page->name);
311 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
314 public function test_export_pdf_endpoint()
316 $this->actingAsApiEditor();
317 $page = $this->entities->page();
319 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
320 $resp->assertStatus(200);
321 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
324 public function test_export_markdown_endpoint()
326 $this->actingAsApiEditor();
327 $page = $this->entities->page();
329 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/markdown");
330 $resp->assertStatus(200);
331 $resp->assertSee('# ' . $page->name);
332 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
335 public function test_cant_export_when_not_have_permission()
337 $types = ['html', 'plaintext', 'pdf', 'markdown'];
338 $this->actingAsApiEditor();
339 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
341 $page = $this->entities->page();
342 foreach ($types as $type) {
343 $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/{$type}");
344 $this->assertPermissionError($resp);