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');
80 $this->assertDatabaseMissing('images', [
86 public function test_secure_images_uploads_to_correct_place()
88 config()->set('filesystems.default', 'local_secure');
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');
94 $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
95 $upload->assertStatus(200);
97 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
99 if (file_exists($expectedPath)) {
100 unlink($expectedPath);
104 public function test_secure_images_included_in_exports()
106 config()->set('filesystems.default', 'local_secure');
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');
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}\">";
116 $upload->assertStatus(200);
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');
122 if (file_exists($expectedPath)) {
123 unlink($expectedPath);
127 public function test_system_images_remain_public()
129 config()->set('filesystems.default', 'local_secure');
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');
135 $upload = $this->call('POST', '/images/system/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
136 $upload->assertStatus(200);
138 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
140 if (file_exists($expectedPath)) {
141 unlink($expectedPath);
145 public function test_image_delete()
147 $page = Page::first();
149 $imageName = 'first-image.png';
151 $this->uploadImage($imageName, $page->id);
152 $image = Image::first();
153 $relPath = $this->getTestImagePath('gallery', $imageName);
155 $delete = $this->delete( '/images/' . $image->id);
156 $delete->assertStatus(200);
158 $this->assertDatabaseMissing('images', [
159 'url' => $this->baseUrl . $relPath,
163 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
166 public function testBase64Get()
168 $page = Page::first();
170 $imageName = 'first-image.png';
172 $this->uploadImage($imageName, $page->id);
173 $image = Image::first();
175 $imageGet = $this->getJson("/images/base64/{$image->id}");
176 $imageGet->assertJson([
177 'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
181 public function test_drawing_base64_upload()
183 $page = Page::first();
184 $editor = $this->getEditor();
185 $this->actingAs($editor);
187 $upload = $this->postJson('images/drawing/upload', [
188 'uploaded_to' => $page->id,
189 'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
192 $upload->assertStatus(200);
193 $upload->assertJson([
195 'uploaded_to' => $page->id,
196 'created_by' => $editor->id,
197 'updated_by' => $editor->id,
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));
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");
208 public function test_user_images_deleted_on_user_deletion()
210 $editor = $this->getEditor();
211 $this->actingAs($editor);
213 $imageName = 'profile.png';
214 $relPath = $this->getTestImagePath('gallery', $imageName);
215 $this->deleteImage($relPath);
217 $file = $this->getTestImage($imageName);
218 $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
219 $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
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");
224 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
225 $userDelete->assertStatus(302);
226 $this->assertDatabaseMissing('images', [
228 'created_by' => $editor->id
232 public function test_deleted_unused_images()
234 $page = Page::first();
235 $admin = $this->getAdmin();
236 $this->actingAs($admin);
238 $imageName = 'unused-image.png';
239 $relPath = $this->getTestImagePath('gallery', $imageName);
240 $this->deleteImage($relPath);
242 $upload = $this->uploadImage($imageName, $page->id);
243 $upload->assertStatus(200);
244 $image = Image::where('type', '=', 'gallery')->first();
246 $pageRepo = app(PageRepo::class);
247 $pageRepo->updatePage($page, $page->book_id, [
248 'name' => $page->name,
249 'html' => $page->html . "<img src=\"{$image->url}\">",
253 // Ensure no images are reported as deletable
254 $imageService = app(ImageService::class);
255 $toDelete = $imageService->deleteUnusedImages(true, true);
256 $this->assertCount(0, $toDelete);
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>",
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);
272 // Check image is found when revisions are destroyed
273 $page->revisions()->delete();
274 $toDelete = $imageService->deleteUnusedImages(true, true);
275 $this->assertCount(1, $toDelete);
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));
284 $this->deleteImage($relPath);