]> BookStack Code Mirror - bookstack/blob - tests/Api/ExportsApiTest.php
API: Added zip export tests, reorganised tests
[bookstack] / tests / Api / ExportsApiTest.php
1 <?php
2
3 namespace Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use Tests\Api\TestsApi;
8 use Tests\Exports\ZipTestHelper;
9 use Tests\TestCase;
10
11 class ExportsApiTest extends TestCase
12 {
13     use TestsApi;
14
15     public function test_book_html_endpoint()
16     {
17         $this->actingAsApiEditor();
18         $book = $this->entities->book();
19
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"');
24     }
25
26     public function test_book_plain_text_endpoint()
27     {
28         $this->actingAsApiEditor();
29         $book = $this->entities->book();
30
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"');
35     }
36
37     public function test_book_pdf_endpoint()
38     {
39         $this->actingAsApiEditor();
40         $book = $this->entities->book();
41
42         $resp = $this->get("/api/books/{$book->id}/export/pdf");
43         $resp->assertStatus(200);
44         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
45     }
46
47     public function test_book_markdown_endpoint()
48     {
49         $this->actingAsApiEditor();
50         $book = Book::visible()->has('pages')->has('chapters')->first();
51
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);
58     }
59
60     public function test_book_zip_endpoint()
61     {
62         $this->actingAsApiEditor();
63         $book = Book::visible()->has('pages')->has('chapters')->first();
64
65         $resp = $this->get("/api/books/{$book->id}/export/zip");
66         $resp->assertStatus(200);
67         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.zip"');
68
69         $zip = ZipTestHelper::extractFromZipResponse($resp);
70         $this->assertArrayHasKey('book', $zip->data);
71     }
72
73     public function test_chapter_html_endpoint()
74     {
75         $this->actingAsApiEditor();
76         $chapter = $this->entities->chapter();
77
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"');
82     }
83
84     public function test_chapter_plain_text_endpoint()
85     {
86         $this->actingAsApiEditor();
87         $chapter = $this->entities->chapter();
88
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"');
93     }
94
95     public function test_chapter_pdf_endpoint()
96     {
97         $this->actingAsApiEditor();
98         $chapter = $this->entities->chapter();
99
100         $resp = $this->get("/api/chapters/{$chapter->id}/export/pdf");
101         $resp->assertStatus(200);
102         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
103     }
104
105     public function test_chapter_markdown_endpoint()
106     {
107         $this->actingAsApiEditor();
108         $chapter = Chapter::visible()->has('pages')->first();
109
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);
115     }
116
117     public function test_chapter_zip_endpoint()
118     {
119         $this->actingAsApiEditor();
120         $chapter = Chapter::visible()->has('pages')->first();
121
122         $resp = $this->get("/api/chapters/{$chapter->id}/export/zip");
123         $resp->assertStatus(200);
124         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.zip"');
125
126         $zip = ZipTestHelper::extractFromZipResponse($resp);
127         $this->assertArrayHasKey('chapter', $zip->data);
128     }
129
130     public function test_page_html_endpoint()
131     {
132         $this->actingAsApiEditor();
133         $page = $this->entities->page();
134
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"');
139     }
140
141     public function test_page_plain_text_endpoint()
142     {
143         $this->actingAsApiEditor();
144         $page = $this->entities->page();
145
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"');
150     }
151
152     public function test_page_pdf_endpoint()
153     {
154         $this->actingAsApiEditor();
155         $page = $this->entities->page();
156
157         $resp = $this->get("/api/pages/{$page->id}/export/pdf");
158         $resp->assertStatus(200);
159         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
160     }
161
162     public function test_page_markdown_endpoint()
163     {
164         $this->actingAsApiEditor();
165         $page = $this->entities->page();
166
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"');
171     }
172
173     public function test_page_zip_endpoint()
174     {
175         $this->actingAsApiEditor();
176         $page = $this->entities->page();
177
178         $resp = $this->get("/api/pages/{$page->id}/export/zip");
179         $resp->assertStatus(200);
180         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.zip"');
181
182         $zip = ZipTestHelper::extractFromZipResponse($resp);
183         $this->assertArrayHasKey('page', $zip->data);
184     }
185
186     public function test_cant_export_when_not_have_permission()
187     {
188         $types = ['html', 'plaintext', 'pdf', 'markdown', 'zip'];
189         $this->actingAsApiEditor();
190         $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
191
192         $book = $this->entities->book();
193         foreach ($types as $type) {
194             $resp = $this->get("/api/books/{$book->id}/export/{$type}");
195             $this->assertPermissionError($resp);
196         }
197
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);
202         }
203
204         $page = $this->entities->page();
205         foreach ($types as $type) {
206             $resp = $this->get("/api/pages/{$page->id}/export/{$type}");
207             $this->assertPermissionError($resp);
208         }
209     }
210 }