1 <?php namespace Tests\Uploads;
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Uploads\Image;
5 use BookStack\Entities\Page;
6 use BookStack\Uploads\ImageService;
9 class ImageTest extends TestCase
14 public function test_image_upload()
16 $page = Page::first();
17 $admin = $this->getAdmin();
18 $this->actingAs($admin);
20 $imageName = 'first-image.png';
21 $relPath = $this->getTestImagePath('gallery', $imageName);
22 $this->deleteImage($relPath);
24 $upload = $this->uploadImage($imageName, $page->id);
25 $upload->assertStatus(200);
27 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
29 $this->deleteImage($relPath);
31 $this->assertDatabaseHas('images', [
32 'url' => $this->baseUrl . $relPath,
34 'uploaded_to' => $page->id,
36 'created_by' => $admin->id,
37 'updated_by' => $admin->id,
42 public function test_php_files_cannot_be_uploaded()
44 $page = Page::first();
45 $admin = $this->getAdmin();
46 $this->actingAs($admin);
48 $fileName = 'bad.php';
49 $relPath = $this->getTestImagePath('gallery', $fileName);
50 $this->deleteImage($relPath);
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);
56 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
58 $this->assertDatabaseMissing('images', [
64 public function test_php_like_files_cannot_be_uploaded()
66 $page = Page::first();
67 $admin = $this->getAdmin();
68 $this->actingAs($admin);
70 $fileName = 'bad.phtml';
71 $relPath = $this->getTestImagePath('gallery', $fileName);
72 $this->deleteImage($relPath);
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);
78 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
81 public function test_files_with_double_extensions_cannot_be_uploaded()
83 $page = Page::first();
84 $admin = $this->getAdmin();
85 $this->actingAs($admin);
87 $fileName = 'bad.phtml.png';
88 $relPath = $this->getTestImagePath('gallery', $fileName);
89 $this->deleteImage($relPath);
91 $file = $this->getTestImage($fileName);
92 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
93 $upload->assertStatus(302);
95 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded double extension file was uploaded but should have been stopped');
98 public function test_secure_images_uploads_to_correct_place()
100 config()->set('filesystems.default', 'local_secure');
102 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
103 $page = Page::first();
104 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
106 $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
107 $upload->assertStatus(200);
109 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
111 if (file_exists($expectedPath)) {
112 unlink($expectedPath);
116 public function test_secure_images_included_in_exports()
118 config()->set('filesystems.default', 'local_secure');
120 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
121 $page = Page::first();
122 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
124 $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
125 $imageUrl = json_decode($upload->getContent(), true)['url'];
126 $page->html .= "<img src=\"{$imageUrl}\">";
128 $upload->assertStatus(200);
130 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
131 $export = $this->get($page->getUrl('/export/html'));
132 $this->assertTrue(str_contains($export->getContent(), $encodedImageContent), 'Uploaded image in export content');
134 if (file_exists($expectedPath)) {
135 unlink($expectedPath);
139 public function test_system_images_remain_public()
141 config()->set('filesystems.default', 'local_secure');
143 $galleryFile = $this->getTestImage('my-system-test-upload.png');
144 $page = Page::first();
145 $expectedPath = public_path('uploads/images/system/' . Date('Y-m-M') . '/my-system-test-upload.png');
147 $upload = $this->call('POST', '/images/system/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
148 $upload->assertStatus(200);
150 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
152 if (file_exists($expectedPath)) {
153 unlink($expectedPath);
157 public function test_image_delete()
159 $page = Page::first();
161 $imageName = 'first-image.png';
163 $this->uploadImage($imageName, $page->id);
164 $image = Image::first();
165 $relPath = $this->getTestImagePath('gallery', $imageName);
167 $delete = $this->delete( '/images/' . $image->id);
168 $delete->assertStatus(200);
170 $this->assertDatabaseMissing('images', [
171 'url' => $this->baseUrl . $relPath,
175 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
178 public function testBase64Get()
180 $page = Page::first();
182 $imageName = 'first-image.png';
184 $this->uploadImage($imageName, $page->id);
185 $image = Image::first();
187 $imageGet = $this->getJson("/images/base64/{$image->id}");
188 $imageGet->assertJson([
189 'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
193 public function test_drawing_base64_upload()
195 $page = Page::first();
196 $editor = $this->getEditor();
197 $this->actingAs($editor);
199 $upload = $this->postJson('images/drawing/upload', [
200 'uploaded_to' => $page->id,
201 'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
204 $upload->assertStatus(200);
205 $upload->assertJson([
207 'uploaded_to' => $page->id,
208 'created_by' => $editor->id,
209 'updated_by' => $editor->id,
212 $image = Image::where('type', '=', 'drawio')->first();
213 $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
215 $testImageData = file_get_contents($this->getTestImageFilePath());
216 $uploadedImageData = file_get_contents(public_path($image->path));
217 $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
220 protected function getTestProfileImage()
222 $imageName = 'profile.png';
223 $relPath = $this->getTestImagePath('user', $imageName);
224 $this->deleteImage($relPath);
226 return $this->getTestImage($imageName);
229 public function test_user_image_upload()
231 $editor = $this->getEditor();
232 $admin = $this->getAdmin();
233 $this->actingAs($admin);
235 $file = $this->getTestProfileImage();
236 $this->call('POST', '/images/user/upload', ['uploaded_to' => $editor->id], [], ['file' => $file], []);
238 $this->assertDatabaseHas('images', [
240 'uploaded_to' => $editor->id,
241 'created_by' => $admin->id,
245 public function test_standard_user_with_manage_users_permission_can_view_other_profile_images()
247 $editor = $this->getEditor();
248 $this->giveUserPermissions($editor, ['users-manage']);
250 $admin = $this->getAdmin();
252 $this->actingAs($admin);
253 $file = $this->getTestProfileImage();
254 $this->call('POST', '/images/user/upload', ['uploaded_to' => $admin->id], [], ['file' => $file], []);
257 'name' => 'profile.png',
258 'uploaded_to' => $admin->id,
262 $this->actingAs($editor);
263 $adminImagesGet = $this->get("/images/user/all/0?uploaded_to=" . $admin->id);
264 $adminImagesGet->assertStatus(200)->assertJsonFragment($expectedJson);
266 $allImagesGet = $this->get("/images/user/all/0");
267 $allImagesGet->assertStatus(200)->assertJsonFragment($expectedJson);
270 public function test_standard_user_cant_view_other_profile_images()
272 $editor = $this->getEditor();
273 $admin = $this->getAdmin();
275 $this->actingAs($admin);
276 $file = $this->getTestProfileImage();
277 $this->call('POST', '/images/user/upload', ['uploaded_to' => $admin->id], [], ['file' => $file], []);
279 $this->actingAs($editor);
280 $adminImagesGet = $this->get("/images/user/all/0?uploaded_to=" . $admin->id);
281 $adminImagesGet->assertStatus(302);
283 $allImagesGet = $this->get("/images/user/all/0");
284 $allImagesGet->assertStatus(302);
287 public function test_standard_user_cant_upload_other_profile_images()
289 $editor = $this->getEditor();
290 $admin = $this->getAdmin();
292 $this->actingAs($editor);
293 $file = $this->getTestProfileImage();
294 $upload = $this->call('POST', '/images/user/upload', ['uploaded_to' => $admin->id], [], ['file' => $file], []);
295 $upload->assertStatus(302);
297 $this->assertDatabaseMissing('images', [
299 'uploaded_to' => $admin->id,
303 public function test_user_images_deleted_on_user_deletion()
305 $editor = $this->getEditor();
306 $this->actingAs($editor);
308 $file = $this->getTestProfileImage();
309 $this->call('POST', '/images/user/upload', ['uploaded_to' => $editor->id], [], ['file' => $file], []);
310 $this->call('POST', '/images/user/upload', ['uploaded_to' => $editor->id], [], ['file' => $file], []);
312 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
313 $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
315 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
316 $userDelete->assertStatus(302);
317 $this->assertDatabaseMissing('images', [
319 'created_by' => $editor->id
321 $this->assertDatabaseMissing('images', [
323 'uploaded_to' => $editor->id
327 public function test_deleted_unused_images()
329 $page = Page::first();
330 $admin = $this->getAdmin();
331 $this->actingAs($admin);
333 $imageName = 'unused-image.png';
334 $relPath = $this->getTestImagePath('gallery', $imageName);
335 $this->deleteImage($relPath);
337 $upload = $this->uploadImage($imageName, $page->id);
338 $upload->assertStatus(200);
339 $image = Image::where('type', '=', 'gallery')->first();
341 $pageRepo = app(PageRepo::class);
342 $pageRepo->updatePage($page, $page->book_id, [
343 'name' => $page->name,
344 'html' => $page->html . "<img src=\"{$image->url}\">",
348 // Ensure no images are reported as deletable
349 $imageService = app(ImageService::class);
350 $toDelete = $imageService->deleteUnusedImages(true, true);
351 $this->assertCount(0, $toDelete);
353 // Save a revision of our page without the image;
354 $pageRepo->updatePage($page, $page->book_id, [
355 'name' => $page->name,
356 'html' => "<p>Hello</p>",
360 // Ensure revision images are picked up okay
361 $imageService = app(ImageService::class);
362 $toDelete = $imageService->deleteUnusedImages(true, true);
363 $this->assertCount(0, $toDelete);
364 $toDelete = $imageService->deleteUnusedImages(false, true);
365 $this->assertCount(1, $toDelete);
367 // Check image is found when revisions are destroyed
368 $page->revisions()->delete();
369 $toDelete = $imageService->deleteUnusedImages(true, true);
370 $this->assertCount(1, $toDelete);
372 // Check the image is deleted
373 $absPath = public_path($relPath);
374 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
375 $toDelete = $imageService->deleteUnusedImages(true, false);
376 $this->assertCount(1, $toDelete);
377 $this->assertFalse(file_exists($absPath));
379 $this->deleteImage($relPath);