]> BookStack Code Mirror - bookstack/blob - tests/Entity/ExportTest.php
Refactored the code for ExportService to use DomDocument.
[bookstack] / tests / Entity / ExportTest.php
1 <?php namespace Tests;
2
3
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6
7 class ExportTest extends TestCase
8 {
9
10     public function test_page_text_export()
11     {
12         $page = Page::first();
13         $this->asEditor();
14
15         $resp = $this->get($page->getUrl('/export/plaintext'));
16         $resp->assertStatus(200);
17         $resp->assertSee($page->name);
18         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
19     }
20
21     public function test_page_pdf_export()
22     {
23         $page = Page::first();
24         $this->asEditor();
25
26         $resp = $this->get($page->getUrl('/export/pdf'));
27         $resp->assertStatus(200);
28         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
29     }
30
31     public function test_page_html_export()
32     {
33         $page = Page::first();
34         $this->asEditor();
35
36         $resp = $this->get($page->getUrl('/export/html'));
37         $resp->assertStatus(200);
38         $resp->assertSee($page->name);
39         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
40     }
41
42     public function test_book_text_export()
43     {
44         $page = Page::first();
45         $book = $page->book;
46         $this->asEditor();
47
48         $resp = $this->get($book->getUrl('/export/plaintext'));
49         $resp->assertStatus(200);
50         $resp->assertSee($book->name);
51         $resp->assertSee($page->name);
52         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
53     }
54
55     public function test_book_pdf_export()
56     {
57         $page = Page::first();
58         $book = $page->book;
59         $this->asEditor();
60
61         $resp = $this->get($book->getUrl('/export/pdf'));
62         $resp->assertStatus(200);
63         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
64     }
65
66     public function test_book_html_export()
67     {
68         $page = Page::first();
69         $book = $page->book;
70         $this->asEditor();
71
72         $resp = $this->get($book->getUrl('/export/html'));
73         $resp->assertStatus(200);
74         $resp->assertSee($book->name);
75         $resp->assertSee($page->name);
76         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
77     }
78
79     public function test_chapter_text_export()
80     {
81         $chapter = Chapter::first();
82         $page = $chapter->pages[0];
83         $this->asEditor();
84
85         $resp = $this->get($chapter->getUrl('/export/plaintext'));
86         $resp->assertStatus(200);
87         $resp->assertSee($chapter->name);
88         $resp->assertSee($page->name);
89         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
90     }
91
92     public function test_chapter_pdf_export()
93     {
94         $chapter = Chapter::first();
95         $this->asEditor();
96
97         $resp = $this->get($chapter->getUrl('/export/pdf'));
98         $resp->assertStatus(200);
99         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
100     }
101
102     public function test_chapter_html_export()
103     {
104         $chapter = Chapter::first();
105         $page = $chapter->pages[0];
106         $this->asEditor();
107
108         $resp = $this->get($chapter->getUrl('/export/html'));
109         $resp->assertStatus(200);
110         $resp->assertSee($chapter->name);
111         $resp->assertSee($page->name);
112         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
113     }
114
115     public function test_page_html_export_contains_custom_head_if_set()
116     {
117         $page = Page::first();
118
119         $customHeadContent = "<style>p{color: red;}</style>";
120         $this->setSettings(['app-custom-head' => $customHeadContent]);
121
122         $resp = $this->asEditor()->get($page->getUrl('/export/html'));
123         $resp->assertSee($customHeadContent);
124     }
125
126     public function test_page_html_export_use_absolute_dates()
127     {
128         $page = Page::first();
129
130         $resp = $this->asEditor()->get($page->getUrl('/export/html'));
131         $resp->assertSee($page->created_at->toDayDateTimeString());
132         $resp->assertDontSee($page->created_at->diffForHumans());
133         $resp->assertSee($page->updated_at->toDayDateTimeString());
134         $resp->assertDontSee($page->updated_at->diffForHumans());
135     }
136
137     public function test_html_export_media_protocol_updated()
138     {
139         $page = Page::first();
140         $page->html = '<p id="bkmrk-%C2%A0-0">&nbsp;</p><p id="bkmrk-%C2%A0-1"><iframe src="//www.youtube.com/embed/LkFt_fp7FmE" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p><p id="bkmrk-"><iframe src="//player.vimeo.com/video/276396369?title=0&amp;amp;byline=0" width="425" height="350" allowfullscreen="allowfullscreen"></iframe></p><p id="bkmrk--0"><iframe style="border: 0;" src="//maps.google.com/embed?testquery=true" width="600" height="450" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p><p id="bkmrk--1"><iframe src="//www.dailymotion.com/embed/video/x2rqgfm" width="480" height="432" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p><p id="bkmrk-%C2%A0-2">&nbsp;</p>';
141         $page->save();
142
143         $this->asEditor();
144         $resp = $this->get($page->getUrl('/export/html'));
145         $resp->assertStatus(200);
146
147         $checks = [
148             'https://www.youtube.com/embed/LkFt_fp7FmE',
149             'https://player.vimeo.com/video/276396369?title=0&amp;amp;byline=0',
150             'https://maps.google.com/embed?testquery=true',
151             'https://www.dailymotion.com/embed/video/x2rqgfm',
152         ];
153
154         foreach ($checks as $check) {
155             $resp->assertSee($check);
156         }
157
158     }
159
160     public function test_pdf_export_no_video_iframe() {
161         $page = Page::first();
162         $page->html = '<p id="bkmrk-%C2%A0-0">&nbsp;</p>' .
163             '<p id="bkmrk-%C2%A0-1"><iframe src="//www.youtube.com/embed/LkFt_fp7FmE" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p>' .
164             '<p id="bkmrk-"><video src="//player.vimeo.com/video/276396369?title=0&amp;amp;byline=0" width="425" height="350" allowfullscreen="allowfullscreen"></video></p>' .
165             '<p id="bkmrk--0"><iframe style="border: 0;" src="//maps.google.com/embed?testquery=true" width="600" height="450" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>' .
166             '<p id="bkmrk--1"><iframe src="//www.dailymotion.com/embed/video/x2rqgfm" width="480" height="432" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>' .
167             '<p id="bkmrk-%C2%A0-2">&nbsp;</p>';
168
169         $page->save();
170
171         $this->asEditor();
172         $resp = $this->get($page->getUrl('/export/pdf?isTesting=true'));
173         $resp->assertStatus(200);
174
175         $checks = [
176             '</video>',
177             '</iframe>'
178         ];
179
180         foreach ($checks as $check) {
181             $resp->assertDontSee($check);
182         }
183     }
184
185 }