]> BookStack Code Mirror - bookstack/blob - tests/Uploads/ImageTest.php
Enable syntax highlight for OCaml, Haskell, Rust
[bookstack] / tests / Uploads / ImageTest.php
1 <?php namespace Tests\Uploads;
2
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Uploads\Image;
5 use BookStack\Entities\Page;
6 use BookStack\Uploads\ImageService;
7 use Tests\TestCase;
8
9 class ImageTest extends TestCase
10 {
11
12     use UsesImages;
13
14     public function test_image_upload()
15     {
16         $page = Page::first();
17         $admin = $this->getAdmin();
18         $this->actingAs($admin);
19
20         $imageName = 'first-image.png';
21         $relPath = $this->getTestImagePath('gallery', $imageName);
22         $this->deleteImage($relPath);
23
24         $upload = $this->uploadImage($imageName, $page->id);
25         $upload->assertStatus(200);
26
27         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
28
29         $this->deleteImage($relPath);
30
31         $this->assertDatabaseHas('images', [
32             'url' => $this->baseUrl . $relPath,
33             'type' => 'gallery',
34             'uploaded_to' => $page->id,
35             'path' => $relPath,
36             'created_by' => $admin->id,
37             'updated_by' => $admin->id,
38             'name' => $imageName
39         ]);
40     }
41
42     public function test_php_files_cannot_be_uploaded()
43     {
44         $page = Page::first();
45         $admin = $this->getAdmin();
46         $this->actingAs($admin);
47
48         $fileName = 'bad.php';
49         $relPath = $this->getTestImagePath('gallery', $fileName);
50         $this->deleteImage($relPath);
51
52         $file = $this->getTestImage($fileName);
53         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
54         $upload->assertStatus(302);
55
56         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
57
58         $this->assertDatabaseMissing('images', [
59             'type' => 'gallery',
60             'name' => $fileName
61         ]);
62     }
63
64     public function test_php_like_files_cannot_be_uploaded()
65     {
66         $page = Page::first();
67         $admin = $this->getAdmin();
68         $this->actingAs($admin);
69
70         $fileName = 'bad.phtml';
71         $relPath = $this->getTestImagePath('gallery', $fileName);
72         $this->deleteImage($relPath);
73
74         $file = $this->getTestImage($fileName);
75         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
76         $upload->assertStatus(302);
77
78         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
79
80         $this->assertDatabaseMissing('images', [
81             'type' => 'gallery',
82             'name' => $fileName
83         ]);
84     }
85
86     public function test_secure_images_uploads_to_correct_place()
87     {
88         config()->set('filesystems.default', 'local_secure');
89         $this->asEditor();
90         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
91         $page = Page::first();
92         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
93
94         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
95         $upload->assertStatus(200);
96
97         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
98
99         if (file_exists($expectedPath)) {
100             unlink($expectedPath);
101         }
102     }
103
104     public function test_secure_images_included_in_exports()
105     {
106         config()->set('filesystems.default', 'local_secure');
107         $this->asEditor();
108         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
109         $page = Page::first();
110         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
111
112         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
113         $imageUrl = json_decode($upload->getContent(), true)['url'];
114         $page->html .= "<img src=\"{$imageUrl}\">";
115         $page->save();
116         $upload->assertStatus(200);
117
118         $encodedImageContent = base64_encode(file_get_contents($expectedPath));
119         $export = $this->get($page->getUrl('/export/html'));
120         $this->assertTrue(str_contains($export->getContent(), $encodedImageContent), 'Uploaded image in export content');
121
122         if (file_exists($expectedPath)) {
123             unlink($expectedPath);
124         }
125     }
126
127     public function test_system_images_remain_public()
128     {
129         config()->set('filesystems.default', 'local_secure');
130         $this->asEditor();
131         $galleryFile = $this->getTestImage('my-system-test-upload.png');
132         $page = Page::first();
133         $expectedPath = public_path('uploads/images/system/' . Date('Y-m-M') . '/my-system-test-upload.png');
134
135         $upload = $this->call('POST', '/images/system/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
136         $upload->assertStatus(200);
137
138         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
139
140         if (file_exists($expectedPath)) {
141             unlink($expectedPath);
142         }
143     }
144
145     public function test_image_delete()
146     {
147         $page = Page::first();
148         $this->asAdmin();
149         $imageName = 'first-image.png';
150
151         $this->uploadImage($imageName, $page->id);
152         $image = Image::first();
153         $relPath = $this->getTestImagePath('gallery', $imageName);
154
155         $delete = $this->delete( '/images/' . $image->id);
156         $delete->assertStatus(200);
157
158         $this->assertDatabaseMissing('images', [
159             'url' => $this->baseUrl . $relPath,
160             'type' => 'gallery'
161         ]);
162
163         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
164     }
165
166     public function testBase64Get()
167     {
168         $page = Page::first();
169         $this->asAdmin();
170         $imageName = 'first-image.png';
171
172         $this->uploadImage($imageName, $page->id);
173         $image = Image::first();
174
175         $imageGet = $this->getJson("/images/base64/{$image->id}");
176         $imageGet->assertJson([
177             'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
178         ]);
179     }
180
181     public function test_drawing_base64_upload()
182     {
183         $page = Page::first();
184         $editor = $this->getEditor();
185         $this->actingAs($editor);
186
187         $upload = $this->postJson('images/drawing/upload', [
188             'uploaded_to' => $page->id,
189             'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
190         ]);
191
192         $upload->assertStatus(200);
193         $upload->assertJson([
194             'type' => 'drawio',
195             'uploaded_to' => $page->id,
196             'created_by' => $editor->id,
197             'updated_by' => $editor->id,
198         ]);
199
200         $image = Image::where('type', '=', 'drawio')->first();
201         $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
202
203         $testImageData = file_get_contents($this->getTestImageFilePath());
204         $uploadedImageData = file_get_contents(public_path($image->path));
205         $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
206     }
207
208     public function test_user_images_deleted_on_user_deletion()
209     {
210         $editor = $this->getEditor();
211         $this->actingAs($editor);
212
213         $imageName = 'profile.png';
214         $relPath = $this->getTestImagePath('gallery', $imageName);
215         $this->deleteImage($relPath);
216
217         $file = $this->getTestImage($imageName);
218         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
219         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
220
221         $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
222         $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
223
224         $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
225         $userDelete->assertStatus(302);
226         $this->assertDatabaseMissing('images', [
227             'type' => 'user',
228             'created_by' => $editor->id
229         ]);
230     }
231
232     public function test_deleted_unused_images()
233     {
234         $page = Page::first();
235         $admin = $this->getAdmin();
236         $this->actingAs($admin);
237
238         $imageName = 'unused-image.png';
239         $relPath = $this->getTestImagePath('gallery', $imageName);
240         $this->deleteImage($relPath);
241
242         $upload = $this->uploadImage($imageName, $page->id);
243         $upload->assertStatus(200);
244         $image = Image::where('type', '=', 'gallery')->first();
245
246         $pageRepo = app(PageRepo::class);
247         $pageRepo->updatePage($page, $page->book_id, [
248             'name' => $page->name,
249             'html' => $page->html . "<img src=\"{$image->url}\">",
250             'summary' => ''
251         ]);
252
253         // Ensure no images are reported as deletable
254         $imageService = app(ImageService::class);
255         $toDelete = $imageService->deleteUnusedImages(true, true);
256         $this->assertCount(0, $toDelete);
257
258         // Save a revision of our page without the image;
259         $pageRepo->updatePage($page, $page->book_id, [
260             'name' => $page->name,
261             'html' => "<p>Hello</p>",
262             'summary' => ''
263         ]);
264
265         // Ensure revision images are picked up okay
266         $imageService = app(ImageService::class);
267         $toDelete = $imageService->deleteUnusedImages(true, true);
268         $this->assertCount(0, $toDelete);
269         $toDelete = $imageService->deleteUnusedImages(false, true);
270         $this->assertCount(1, $toDelete);
271
272         // Check image is found when revisions are destroyed
273         $page->revisions()->delete();
274         $toDelete = $imageService->deleteUnusedImages(true, true);
275         $this->assertCount(1, $toDelete);
276
277         // Check the image is deleted
278         $absPath = public_path($relPath);
279         $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
280         $toDelete = $imageService->deleteUnusedImages(true, false);
281         $this->assertCount(1, $toDelete);
282         $this->assertFalse(file_exists($absPath));
283
284         $this->deleteImage($relPath);
285     }
286
287 }