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;
7 use Illuminate\Support\Str;
10 class ImageTest extends TestCase
15 public function test_image_upload()
17 $page = Page::first();
18 $admin = $this->getAdmin();
19 $this->actingAs($admin);
21 $imgDetails = $this->uploadGalleryImage($page);
22 $relPath = $imgDetails['path'];
24 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
26 $this->deleteImage($relPath);
28 $this->assertDatabaseHas('images', [
29 'url' => $this->baseUrl . $relPath,
31 'uploaded_to' => $page->id,
33 'created_by' => $admin->id,
34 'updated_by' => $admin->id,
35 'name' => $imgDetails['name'],
39 public function test_image_display_thumbnail_generation_does_not_increase_image_size()
41 $page = Page::first();
42 $admin = $this->getAdmin();
43 $this->actingAs($admin);
45 $originalFile = $this->getTestImageFilePath('compressed.png');
46 $originalFileSize = filesize($originalFile);
47 $imgDetails = $this->uploadGalleryImage($page, 'compressed.png');
48 $relPath = $imgDetails['path'];
50 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
51 $displayImage = $imgDetails['response']->thumbs->display;
53 $displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
54 $displayImagePath = public_path($displayImageRelPath);
55 $displayFileSize = filesize($displayImagePath);
57 $this->deleteImage($relPath);
58 $this->deleteImage($displayImageRelPath);
60 $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
63 public function test_image_edit()
65 $editor = $this->getEditor();
66 $this->actingAs($editor);
68 $imgDetails = $this->uploadGalleryImage();
69 $image = Image::query()->first();
71 $newName = Str::random();
72 $update = $this->put('/images/' . $image->id, ['name' => $newName]);
73 $update->assertSuccessful();
80 $this->deleteImage($imgDetails['path']);
82 $this->assertDatabaseHas('images', [
88 public function test_gallery_get_list_format()
92 $imgDetails = $this->uploadGalleryImage();
93 $image = Image::query()->first();
95 $emptyJson = ['images' => [], 'has_more' => false];
100 'name' => $imgDetails['name'],
106 $pageId = $imgDetails['page']->id;
107 $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
108 $firstPageRequest->assertSuccessful()->assertJson($resultJson);
110 $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
111 $secondPageRequest->assertSuccessful()->assertExactJson($emptyJson);
113 $namePartial = substr($imgDetails['name'], 0, 3);
114 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
115 $searchHitRequest->assertSuccessful()->assertJson($resultJson);
117 $namePartial = Str::random(16);
118 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
119 $searchHitRequest->assertSuccessful()->assertExactJson($emptyJson);
122 public function test_image_usage()
124 $page = Page::first();
125 $editor = $this->getEditor();
126 $this->actingAs($editor);
128 $imgDetails = $this->uploadGalleryImage($page);
130 $image = Image::query()->first();
131 $page->html = '<img src="'.$image->url.'">';
134 $usage = $this->get('/images/usage/' . $image->id);
135 $usage->assertSuccessful();
139 'name' => $page->name
143 $this->deleteImage($imgDetails['path']);
146 public function test_php_files_cannot_be_uploaded()
148 $page = Page::first();
149 $admin = $this->getAdmin();
150 $this->actingAs($admin);
152 $fileName = 'bad.php';
153 $relPath = $this->getTestImagePath('gallery', $fileName);
154 $this->deleteImage($relPath);
156 $file = $this->getTestImage($fileName);
157 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
158 $upload->assertStatus(302);
160 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
162 $this->assertDatabaseMissing('images', [
168 public function test_php_like_files_cannot_be_uploaded()
170 $page = Page::first();
171 $admin = $this->getAdmin();
172 $this->actingAs($admin);
174 $fileName = 'bad.phtml';
175 $relPath = $this->getTestImagePath('gallery', $fileName);
176 $this->deleteImage($relPath);
178 $file = $this->getTestImage($fileName);
179 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
180 $upload->assertStatus(302);
182 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
185 public function test_files_with_double_extensions_cannot_be_uploaded()
187 $page = Page::first();
188 $admin = $this->getAdmin();
189 $this->actingAs($admin);
191 $fileName = 'bad.phtml.png';
192 $relPath = $this->getTestImagePath('gallery', $fileName);
193 $this->deleteImage($relPath);
195 $file = $this->getTestImage($fileName);
196 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
197 $upload->assertStatus(302);
199 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded double extension file was uploaded but should have been stopped');
202 public function test_secure_images_uploads_to_correct_place()
204 config()->set('filesystems.images', 'local_secure');
206 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
207 $page = Page::first();
208 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
210 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
211 $upload->assertStatus(200);
213 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
215 if (file_exists($expectedPath)) {
216 unlink($expectedPath);
220 public function test_secure_images_included_in_exports()
222 config()->set('filesystems.images', 'local_secure');
224 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
225 $page = Page::first();
226 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
228 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
229 $imageUrl = json_decode($upload->getContent(), true)['url'];
230 $page->html .= "<img src=\"{$imageUrl}\">";
232 $upload->assertStatus(200);
234 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
235 $export = $this->get($page->getUrl('/export/html'));
236 $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
238 if (file_exists($expectedPath)) {
239 unlink($expectedPath);
243 public function test_system_images_remain_public()
245 config()->set('filesystems.images', 'local_secure');
247 $galleryFile = $this->getTestImage('my-system-test-upload.png');
248 $expectedPath = public_path('uploads/images/system/' . Date('Y-m') . '/my-system-test-upload.png');
250 $upload = $this->call('POST', '/settings', [], [], ['app_logo' => $galleryFile], []);
251 $upload->assertRedirect('/settings');
253 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
255 if (file_exists($expectedPath)) {
256 unlink($expectedPath);
260 public function test_image_delete()
262 $page = Page::first();
264 $imageName = 'first-image.png';
266 $this->uploadImage($imageName, $page->id);
267 $image = Image::first();
268 $relPath = $this->getTestImagePath('gallery', $imageName);
270 $delete = $this->delete( '/images/' . $image->id);
271 $delete->assertStatus(200);
273 $this->assertDatabaseMissing('images', [
274 'url' => $this->baseUrl . $relPath,
278 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
281 public function testBase64Get()
283 $page = Page::first();
285 $imageName = 'first-image.png';
287 $this->uploadImage($imageName, $page->id);
288 $image = Image::first();
289 $image->type = 'drawio';
292 $imageGet = $this->getJson("/images/drawio/base64/{$image->id}");
293 $imageGet->assertJson([
294 'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
298 public function test_drawing_base64_upload()
300 $page = Page::first();
301 $editor = $this->getEditor();
302 $this->actingAs($editor);
304 $upload = $this->postJson('images/drawio', [
305 'uploaded_to' => $page->id,
306 'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
309 $upload->assertStatus(200);
310 $upload->assertJson([
312 'uploaded_to' => $page->id,
313 'created_by' => $editor->id,
314 'updated_by' => $editor->id,
317 $image = Image::where('type', '=', 'drawio')->first();
318 $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
320 $testImageData = file_get_contents($this->getTestImageFilePath());
321 $uploadedImageData = file_get_contents(public_path($image->path));
322 $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
325 protected function getTestProfileImage()
327 $imageName = 'profile.png';
328 $relPath = $this->getTestImagePath('user', $imageName);
329 $this->deleteImage($relPath);
331 return $this->getTestImage($imageName);
334 public function test_user_image_upload()
336 $editor = $this->getEditor();
337 $admin = $this->getAdmin();
338 $this->actingAs($admin);
340 $file = $this->getTestProfileImage();
341 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
343 $this->assertDatabaseHas('images', [
345 'uploaded_to' => $editor->id,
346 'created_by' => $admin->id,
350 public function test_user_images_deleted_on_user_deletion()
352 $editor = $this->getEditor();
353 $this->actingAs($editor);
355 $file = $this->getTestProfileImage();
356 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
358 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
359 $this->assertTrue($profileImages->count() === 1, "Found profile images does not match upload count");
361 $imagePath = public_path($profileImages->first()->path);
362 $this->assertTrue(file_exists($imagePath));
364 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
365 $userDelete->assertStatus(302);
367 $this->assertDatabaseMissing('images', [
369 'created_by' => $editor->id
371 $this->assertDatabaseMissing('images', [
373 'uploaded_to' => $editor->id
376 $this->assertFalse(file_exists($imagePath));
379 public function test_deleted_unused_images()
381 $page = Page::first();
382 $admin = $this->getAdmin();
383 $this->actingAs($admin);
385 $imageName = 'unused-image.png';
386 $relPath = $this->getTestImagePath('gallery', $imageName);
387 $this->deleteImage($relPath);
389 $upload = $this->uploadImage($imageName, $page->id);
390 $upload->assertStatus(200);
391 $image = Image::where('type', '=', 'gallery')->first();
393 $pageRepo = app(PageRepo::class);
394 $pageRepo->update($page, [
395 'name' => $page->name,
396 'html' => $page->html . "<img src=\"{$image->url}\">",
400 // Ensure no images are reported as deletable
401 $imageService = app(ImageService::class);
402 $toDelete = $imageService->deleteUnusedImages(true, true);
403 $this->assertCount(0, $toDelete);
405 // Save a revision of our page without the image;
406 $pageRepo->update($page, [
407 'name' => $page->name,
408 'html' => "<p>Hello</p>",
412 // Ensure revision images are picked up okay
413 $imageService = app(ImageService::class);
414 $toDelete = $imageService->deleteUnusedImages(true, true);
415 $this->assertCount(0, $toDelete);
416 $toDelete = $imageService->deleteUnusedImages(false, true);
417 $this->assertCount(1, $toDelete);
419 // Check image is found when revisions are destroyed
420 $page->revisions()->delete();
421 $toDelete = $imageService->deleteUnusedImages(true, true);
422 $this->assertCount(1, $toDelete);
424 // Check the image is deleted
425 $absPath = public_path($relPath);
426 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
427 $toDelete = $imageService->deleteUnusedImages(true, false);
428 $this->assertCount(1, $toDelete);
429 $this->assertFalse(file_exists($absPath));
431 $this->deleteImage($relPath);