5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use Tests\Exports\ZipTestHelper;
10 class ExportsApiTest extends TestCase
14 public function test_book_html_endpoint()
16 $this->actingAsApiEditor();
17 $book = $this->entities->book();
19 $resp = $this->get("/api/books/{$book->id}/export/html");
20 $resp->assertStatus(200);
21 $resp->assertSee($book->name);
22 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
25 public function test_book_plain_text_endpoint()
27 $this->actingAsApiEditor();
28 $book = $this->entities->book();
30 $resp = $this->get("/api/books/{$book->id}/export/plaintext");
31 $resp->assertStatus(200);
32 $resp->assertSee($book->name);
33 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
36 public function test_book_pdf_endpoint()
38 $this->actingAsApiEditor();
39 $book = $this->entities->book();
41 $resp = $this->get("/api/books/{$book->id}/export/pdf");
42 $resp->assertStatus(200);
43 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
46 public function test_book_markdown_endpoint()
48 $this->actingAsApiEditor();
49 $book = Book::visible()->has('pages')->has('chapters')->first();
51 $resp = $this->get("/api/books/{$book->id}/export/markdown");
52 $resp->assertStatus(200);
53 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
54 $resp->assertSee('# ' . $book->name);
55 $resp->assertSee('# ' . $book->pages()->first()->name);
56 $resp->assertSee('# ' . $book->chapters()->first()->name);
59 public function test_book_zip_endpoint()
61 $this->actingAsApiEditor();
62 $book = Book::visible()->has('pages')->has('chapters')->first();
64 $resp = $this->get("/api/books/{$book->id}/export/zip");
65 $resp->assertStatus(200);
66 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.zip"');
68 $zip = ZipTestHelper::extractFromZipResponse($resp);
69 $this->assertArrayHasKey('book', $zip->data);
72 public function test_chapter_html_endpoint()
74 $this->actingAsApiEditor();
75 $chapter = $this->entities->chapter();
77 $resp = $this->get("/api/chapters/{$chapter->id}/export/html");
78 $resp->assertStatus(200);
79 $resp->assertSee($chapter->name);
80 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
83 public function test_chapter_plain_text_endpoint()
85 $this->actingAsApiEditor();
86 $chapter = $this->entities->chapter();
88 $resp = $this->get("/api/chapters/{$chapter->id}/export/plaintext");
89 $resp->assertStatus(200);
90 $resp->assertSee($chapter->name);
91 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
94 public function test_chapter_pdf_endpoint()
96 $this->actingAsApiEditor();
97 $chapter = $this->entities->chapter();
99 $resp = $this->get("/api/chapters/{$chapter->id}/export/pdf");
100 $resp->assertStatus(200);
101 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
104 public function test_chapter_markdown_endpoint()
106 $this->actingAsApiEditor();
107 $chapter = Chapter::visible()->has('pages')->first();
109 $resp = $this->get("/api/chapters/{$chapter->id}/export/markdown");
110 $resp->assertStatus(200);
111 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
112 $resp->assertSee('# ' . $chapter->name);
113 $resp->assertSee('# ' . $chapter->pages()->first()->name);
116 public function test_chapter_zip_endpoint()
118 $this->actingAsApiEditor();
119 $chapter = Chapter::visible()->has('pages')->first();
121 $resp = $this->get("/api/chapters/{$chapter->id}/export/zip");
122 $resp->assertStatus(200);
123 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.zip"');
125 $zip = ZipTestHelper::extractFromZipResponse($resp);
126 $this->assertArrayHasKey('chapter', $zip->data);
129 public function test_page_html_endpoint()
131 $this->actingAsApiEditor();
132 $page = $this->entities->page();
134 $resp = $this->get("/api/pages/{$page->id}/export/html");
135 $resp->assertStatus(200);
136 $resp->assertSee($page->name);
137 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
140 public function test_page_plain_text_endpoint()
142 $this->actingAsApiEditor();
143 $page = $this->entities->page();
145 $resp = $this->get("/api/pages/{$page->id}/export/plaintext");
146 $resp->assertStatus(200);
147 $resp->assertSee($page->name);
148 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
151 public function test_page_pdf_endpoint()
153 $this->actingAsApiEditor();
154 $page = $this->entities->page();
156 $resp = $this->get("/api/pages/{$page->id}/export/pdf");
157 $resp->assertStatus(200);
158 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
161 public function test_page_markdown_endpoint()
163 $this->actingAsApiEditor();
164 $page = $this->entities->page();
166 $resp = $this->get("/api/pages/{$page->id}/export/markdown");
167 $resp->assertStatus(200);
168 $resp->assertSee('# ' . $page->name);
169 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
172 public function test_page_zip_endpoint()
174 $this->actingAsApiEditor();
175 $page = $this->entities->page();
177 $resp = $this->get("/api/pages/{$page->id}/export/zip");
178 $resp->assertStatus(200);
179 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.zip"');
181 $zip = ZipTestHelper::extractFromZipResponse($resp);
182 $this->assertArrayHasKey('page', $zip->data);
185 public function test_cant_export_when_not_have_permission()
187 $types = ['html', 'plaintext', 'pdf', 'markdown', 'zip'];
188 $this->actingAsApiEditor();
189 $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
191 $book = $this->entities->book();
192 foreach ($types as $type) {
193 $resp = $this->get("/api/books/{$book->id}/export/{$type}");
194 $this->assertPermissionError($resp);
197 $chapter = Chapter::visible()->has('pages')->first();
198 foreach ($types as $type) {
199 $resp = $this->get("/api/chapters/{$chapter->id}/export/{$type}");
200 $this->assertPermissionError($resp);
203 $page = $this->entities->page();
204 foreach ($types as $type) {
205 $resp = $this->get("/api/pages/{$page->id}/export/{$type}");
206 $this->assertPermissionError($resp);