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 public function test_user_images_deleted_on_user_deletion()
222 $editor = $this->getEditor();
223 $this->actingAs($editor);
225 $imageName = 'profile.png';
226 $relPath = $this->getTestImagePath('gallery', $imageName);
227 $this->deleteImage($relPath);
229 $file = $this->getTestImage($imageName);
230 $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
231 $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
233 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
234 $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
236 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
237 $userDelete->assertStatus(302);
238 $this->assertDatabaseMissing('images', [
240 'created_by' => $editor->id
244 public function test_deleted_unused_images()
246 $page = Page::first();
247 $admin = $this->getAdmin();
248 $this->actingAs($admin);
250 $imageName = 'unused-image.png';
251 $relPath = $this->getTestImagePath('gallery', $imageName);
252 $this->deleteImage($relPath);
254 $upload = $this->uploadImage($imageName, $page->id);
255 $upload->assertStatus(200);
256 $image = Image::where('type', '=', 'gallery')->first();
258 $pageRepo = app(PageRepo::class);
259 $pageRepo->updatePage($page, $page->book_id, [
260 'name' => $page->name,
261 'html' => $page->html . "<img src=\"{$image->url}\">",
265 // Ensure no images are reported as deletable
266 $imageService = app(ImageService::class);
267 $toDelete = $imageService->deleteUnusedImages(true, true);
268 $this->assertCount(0, $toDelete);
270 // Save a revision of our page without the image;
271 $pageRepo->updatePage($page, $page->book_id, [
272 'name' => $page->name,
273 'html' => "<p>Hello</p>",
277 // Ensure revision images are picked up okay
278 $imageService = app(ImageService::class);
279 $toDelete = $imageService->deleteUnusedImages(true, true);
280 $this->assertCount(0, $toDelete);
281 $toDelete = $imageService->deleteUnusedImages(false, true);
282 $this->assertCount(1, $toDelete);
284 // Check image is found when revisions are destroyed
285 $page->revisions()->delete();
286 $toDelete = $imageService->deleteUnusedImages(true, true);
287 $this->assertCount(1, $toDelete);
289 // Check the image is deleted
290 $absPath = public_path($relPath);
291 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
292 $toDelete = $imageService->deleteUnusedImages(true, false);
293 $this->assertCount(1, $toDelete);
294 $this->assertFalse(file_exists($absPath));
296 $this->deleteImage($relPath);