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