5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use Tests\Api\TestsApi;
8 use Tests\Exports\ZipTestHelper;
11 class ExportsApiTest extends TestCase
15 public function test_book_html_endpoint()
17 $this->actingAsApiEditor();
18 $book = $this->entities->book();
20 $resp = $this->get("/api/books/{$book->id}/export/html");
21 $resp->assertStatus(200);
22 $resp->assertSee($book->name);
23 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
26 public function test_book_plain_text_endpoint()
28 $this->actingAsApiEditor();
29 $book = $this->entities->book();
31 $resp = $this->get("/api/books/{$book->id}/export/plaintext");
32 $resp->assertStatus(200);
33 $resp->assertSee($book->name);
34 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
37 public function test_book_pdf_endpoint()
39 $this->actingAsApiEditor();
40 $book = $this->entities->book();
42 $resp = $this->get("/api/books/{$book->id}/export/pdf");
43 $resp->assertStatus(200);
44 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
47 public function test_book_markdown_endpoint()
49 $this->actingAsApiEditor();
50 $book = Book::visible()->has('pages')->has('chapters')->first();
52 $resp = $this->get("/api/books/{$book->id}/export/markdown");
53 $resp->assertStatus(200);
54 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
55 $resp->assertSee('# ' . $book->name);
56 $resp->assertSee('# ' . $book->pages()->first()->name);
57 $resp->assertSee('# ' . $book->chapters()->first()->name);
60 public function test_book_zip_endpoint()
62 $this->actingAsApiEditor();
63 $book = Book::visible()->has('pages')->has('chapters')->first();
65 $resp = $this->get("/api/books/{$book->id}/export/zip");
66 $resp->assertStatus(200);
67 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.zip"');
69 $zip = ZipTestHelper::extractFromZipResponse($resp);
70 $this->assertArrayHasKey('book', $zip->data);
73 public function test_chapter_html_endpoint()
75 $this->actingAsApiEditor();
76 $chapter = $this->entities->chapter();
78 $resp = $this->get("/api/chapters/{$chapter->id}/export/html");
79 $resp->assertStatus(200);
80 $resp->assertSee($chapter->name);
81 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
84 public function test_chapter_plain_text_endpoint()
86 $this->actingAsApiEditor();
87 $chapter = $this->entities->chapter();
89 $resp = $this->get("/api/chapters/{$chapter->id}/export/plaintext");
90 $resp->assertStatus(200);
91 $resp->assertSee($chapter->name);
92 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
95 public function test_chapter_pdf_endpoint()
97 $this->actingAsApiEditor();
98 $chapter = $this->entities->chapter();
100 $resp = $this->get("/api/chapters/{$chapter->id}/export/pdf");
101 $resp->assertStatus(200);
102 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
105 public function test_chapter_markdown_endpoint()
107 $this->actingAsApiEditor();
108 $chapter = Chapter::visible()->has('pages')->first();
110 $resp = $this->get("/api/chapters/{$chapter->id}/export/markdown");
111 $resp->assertStatus(200);
112 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
113 $resp->assertSee('# ' . $chapter->name);
114 $resp->assertSee('# ' . $chapter->pages()->first()->name);
117 public function test_chapter_zip_endpoint()
119 $this->actingAsApiEditor();
120 $chapter = Chapter::visible()->has('pages')->first();
122 $resp = $this->get("/api/chapters/{$chapter->id}/export/zip");
123 $resp->assertStatus(200);
124 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.zip"');
126 $zip = ZipTestHelper::extractFromZipResponse($resp);
127 $this->assertArrayHasKey('chapter', $zip->data);
130 public function test_page_html_endpoint()
132 $this->actingAsApiEditor();
133 $page = $this->entities->page();
135 $resp = $this->get("/api/pages/{$page->id}/export/html");
136 $resp->assertStatus(200);
137 $resp->assertSee($page->name);
138 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
141 public function test_page_plain_text_endpoint()
143 $this->actingAsApiEditor();
144 $page = $this->entities->page();
146 $resp = $this->get("/api/pages/{$page->id}/export/plaintext");
147 $resp->assertStatus(200);
148 $resp->assertSee($page->name);
149 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
152 public function test_page_pdf_endpoint()
154 $this->actingAsApiEditor();
155 $page = $this->entities->page();
157 $resp = $this->get("/api/pages/{$page->id}/export/pdf");
158 $resp->assertStatus(200);
159 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
162 public function test_page_markdown_endpoint()
164 $this->actingAsApiEditor();
165 $page = $this->entities->page();
167 $resp = $this->get("/api/pages/{$page->id}/export/markdown");
168 $resp->assertStatus(200);
169 $resp->assertSee('# ' . $page->name);
170 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
173 public function test_page_zip_endpoint()
175 $this->actingAsApiEditor();
176 $page = $this->entities->page();
178 $resp = $this->get("/api/pages/{$page->id}/export/zip");
179 $resp->assertStatus(200);
180 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.zip"');
182 $zip = ZipTestHelper::extractFromZipResponse($resp);
183 $this->assertArrayHasKey('page', $zip->data);
186 public function test_cant_export_when_not_have_permission()
188 $types = ['html', 'plaintext', 'pdf', 'markdown', 'zip'];
189 $this->actingAsApiEditor();
190 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
192 $book = $this->entities->book();
193 foreach ($types as $type) {
194 $resp = $this->get("/api/books/{$book->id}/export/{$type}");
195 $this->assertPermissionError($resp);
198 $chapter = Chapter::visible()->has('pages')->first();
199 foreach ($types as $type) {
200 $resp = $this->get("/api/chapters/{$chapter->id}/export/{$type}");
201 $this->assertPermissionError($resp);
204 $page = $this->entities->page();
205 foreach ($types as $type) {
206 $resp = $this->get("/api/pages/{$page->id}/export/{$type}");
207 $this->assertPermissionError($resp);