Skip to content

Commit d556845

Browse files
committed
API: Added zip export tests, reorganised tests
Extracted an extra method into helper for reuse.
1 parent d15eb12 commit d556845

File tree

6 files changed

+256
-216
lines changed

6 files changed

+256
-216
lines changed

tests/Api/BooksApiTest.php

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -287,62 +287,4 @@ public function test_delete_endpoint()
287287
$resp->assertStatus(204);
288288
$this->assertActivityExists('book_delete');
289289
}
290-
291-
public function test_export_html_endpoint()
292-
{
293-
$this->actingAsApiEditor();
294-
$book = $this->entities->book();
295-
296-
$resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
297-
$resp->assertStatus(200);
298-
$resp->assertSee($book->name);
299-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
300-
}
301-
302-
public function test_export_plain_text_endpoint()
303-
{
304-
$this->actingAsApiEditor();
305-
$book = $this->entities->book();
306-
307-
$resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
308-
$resp->assertStatus(200);
309-
$resp->assertSee($book->name);
310-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
311-
}
312-
313-
public function test_export_pdf_endpoint()
314-
{
315-
$this->actingAsApiEditor();
316-
$book = $this->entities->book();
317-
318-
$resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
319-
$resp->assertStatus(200);
320-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
321-
}
322-
323-
public function test_export_markdown_endpoint()
324-
{
325-
$this->actingAsApiEditor();
326-
$book = Book::visible()->has('pages')->has('chapters')->first();
327-
328-
$resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
329-
$resp->assertStatus(200);
330-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
331-
$resp->assertSee('# ' . $book->name);
332-
$resp->assertSee('# ' . $book->pages()->first()->name);
333-
$resp->assertSee('# ' . $book->chapters()->first()->name);
334-
}
335-
336-
public function test_cant_export_when_not_have_permission()
337-
{
338-
$types = ['html', 'plaintext', 'pdf', 'markdown'];
339-
$this->actingAsApiEditor();
340-
$this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
341-
342-
$book = $this->entities->book();
343-
foreach ($types as $type) {
344-
$resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
345-
$this->assertPermissionError($resp);
346-
}
347-
}
348290
}

tests/Api/ChaptersApiTest.php

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -269,61 +269,4 @@ public function test_delete_endpoint()
269269
$resp->assertStatus(204);
270270
$this->assertActivityExists('chapter_delete');
271271
}
272-
273-
public function test_export_html_endpoint()
274-
{
275-
$this->actingAsApiEditor();
276-
$chapter = $this->entities->chapter();
277-
278-
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
279-
$resp->assertStatus(200);
280-
$resp->assertSee($chapter->name);
281-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
282-
}
283-
284-
public function test_export_plain_text_endpoint()
285-
{
286-
$this->actingAsApiEditor();
287-
$chapter = $this->entities->chapter();
288-
289-
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
290-
$resp->assertStatus(200);
291-
$resp->assertSee($chapter->name);
292-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
293-
}
294-
295-
public function test_export_pdf_endpoint()
296-
{
297-
$this->actingAsApiEditor();
298-
$chapter = $this->entities->chapter();
299-
300-
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
301-
$resp->assertStatus(200);
302-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
303-
}
304-
305-
public function test_export_markdown_endpoint()
306-
{
307-
$this->actingAsApiEditor();
308-
$chapter = Chapter::visible()->has('pages')->first();
309-
310-
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
311-
$resp->assertStatus(200);
312-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
313-
$resp->assertSee('# ' . $chapter->name);
314-
$resp->assertSee('# ' . $chapter->pages()->first()->name);
315-
}
316-
317-
public function test_cant_export_when_not_have_permission()
318-
{
319-
$types = ['html', 'plaintext', 'pdf', 'markdown'];
320-
$this->actingAsApiEditor();
321-
$this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
322-
323-
$chapter = Chapter::visible()->has('pages')->first();
324-
foreach ($types as $type) {
325-
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
326-
$this->assertPermissionError($resp);
327-
}
328-
}
329272
}

tests/Api/ExportsApiTest.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
}

tests/Api/PagesApiTest.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -308,60 +308,4 @@ public function test_delete_endpoint()
308308
$resp->assertStatus(204);
309309
$this->assertActivityExists('page_delete', $page);
310310
}
311-
312-
public function test_export_html_endpoint()
313-
{
314-
$this->actingAsApiEditor();
315-
$page = $this->entities->page();
316-
317-
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
318-
$resp->assertStatus(200);
319-
$resp->assertSee($page->name);
320-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
321-
}
322-
323-
public function test_export_plain_text_endpoint()
324-
{
325-
$this->actingAsApiEditor();
326-
$page = $this->entities->page();
327-
328-
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
329-
$resp->assertStatus(200);
330-
$resp->assertSee($page->name);
331-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
332-
}
333-
334-
public function test_export_pdf_endpoint()
335-
{
336-
$this->actingAsApiEditor();
337-
$page = $this->entities->page();
338-
339-
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
340-
$resp->assertStatus(200);
341-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
342-
}
343-
344-
public function test_export_markdown_endpoint()
345-
{
346-
$this->actingAsApiEditor();
347-
$page = $this->entities->page();
348-
349-
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/markdown");
350-
$resp->assertStatus(200);
351-
$resp->assertSee('# ' . $page->name);
352-
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
353-
}
354-
355-
public function test_cant_export_when_not_have_permission()
356-
{
357-
$types = ['html', 'plaintext', 'pdf', 'markdown'];
358-
$this->actingAsApiEditor();
359-
$this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
360-
361-
$page = $this->entities->page();
362-
foreach ($types as $type) {
363-
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/{$type}");
364-
$this->assertPermissionError($resp);
365-
}
366-
}
367311
}

0 commit comments

Comments
 (0)